Author Topic: associativity of the exponential operator  (Read 5516 times)

0 Members and 1 Guest are viewing this topic.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
associativity of the exponential operator
« on: January 21, 2022, 10:35:37 am »
QB64 and other Basics treat the ^ operator as left-associative while other programming languages that have an exponentiation operator treat it as right-associative, for example Python and FORTRAN
doing a web search is inconclusive some assert that there's no consensus  whether it should be left or right associative while other state that it's right-associative
Mathematica and Maxima treats it as right-associative while Maple gives the error message that the power operator is non-associative
from a mathematical perspective which is right? non-associative or right-associative?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #1 on: January 21, 2022, 11:04:26 am »
A^B <> B^A hence not associative

A^B^C I would say depends on how you () the pairs other wise it goes Left to Right
Code: QB64: [Select]
  1. Print 2 ^ 3 ^ 4
  2. Print (2 ^ 3) ^ 4
  3. Print 2 ^ (3 ^ 4)
  4.  
« Last Edit: January 21, 2022, 11:11:24 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: associativity of the exponential operator
« Reply #2 on: January 21, 2022, 11:17:09 am »
I was taught left to right, and in the case of A ^ B ^ C, it's the same thing as A ^ ( B * C).

2 ^ 3 ^ 4
8 ^ 4
4096

The same as:
2 ^ (3 * 4)
2 ^ 12
4096


Power Raised to a Power

According to this law, if ‘a’ is the base, then the power raised to the power of base ‘a’ gives the product of the powers raised to the base ‘a’, such as;

(a^m)^n = a^(m*n)
where a is a non-zero term and m and n are integers.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: associativity of the exponential operator
« Reply #3 on: January 21, 2022, 12:20:37 pm »
The idiot's working rule (I always follow this):

Always put brackets in.


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: associativity of the exponential operator
« Reply #4 on: January 21, 2022, 12:27:58 pm »
The idiot's working rule (I always follow this):

Always put brackets in.

And only idiots fail to follow this rule.  With brackets there's no room for disambiguaty or misinterpretation of the order of operations and math formula.  Anytime you're in doubt, manually order your math yourself with brackets and parentheses and save yourself the headache of learning which app takes precedence in which situation.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: associativity of the exponential operator
« Reply #5 on: January 21, 2022, 04:38:49 pm »
gentlemen no need to call names, the question I posted is logical and there's a reason, I was perusing the rosetta code and came to this https://rosettacode.org/wiki/Arbitrary-precision_integers_(included)
the question I posted said nothing about operator precedent ambiguity or the need to use parenthesis to eliminate them

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #6 on: January 21, 2022, 04:56:47 pm »
  [ You are not allowed to view this attachment ]  
Code: QB64: [Select]
  1. print 5^(4^(3^2))    
  2.  

>> INF

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: associativity of the exponential operator
« Reply #7 on: January 21, 2022, 05:31:10 pm »
bplus, the approximate value is 6.2060698786608744707483205572846793269 E183230
that's a bit above the range of double or _float :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #8 on: January 21, 2022, 06:26:31 pm »
Ah so we can expect that many digits in answer as exponent?

Oh has been working on the exact answer for about an hour, and that is the easy part of it!

Started over, that many digits isn't going to fit on one screen more like 45, yikes! better file it!
« Last Edit: January 21, 2022, 07:08:46 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #9 on: January 21, 2022, 11:29:14 pm »
@jack

Ok oh did it! Calculated that number in just under 4 hours (is Luke laughing?)

Here is code that cranked out the number and filed it:
Code: [Select]
'Dim a As Double    Big Power Play b+ 2022-01-21 calc 5^262144 and file result in: 5 tothe 4 tothe 3 tothe 2.txt
'a = 5 ^ (4 ^ (3 ^ 2))
'Print 4 ^ 9 ' 262144  so 5^262144 that's all
x = 1
[
x = mult[x,5]
i = a[i,1]
if eq[m[i,10000],0]
, i
fi
if gte[i,262144]
.
.
. x
save x;5 tothe 4 tothe 3 tothe 2.txt
exit
fi
]

