Page 1 of 3
operator ^ for power
Posted: Mon Sep 21, 2015 5:38 pm
by GPI
I was suprised, that purebasic miss one basic operator
Code: Select all
debug 2^5
debug 2*2*2*2*2
debug pow(2,5)
all should output 32. In case of 2^5 it shoud create a integer, because no float/double is evolved.
Code: Select all
Priority Level | Operators
---------------+---------------------
8 (high) | ~, -
7.5 | ^
7 | <<, >>, %, !
6 | |, &
5 | *, /
4 | +, -
3 | >, >=, <, <=, =, <>
2 | Not
1 (low) | And, Or, XOr
Re: operator ^ for power
Posted: Mon Sep 21, 2015 6:49 pm
by Little John
[u]Little John[/u] wrote:If such an operater will be implemented in PB, then it should work mathematically correct!
Unfortunately, I've often seen in expression evaluators that something like
4^3^2 is
evaluated from left to right, which is mathematically
wrong:
(4^3)^2 = 64^2 =
4096
Correctly, multiple power operators without any brackets are
evaluated from right to left:
4^3^2 = 4^(3^2) = 4^9 =
262144
[u]Trond[/u] wrote:^ is right associative, and there is no doubt about it.
Re: operator ^ for power
Posted: Mon Sep 21, 2015 6:51 pm
by GPI
thanks for the info

(I write a eval routine

)
Re: operator ^ for power
Posted: Mon Sep 21, 2015 6:58 pm
by Little John
You are welcome!
GPI wrote:(I write a eval routine

)
Here is some related code by me.

Re: operator ^ for power
Posted: Tue Sep 22, 2015 8:34 pm
by jwrjrjwrjr
+1 on the request and on the proper implementation!
Re: operator ^ for power
Posted: Tue Sep 22, 2015 9:17 pm
by GPI
maybe important:
-9^2 is equal to -(9^2)
(thanks to little john)
Re: operator ^ for power
Posted: Tue Sep 22, 2015 9:40 pm
by Little John
GPI, you are welcome!
For the skeptics.
Wolfram Alpha: -9^2
Re: operator ^ for power
Posted: Tue Sep 22, 2015 10:50 pm
by skywalk
Same with Windows Calculator:
-9^2 = 0 - 9^2 = -81.
Re: operator ^ for power
Posted: Tue Sep 22, 2015 10:54 pm
by GPI
Good example
http://www.wolframalpha.com/input/?i=3%2B-2
In your code this is illegal
Code: Select all
Check("3+-2", Eval::#Err_BadSyntax)
Re: operator ^ for power
Posted: Wed Sep 23, 2015 5:34 am
by Little John
GPI wrote:In your code this is illegal
Code: Select all
Check("3+-2", Eval::#Err_BadSyntax)
How strict the syntax checking is, is another story.
My parser does deliberately rather strict syntax checking.
With Wolfram Alpha, even this works:
http://www.wolframalpha.com/input/?i=nine+to+the+power+of+two
http://www.wolframalpha.com/input/?i=nine+to+power+of+two
http://www.wolframalpha.com/input/?i=nine+to+power+two
However, I think this is a bit offtopic here in the "Feature Requests" section.
Re: operator ^ for power
Posted: Wed Sep 23, 2015 8:17 am
by Lord
GPI wrote:maybe important:
-9^2 is equal to -(9^2)
(thanks to little john)
I don't think so.
You have to differentiate between
a minus sign as a indicator for a negative number -> (-9)
example:
http://www.wolframalpha.com/input/?i=(-9)^0.5
and
a minus sign as a calculation rule -> -(9)
example:
http://www.wolframalpha.com/input/?i=-+(9^0.5)
You have to set correct parenthesis in order to get a correct result.
Re: operator ^ for power
Posted: Wed Sep 23, 2015 8:28 am
by Little John
Lord wrote:You have to set correct parenthesis in order to get a correct result.
No.
You
can set parentheses, but in this case you
don't necessarily have to do so.
You only have to set parentheses here, if you want to calculate (-9)^2.
As GPI wrote, -9^2 "automatically" means the same as -(9^2).
Just like e.g. 3 + 4 * 5 means the same as 3 + (4 * 5).
Those are well established math rules, regardless whether you believe it or not.

Re: operator ^ for power
Posted: Wed Sep 23, 2015 2:01 pm
by Lord
Little John wrote:...regardless whether you believe it or not.

We are not in church here, arent we?
skywalk wrote:Same with Windows Calculator:
-9^2 = 0 - 9^2 = -81.
Just try these steps:
[-][9][=][x^y][0][,][5]
against
[-][9][x^y][0][,][5]
It shows that in the first example the calculator tried to get the root of a negative number (-9).
The second example interprets the minus sign as a calculation rule (subtract 9^0.5 from something, here from 0 as there is nothing before).
If we leave square roots and try to get a cubic root, both ways lead to valid results (for example -27 -> -3).
So dealing with odd roots there is no problem, but with even roots you have to make clear wether you want to subtract a result or get a root from negativ number.
Re: operator ^ for power
Posted: Wed Sep 23, 2015 6:43 pm
by Little John
Lord wrote:Little John wrote:...regardless whether you believe it or not.

We are not in church here, arent we?
No, we are not in church here. I'm glad that we seem to be in agreement about this.

Re: operator ^ for power
Posted: Wed Sep 23, 2015 7:47 pm
by GPI
@lord
I think, that -9^2 = (-9)^2 would make more sense, but it is -(9^2).
For example try to goolge for "-9^2".