Posts: 16
Threads: 1
Joined: Apr 2008
04-21-2008, 06:15 PM
(This post was last modified: 04-24-2008, 02:45 AM by Frontrunner.)
Hi all...
My challenge is to write a routine which will translate correctly (in all circumstances) pow functions as done in C.
The winner we judged on two things.
1) Translations must be correct
2) Efficiency
Example:
Basic
r1 = (-c + (SQR(b(x) ^ 2 - (4 * a * c)))) / (2 * a)
C
r1 = (-c + (sqrt(pow(b(x), 2) - (4 * a * c)))) / (2 * a)
Good luck!
Fronrunner
Posts: 1,138
Threads: 211
Joined: Feb 2020
Sounds like an interesting, if quite specific, challenge. Its too bad I'm busy at the moment as I have always wanted to get around to writing parsers.
Posts: 16
Threads: 1
Joined: Apr 2008
That's right, it is quite specific but a general parser would be too easy for a challenge :
Cheers,
Frontrunner
Posts: 1,138
Threads: 211
Joined: Feb 2020
Hopefully I can get some time to have a go, seems straight forward enough but will see.
Posts: 16
Threads: 1
Joined: Apr 2008
I am looking forward to see your contribution!
Cheers,
Frontrunner
Posts: 544
Threads: 27
Joined: Jan 2005
Could you please be patient with me and tell me what POW stands for? The only meaning I have for those three letters at present is, Prisoner Of War, and, I'm sure, that is not what is intended.Â
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 16
Threads: 1
Joined: Apr 2008
Hi Ralph,
Sure I will try to explain you what POW means:
Like in qbasic there is in C a mathematical function to compute the power exponent.
In qbasic we use ^ but in C it is called POW.
I will give some more example in both C and bqasic so you can see the differences.
Example in C
 printf ("7 ^ 3 = %lf\n", pow (7,3));
 printf ("4.73 ^ 12 = %lf\n", pow (4.73,12));
 printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
Example in qbasic
 print "7 ^ 3 = ", 7 ^ 3
 print "4.73 ^ 3 = ", 4.73 ^ 12
 print "32.01 ^ 1.54 = ", 32.01 ^ 1.54
Both should output something like this:
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
Please forget the C syntax you see in the example.
A correct translation (according to this challenge) should translate print 3 ^ 7 to print pow(3,7).
Now this looks easy but things start to get more complicated when translating
print (10 ^ 2) - 5 + (21 * (3 - 4 ^ 6) * 2 + 10 - 5) ^ 2 + 3 - (3 + 7) + 99
To
print pow(10,2))-5+pow((21*(3-pow(4,6))*2+10-5),2)+3-(3+7)+99)
I hope that helped a little.
Kind regards,
Frontrunner
Posts: 544
Threads: 27
Joined: Jan 2005
Frontrunner:
Yes, I now understand what your challenge is all about; translating a qb expression with a power expression into its equivalent code in C.Â
I am not into C, so, I can not compete. But, I know that others will!
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 16
Threads: 1
Joined: Apr 2008
Hi Ralph,
You are to some part right!
But please forget about the C part, as long as the POW and the ^ operator are being translated correctly.
I am not here on a qbasic forum for C codingÂ
Cheers,
Frontrunner
Posts: 1,138
Threads: 211
Joined: Feb 2020
I'm trying to dust off my head and have a go, I know its about parsing correcting but am confused by your original example:
r1 = (-b + (SQR(b(x) ^ 2 - (4 * a * c)))) / (2 * a)
Is b and b(x) an integer and an arrary respectively or am I miss reading, I've been out of maths and coding for too long