Evaluate and process math expressions

Share your advanced PureBasic knowledge/code with the community.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Evaluate and process math expressions

Post by jack »

thank you Little John
one of my dreams is to have PB pre-processor that would simulate operator and function overloading, you could then use a big-math library using normal syntax.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Evaluate and process math expressions

Post by Little John »

I mainly made the following changes to the pre-processor in the 2nd post:
  • New: Functions "Ceil()", "Trunc()", and "Floor()" can be replaced with the equivalent PB functions.
    More functions can be added easily.
  • New: Any boolean expression can be enclosed in a "Bool()" function.
  • Flags are used for controlling what the module will replace.
Support for the functions "Ceil()", "Trunc()", and "Floor()" was also added to the evaluator in the 1st post, and to the test code in the 4th post.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Evaluate and process math expressions

Post by Thunder93 »

Thanks for the updates Little John! Really nice to see someone bettering in this area. :)
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Evaluate and process math expressions

Post by GPI »

Question:
check("-9^0.5", "-3")
in my opinion this should fail!
-9^0.5 is equal to pow(-9,0.5) and this is not defined.

PB use this priority levels

Code: Select all

  Priority Level |     Operators
  ---------------+---------------------
       8 (high)  |      ~, -
       7         |    <<, >>, %, !
       6         |       |, &
       5         |       *, /
       4         |       +, -
       3         | >, >=, <, <=, =, <>
       2         |       Not
       1 (low)   |  And, Or, XOr
imo ^ should be lower than 8 and higher than 5 (in my code i use "7.5").

The first - is not prio 4, it is prio 8 - it is a sign.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Evaluate and process math expressions

Post by Little John »

GPI wrote:Question:
check("-9^0.5", "-3")
in my opinion this should fail!
-9^0.5 is equal to pow(-9,0.5) and this is not defined.
Hi,

it works as intended. My parser works according to the well established math rules:

Code: Select all

-9^0.5 = - (9^0.5) = -3
(-9)^0.5 = undefined (if we only work with real numbers)
Several programs ignore one or another math rule, but that's not what I want to do.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Evaluate and process math expressions

Post by GPI »

ok, thanks, than ^ must have a prio over 8...

edit:
nope, it should be 7.5... the first - is always special...
Last edited by GPI on Tue Sep 22, 2015 9:24 pm, edited 1 time in total.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Evaluate and process math expressions

Post by davido »

@Little John,
Excellent.
Thank you for sharing. :D
DE AA EB
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Evaluate and process math expressions

Post by Little John »

Thank you, davido!
You are very welcome.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Evaluate and process math expressions

Post by Demivec »

GPI wrote:ok, thanks, than ^ must have a prio over 8...

edit:
nope, it should be 7.5... the first - is always special...
^ would be above -, so that would make it >8 .

In mathematical operator precedence the unary operator - can be considered equal with the multiplication operator * .

This is evidence by writing things in this format: -1(x) = -x .

Since ^ has a higher priority than *, this would mean it also has a higher priority than - .

This in turn leads to the natural conclusion that -9^2 = -81, because it is the same as -1(9)^2 .
Post Reply