Page 2 of 3
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 7:31 pm
by heartbone
jack wrote:I am not sure about that
Mathematica 4^3^2 -> 262144
Maple 4^3^2 -> Error, power is non-associative
Wikipedia says the exponential operator is right associative
http://en.wikipedia.org/wiki/Operator_associativity
Maxima 4^3^2 -> 262144
Axiom 4^3^2 -> 262144
Reduce 4^3^2 -> 4096
so who's right?
skywalk wrote:vb6 wrote:debug.Print 4^3^2
4096
Wolfram Language: 262144
Microsoft Visual Basic 6: 4096
That pretty much settles it.
4^3^2 = (4^(3^2))
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 7:39 pm
by Little John
applePi wrote:Thanks Little John for the notes
applePi, you are welcome.
jack wrote:I am not sure about that
No problem.
I
am sure about that.

There are many things that I don't know.
However, there are are few things which I do know.

I'm now 57 years old. Believe it or not, I still remember how our math teacher explained this rule to us at school.
You are free to look it up in a serious math textbook of your choice.
jack wrote:Mathematica 4^3^2 -> 262144
Wolfram alpha is driven by Mathematica, so it's no surprise that both solutions are equal.
Even if someone doesn't know much abouth mathematics, s/he can rest assure that the probability that Mathematica is wrong in such simple cases is almost zero.
jack wrote:Maple 4^3^2 -> Error, power is non-associative
That seems to be a Maple specific syntax rule. Purely mathematically speaking, this is nonsense.
jack wrote:Reduce 4^3^2 -> 4096
Some expression evaluators do it wrong. That's the problem here.
The bottom line is: If in doubt, use brackets in your expression.
( Or be happy if your programming language has a Pow() function, so you do not encounter this issue.

)
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 7:51 pm
by Danilo
skywalk wrote:vb6 wrote:debug.Print 4^3^2
4096
-
^ Operator (Visual Basic)
Remarks
When more than one exponentiation is performed in a single expression, the ^ operator is evaluated as it is encountered from left to right.
It's documented behaviour.
Re: Pow '^' operator
Posted: Mon Apr 20, 2015 7:59 pm
by Little John
Strictly speaking, a programming language is free to define its own rules for this and that (including how it handles math operators).
However, I personally highly prefer if it is not in contrast to the well established general math rules.
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 4:32 am
by applePi
regarding the 4^3^2 we can trust also the old classic open source perl v5.xx in which (believe or not) wolfram mathematica have borrowed many of its tricks and algorithms for free and applied it to its expensive software. the classic perl <= v5 was written in c language
execute from perl : print 4**3**2 , and the result is 262144 ie 4**(3**2)
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 4:37 am
by skywalk
For floating point math functions, I use parentheses and may split the equation to avoid confusion.
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 10:30 am
by Trond
^ is right associative, and there is no doubt about it. Which is probably why it isn't implemented. PB doesn't have any right associative operators yet, so it would be a lot more work than implementing a new left associative.
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 11:24 am
by Dude
applePi wrote: i don't want to use converters/parsers which will complicate things
Hang on... I'm confused. You don't want to use them, but isn't that what "bc test.bas" is doing? It's a third-party converter?
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 11:28 am
by Trond
There's a large difference between evaluating a string at runtime and letting the PB compiler compiler PB source code. Two different uses.
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 11:34 am
by Dude
Isn't the idea to evaluate any given math string at runtime?
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 11:40 am
by Danilo
Dude wrote:Isn't the idea to evaluate any given math string at runtime?
No, applePi just wanted to convert non-PB-Syntax to PB-Syntax, especially convert ^ to PB's Pow().
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 12:03 pm
by Dude
Oh well, I misunderstood. So then a PureBasic pre-processor tool should do the job, so he could use ^ in his PureBasic sources and the pre-processor would convert them to Pow() at compile time. Still no need for a third-party tool to do it.

Re: Pow '^' operator
Posted: Tue Apr 21, 2015 7:33 pm
by Little John
applePi wrote:Thanks for the suggestions, i also have found a solution, is to use
http://sourceforge.net/projects/bcx-basic/ . and after the install, make a test.bas file contains as an example y = (0.4^2-(0.6-(x^2+y^2)^0.5)^2)^0.5 then save it to its Bin folder and from command prompt write bc test.bas , it will produce test.c which is
y=pow((pow(.4,2)-pow((.6-pow((pow(x,2)+pow(y,2)),.5)),2)),.5);
then we use it in purebasic after replacing .5 to 0.5
seems usable
I've got an alternative solution for you.
Some years ago, I wrote a recursive descent parser in PB for evaluating mathematical expressions.
Now I just changed it a little, so that it does not evaluate the expression, but replaces "a^b" with "Pow(a,b)"
(mathematically correct, of course). You can use this code e.g. for building an IDE tool or something.
Have fun!
//edit 2015-04-22:
Code slightly improved and moved to the
"Tricks 'n' Tips" section.
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 9:27 pm
by applePi
thats too generous Little John, this solves my problem for sure. and you have provided full examples of the usage including my problematic equation. it is handy and usable. i suggest fixing this solution in tips forum , since '^' are everywhere and all the literature and edu curriculum use '^'
in fact before i post the request here i was about to post in your thread about JSON about this subject, then i decided to post it here for a wide audience.
thank you
Re: Pow '^' operator
Posted: Tue Apr 21, 2015 10:51 pm
by juror
Yet another LJ gem.
How many more of these wonderful little jewels do you have hidden somewhere?
Many thanks
