# Qbasicnews.com

Full Version: Mathematical expression translator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
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
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.
That's right, it is quite specific but a general parser would be too easy for a challenge :

Cheers,
Frontrunner
Hopefully I can get some time to have a go, seems straight forward enough but will see.
I am looking forward to see your contribution!

Cheers,
Frontrunner
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.Â
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
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!
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
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
Pages: 1 2 3 4