operator ^ for power

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

operator ^ for power

Post 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
Last edited by GPI on Tue Sep 22, 2015 9:22 pm, edited 2 times in total.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: operator ^ for power

Post 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.
Last edited by Little John on Mon Sep 21, 2015 6:51 pm, edited 1 time in total.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: operator ^ for power

Post by GPI »

thanks for the info :)
(I write a eval routine :) )
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: operator ^ for power

Post by Little John »

You are welcome!
GPI wrote:(I write a eval routine :) )
Here is some related code by me. :-)
jwrjrjwrjr
User
User
Posts: 14
Joined: Sat May 23, 2015 3:12 am
Location: The other Georgia

Re: operator ^ for power

Post by jwrjrjwrjr »

+1 on the request and on the proper implementation!
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: operator ^ for power

Post by GPI »

maybe important:
-9^2 is equal to -(9^2)

(thanks to little john)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: operator ^ for power

Post by Little John »

GPI, you are welcome!

For the skeptics. :-)
Wolfram Alpha: -9^2
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: operator ^ for power

Post by skywalk »

Same with Windows Calculator:
-9^2 = 0 - 9^2 = -81.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: operator ^ for power

Post by GPI »

Little John wrote:GPI, you are welcome!

For the skeptics. :-)
Wolfram Alpha: -9^2
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)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: operator ^ for power

Post 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.
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: operator ^ for power

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

Re: operator ^ for power

Post 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. ;-)
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: operator ^ for power

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

Re: operator ^ for power

Post 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. ;-)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: operator ^ for power

Post 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".
Post Reply