Page 2 of 2

Re: Evaluate and process math expressions

Posted: Fri Apr 24, 2015 10:49 pm
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.

Re: Evaluate and process math expressions

Posted: Sun Apr 26, 2015 10:50 pm
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.

Re: Evaluate and process math expressions

Posted: Sun Apr 26, 2015 11:04 pm
by Thunder93
Thanks for the updates Little John! Really nice to see someone bettering in this area. :)

Re: Evaluate and process math expressions

Posted: Tue Sep 22, 2015 8:33 pm
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.

Re: Evaluate and process math expressions

Posted: Tue Sep 22, 2015 8:57 pm
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.

Re: Evaluate and process math expressions

Posted: Tue Sep 22, 2015 9:15 pm
by GPI
ok, thanks, than ^ must have a prio over 8...

edit:
nope, it should be 7.5... the first - is always special...

Re: Evaluate and process math expressions

Posted: Tue Sep 22, 2015 9:23 pm
by davido
@Little John,
Excellent.
Thank you for sharing. :D

Re: Evaluate and process math expressions

Posted: Tue Sep 22, 2015 9:47 pm
by Little John
Thank you, davido!
You are very welcome.

Re: Evaluate and process math expressions

Posted: Sat Sep 26, 2015 6:27 am
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 .