Pow '^' operator

Just starting out? Need help? Post your questions and find answers here.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Pow '^' operator

Post 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))
Keep it BASIC.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Pow '^' operator

Post 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. :D )
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Pow '^' operator

Post 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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Pow '^' operator

Post 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.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Pow '^' operator

Post 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)
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pow '^' operator

Post by skywalk »

For floating point math functions, I use parentheses and may split the equation to avoid confusion.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Pow '^' operator

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Pow '^' operator

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post by Dude »

Isn't the idea to evaluate any given math string at runtime?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Pow '^' operator

Post 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().
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Pow '^' operator

Post 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. ;)
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Pow '^' operator

Post 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.
Last edited by Little John on Wed Apr 22, 2015 10:45 pm, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Pow '^' operator

Post 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
juror
Enthusiast
Enthusiast
Posts: 228
Joined: Mon Jul 09, 2007 4:47 pm
Location: Courthouse

Re: Pow '^' operator

Post by juror »

Yet another LJ gem.

How many more of these wonderful little jewels do you have hidden somewhere?

Many thanks :)
Post Reply