Qbasicnews.com
Mathematical expression translator - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html)
+--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html)
+--- Thread: Mathematical expression translator (/thread-10109.html)

Pages: 1 2 3 4


Mathematical expression translator - Frontrunner - 04-21-2008

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



Re: Mathematical expression translator - wildcard - 04-21-2008

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.


Re: Mathematical expression translator - Frontrunner - 04-22-2008

That's right, it is quite specific but a general parser would be too easy for a challenge :Smile

Cheers,
Frontrunner


Re: Mathematical expression translator - wildcard - 04-22-2008

Hopefully I can get some time to have a go, seems straight forward enough but will see.


Re: Mathematical expression translator - Frontrunner - 04-22-2008

I am looking forward to see your contribution!

Cheers,
Frontrunner


Re: Mathematical expression translator - Ralph - 04-23-2008

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.  Sad


Re: Mathematical expression translator - Frontrunner - 04-23-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


Re: Mathematical expression translator - Ralph - 04-23-2008

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! Smile


Re: Mathematical expression translator - Frontrunner - 04-24-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  Big Grin

Cheers,
Frontrunner


Re: Mathematical expression translator - wildcard - 04-24-2008

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 Smile