And here is code that loaded the file and read the digits:
Code: [Select]
' Big Power Play Read 1st and last 20.txt b+ 2022-01-21
fstring = load[5 tothe 4 tothe 3 tothe 2.txt]
l = left[fstring,20]
r = right[fstring,20]
. l
. r

A snapshot of numbers compared to Rosetta's
  [ You are not allowed to view this attachment ]  

And if you need the file of numbers let me know. ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #10 on: January 22, 2022, 12:20:55 am »
Hey I made a function to do these giant numbers, too bad we can't get very far with them:

Code: QB64: [Select]
  1. _Title "x toThePower y toThePower z toThePower" 'b+ 2022-01-22
  2.  
  3. Print toThePower##("4, 3, 2") ' this might look familiar one number short of "5,4,3,2"
  4. Print toThePower##("3, 3, 3")
  5. Print toThePower##("2, 2, 2, 2")
  6. Print toThePower##("3, 2, 3, 2")
  7.  
  8. Function toThePower## (toThePowerString$)
  9.     ReDim a$(1 To 1), b As _Float
  10.     Split toThePowerString$, ",", a$()
  11.     b = Val(a$(UBound(a$)))
  12.     For i = UBound(a$) - 1 To 1 Step -1
  13.         b = Val(a$(i)) ^ b
  14.     Next
  15.     toThePower## = b
  16.  
  17. Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
  18.     Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
  19.     curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
  20.     dpos = InStr(curpos, SplitMeString, delim)
  21.     Do Until dpos = 0
  22.         loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
  23.         arrpos = arrpos + 1
  24.         If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
  25.         curpos = dpos + LD
  26.         dpos = InStr(curpos, SplitMeString, delim)
  27.     Loop
  28.     loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
  29.     ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
  30.  
  31.  

Offline tomxp411

  • Newbie
  • Posts: 28
    • View Profile
Re: associativity of the exponential operator
« Reply #11 on: January 22, 2022, 02:35:34 am »
The idiot's working rule (I always follow this):

Always put brackets in.

This is true. My entry exam at a software company I worked for actually had an order of operations problem on the test. It was something like
when IF A AND B OR C AND D gives unexpected results, what might be the cause? And the obvious answer is "the order of operations."

In the case of operators that are equal in rank, you're supposed to evaluate from left to right. So A^B^C should always evaluate A^B first, then n^C.

But after seeing the number of people who get confused by 5 + 6*0 - 3 on Facebook, I think even software engineers don't really understand the math they're implementing sometimes.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: associativity of the exponential operator
« Reply #12 on: January 22, 2022, 08:51:13 am »
bplus, your interpreter looks interesting, I had a crazy thought, could one calculate the last 20 digits without calculating all the digits?
something like this https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: associativity of the exponential operator
« Reply #13 on: January 22, 2022, 09:59:24 am »
That's not crazy that's classic slick idea for arbitrary calculations, do a chuck at a time.

But just doing the last chunk of the calculation is like going to a store paying for a dollar+ item with a $20 and only getting back the coin part of your change. :)

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: associativity of the exponential operator
« Reply #14 on: January 24, 2022, 04:15:22 am »
gentlemen no need to call names, the question I posted is logical and there's a reason, I was perusing the rosetta code and came to this https://rosettacode.org/wiki/Arbitrary-precision_integers_(included)
the question I posted said nothing about operator precedent ambiguity or the need to use parenthesis to eliminate them

@jack Just to clarify, neither I nor Steve was being insulting (by using the word 'idiot').  I used it self-deprecatingly and Steve referred to this humorously.  Your post is relevant and interesting (although until I read it, I didn't know what the title meant).