Maths in a Scripting Language (Help!)
there is a sea of expertise and knowledge in these forums, so i hope i can get an answer to this thought ..
im baffled as to how maths operations are done in a scripting language (as i am writing one) any ideas, puesedo code etc would be of great help, im just heading off to flipcde to check their tutorial, ciao!
thanx in advance!
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)
http://www.silvermace.com/ -- My personal website
http://www.silvermace.com/ -- My personal website
Usually an expression tree gets build for a mathematical expression. In the internal nodes of the tree the mathematical operator's are stored, in the leaves the variables/numbers get stored. e.g.
a + b*(d - c)
would generate a tree like this (note I hope the html doesn't mess up the spaces):
After the tree has bin generated, calculating the result is pretty easy, recurs through the tree and calculate the result of each branch.
[edited by - George2 on July 5, 2002 9:27:53 AM]
[edited by - George2 on July 5, 2002 9:29:13 AM]
[edited by - George2 on July 5, 2002 9:30:05 AM]
a + b*(d - c)
would generate a tree like this (note I hope the html doesn't mess up the spaces):
+ / \ a * / \ b - / \ d c
After the tree has bin generated, calculating the result is pretty easy, recurs through the tree and calculate the result of each branch.
[edited by - George2 on July 5, 2002 9:27:53 AM]
[edited by - George2 on July 5, 2002 9:29:13 AM]
[edited by - George2 on July 5, 2002 9:30:05 AM]
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Search google about postfix expressions. It''s pretty easy to code, quite efficient it does not use recursion (it uses a stack instead).
-nosfy Lead programmerCreation Objet Inc.Zone Sudoku: une grille gratuite à chaque jour
What''s wrong with recursion, it''s easy, and it would take a huge expression to generate a stack overflow.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Omg, thanx to both of you, you dont know how much that helped!
Im mearly a 15 year old with a simple mind, thanx for the quick reply''s
Thanx again!
Im mearly a 15 year old with a simple mind, thanx for the quick reply''s
Thanx again!
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)
http://www.silvermace.com/ -- My personal website
http://www.silvermace.com/ -- My personal website
quote:
Original post by George2
What''s wrong with recursion, it''s easy, and it would take a huge expression to generate a stack overflow.
There''s nothing wrong with recursion. It''s merely that infix->postfix->execution is more linear (especially in evaluation) than recursion (consider when you have multiple decent paths - it''s just more tedious and more consumptive of memory).
For the record, the last two times I''ve done arithmetic/logical evaluation, I''ve done it recursively.
Strangely enough, I wrote myself a recursive descent parser yesterday, largely based on the grammar descibed in part 6 of Jack Crenshaw''s compiler construction tutorial (search the web for it). Basically, it comes down to a couple of simple concepts. Firstly, you need to be able to describe mathematical expressions in a formal way, which is where the ''grammar'' comes in. (Search the web for Backus-Naur form if you want to see how this is usually done.) That grammar shows you all the different types of subexpression, which have their own function. Each of those functions does a little processing of its own, then looks at the next character in the expression to try and guess what subexpression to call next. This continues until you run out of valid characters (an error) or your first top-level function returns. Probably the easiest way to do this to begin with is to use 3 globals (1 for the expression, 1 for the current position within it, and 1 for the result), and write it procedurally. You can wrap all that in a class later if you like.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
Yay! I finnished it, I used the Postfix Method, nosfy was correct in stating that its easy to understand & code.
I first sorted the expressions into postfix, then converted to bytecode and programmed a Maths Virtual Machine to execute the bytecode =)
eg. 12*2+1=26 right!?
Postfix
[12 2 * 1 +]
ByteCode
Push 12
Push 2
Mul
Push 1
Add
VitualMachine
Go through the Stack, top->bottom.
Pop the Stack twice, compute the value, and Push the Result!
Hel presto, ur Left with the Result!!!
I''ll be Posting Code at my Site Soon!!
Switch|Darkside - Just Click It, You Know You Want to.
I first sorted the expressions into postfix, then converted to bytecode and programmed a Maths Virtual Machine to execute the bytecode =)
eg. 12*2+1=26 right!?
Postfix
[12 2 * 1 +]
ByteCode
Push 12
Push 2
Mul
Push 1
Add
VitualMachine
Go through the Stack, top->bottom.
Pop the Stack twice, compute the value, and Push the Result!
Hel presto, ur Left with the Result!!!
I''ll be Posting Code at my Site Soon!!
Switch|Darkside - Just Click It, You Know You Want to.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)
http://www.silvermace.com/ -- My personal website
http://www.silvermace.com/ -- My personal website
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement