Qbasicnews.com
January 22, 2018, 05:12:04 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3
  Print  
Author Topic: Mathematical expression translator  (Read 29981 times)
Frontrunner
New Member

Posts: 16


« on: April 21, 2008, 10:15:33 AM »

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
« Last Edit: April 23, 2008, 06:45:29 PM by Frontrunner » Logged
wildcard
*.*
Administrator
__/--\__
*****
Posts: 2365



« Reply #1 on: April 21, 2008, 03:48:18 PM »

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.
Logged
Frontrunner
New Member

Posts: 16


« Reply #2 on: April 21, 2008, 04:08:58 PM »

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

Cheers,
Frontrunner
Logged
wildcard
*.*
Administrator
__/--\__
*****
Posts: 2365



« Reply #3 on: April 22, 2008, 04:17:20 AM »

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

Posts: 16


« Reply #4 on: April 22, 2008, 06:57:00 AM »

I am looking forward to see your contribution!

Cheers,
Frontrunner
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #5 on: April 22, 2008, 09:40:28 PM »

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
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Frontrunner
New Member

Posts: 16


« Reply #6 on: April 23, 2008, 03:54:24 AM »

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
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #7 on: April 23, 2008, 03:09:47 PM »

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! Smiley
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Frontrunner
New Member

Posts: 16


« Reply #8 on: April 23, 2008, 05:25:53 PM »

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  Cheesy

Cheers,
Frontrunner
Logged
wildcard
*.*
Administrator
__/--\__
*****
Posts: 2365



« Reply #9 on: April 23, 2008, 06:06:44 PM »

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 Smiley
Logged
Frontrunner
New Member

Posts: 16


« Reply #10 on: April 23, 2008, 06:52:54 PM »

Thank you for pointing me to the confusion.
I have changed the original example  Grin
b is indeed an array.

But you have seen it right that the challenge is all about an parsing correction!

Sorry for the confusion.

Kind regards,
Frontrunner
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #11 on: April 23, 2008, 06:56:09 PM »

There is nothing as good for explaining as one or more examples!  Could you post at least one, good example of a math expression and the qbasic equivalent code that you would want?  That would help a lot!!! Smiley
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
wildcard
*.*
Administrator
__/--\__
*****
Posts: 2365



« Reply #12 on: April 23, 2008, 07:19:35 PM »

Frontrunner: No problem, haven't got to using it yet though still working on the simpler bracket-less parser.

Ralph: The challenge is to write a converter/parser that changes x ^ y to pow(x,y) however in the context of a more complex equation such as posted.
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #13 on: April 23, 2008, 11:51:58 PM »

Thanks, Wildcard.  I've never tried things like this before.  At first thought, it would seem that the algorithm would look for each character ^, then, for each, compare the characters going backwards and consider where to stop, put a "(" there, then continue until either a "(" or any mathematical function is found; next, do the same, going forward.  If this is the way to do it, I can see I would flounder in all the considerations I would have to take into account!  It really is too much for me, and definetely, "Not my cup of tea!"
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Frontrunner
New Member

Posts: 16


« Reply #14 on: April 24, 2008, 05:39:17 PM »

Hi All...

Ralph I see you are understanding the problem quite good and your approach is also good.

As for your reference I will give another tricky example:

Basic
a = -x ^ 3 - 3 * x ^ 2 + 4 * x - (x ^ 3 + x ^ 2 - 2) * (-1)

C
a = -pow(x,3) - 3 * pow(x,2) + 4 * x - (pow(x,3) + pow(x,2) - 2) * (-1)

Kind regards,
Frontrunner
« Last Edit: April 26, 2008, 07:11:09 PM by Frontrunner » Logged
Pages: [1] 2 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!