illegal function call when evaluating this formula

Basic and Machine Language

Moderator: Moderators

Post Reply
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

illegal function call when evaluating this formula

Post by nippur72 »

For an unknown reason, the evaluation of this formula fails:

Code: Select all

10 PRINT (7-SQR(50))^(1/3)+(7+SQR(50))^(1/3)
RUN
? ILLEGAL FUNCTION CALL
 ERROR IN 10
READY.
I tried to split it into simpler terms, but it's the same. Tested in VIC 20 and in two different Z80 Microsoft Basic, the result is always the same.

Mike, are you reading this :wink: ?

(P.S. I was watching this youtube video)
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: illegal function call when evaluating this formula

Post by Mike »

nippur72 wrote:Mike, are you reading this? :wink:
Yes. :wink:
For an unknown reason, the evaluation of this formula fails:

Code: Select all

10 PRINT (7-SQR(50))^(1/3)+(7+SQR(50))^(1/3)
RUN
?ILLEGAL FUNCTION CALL
 ERROR IN 10
READY.
[...]
As you know, the exponentiation operator is quite complex. For arbitrary exponents, it is actually only well defined for positive numbers as bases. Only in case of integer exponents, and by convention(!), negative numbers as bases are also allowed - but the expression "(7-SQR(50))^(1/3)" has a base less than zero and a non-integer exponent (1/3). Which is the valid reason MS BASIC throws an error.

I have taken a short look into the YouTube video you pointed to. I'm sorry, but this guy is putting the car before the horse.

Exponentiation by "(1/3)" is, at a first look, equivalent to taking the cube root. However, in the complex plane, the equation "x³=a" has 3 solutions for x. The guy in the video now makes lots of funny non-equivalent formula transformations - the first of them already is "raise both sides to the power of 3" *) - but he never comes to the point that there actually exist complex numbers he would need to take into account!

So the question he poses ("what is the value of X?") is already ill-defined. Each of the roots has 3 different solutions (one of them being the so-called principal value) and the sum of the two roots has 3x3=9 different solutions. One of them is the one he 'calculates', but he loses all the other ones.

Now, again by convention, and when we want to stick with strictly real values, the cube root can be calculated as SGN(x)*(ABS(x)^(1/3)), and

Code: Select all

1 DEFFNCB(X)=SGN(X)*(ABS(X)^(1/3))
2 PRINTFNCB(7-SQR(50))+FNCB(7+SQR(50))
RUN
yields the "expected" result of 2.

But, as I wrote, there's more to the whole picture, which the guy in the YouTube video sadly misses out.



*) why is "raise both sides of an equation to a power" a non-equivalent transformation? As example, take the "equation" -1 = 1, which of course is false. Now squaring both sides (i.e. "raise both sides to the power of two") makes this (-1)^2 = 1^2, i.e. 1=1, which now is true. But an equivalent transformation must leave the truth value of an equation unchanged in all cases, which is violated by this simple counter example.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Re: illegal function call when evaluating this formula

Post by nippur72 »

Mike wrote: Wed Apr 29, 2020 2:51 pm but the expression "(7-SQR(50))^(1/3)" has a base less than zero and a non-integer exponent (1/3). Which is the valid reason MS BASIC throws an error.
ok, but now PRINT 7-SQR(50), it gives you -.071067813, go with the VIC20 cursor and turn that into PRINT -.071067813 ^ (1/3) ... it gives you the result without complaining! So the problem is not in the base < 0 and non integer exponent.
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: illegal function call when evaluating this formula

Post by wimoos »

Ha!

But put brackets around the result of 7-SQR(50) and the error is back again !
Maybe because exponentation is before negation...?

PRINT -.071 ^ (1/3) is interpreted as PRINT -EXP((1/3)*LOG(.071)) instead of PRINT EXP((1/3)*LOG(-.071))
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: illegal function call when evaluating this formula

Post by Mike »

nippur72 wrote:ok, but now PRINT 7-SQR(50), it gives you -.071067813, go with the VIC20 cursor and turn that into PRINT -.071067813 ^ (1/3) ... it gives you the result without complaining! So the problem is not in the base < 0 and non integer exponent. [...]
http://sleepingelephant.com/ipw-web/bul ... ilit=binds ... ;)
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Re: illegal function call when evaluating this formula

Post by nippur72 »

haha I'm stumbling upon the same mistake over and over :oops: :oops: :oops:
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: illegal function call when evaluating this formula

Post by Mike »

TBH, it really depends on the programming language or programmable calculator you are using, whether the sign operator or power operator binds more tightly.

If you put negative number literals into parentheses, you are always on the safe side regarding this matter. In German speaking countries, where a dot in the middle of a line denotes multiplication (as in 2·3 = 6), writing the "morse sign" ·- is especially dangerous, as it could easily be mistaken for just being a minus sign. Therefore, it is good practice here to write, for example, x·(-4) instead of x·-4 (the latter, as noted, could be confused with x-4).

So, needless to say, I have seen sensitised regarding that matter. If you happen to stumble across this just every 10 years, that's probably a tolerable error rate. :mrgreen:


Regarding the original topic, details of what is happening here are discussed in complex analysis, especially regarding 'multi-valued' functions (and their so-called sheets). 'Principal value', 'branch point' and 'branch cut' are related terms you should investigate. :)

Plotting all roots (i.e. all 9 solutions) of the equation makes up for nice diagram, BTW:

Image

The horizontal axis denotes the real part of X i.e. Re(X), and the vertical axis gives the imaginary part of X, Im(X).

X=2 is found in the diagram, as well as the other solution returned by Wolfram Alpha when it evaluates the principal value.
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: illegal function call when evaluating this formula

Post by wimoos »

Mike wrote: Thu Apr 30, 2020 4:36 pm ....In German speaking countries,..
Ahem... :|
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: illegal function call when evaluating this formula

Post by Mike »

wimoos wrote:Ahem... :|
That does not exclude other countries, if you have any problems with my explanation above.

How is the rule "Punktrechnung geht vor Strichrechnung" put in Dutch?
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: illegal function call when evaluating this formula

Post by wimoos »

Recently I saw an interesting video about this subject (negative base, fractional power) which made me think again about this forum entry:
(-8)^(-2/3) which was calculated to be 1/4.

The interesting part of this is that they halved the power and squared the base (leading to a positive base), so: 64^(-1/3). Which is the same as (+8)^(-2/3).

Which leads to think in these cases you could always leave the minus sign out. You can always halve the power and square the base.
Why is this not done as a rule ?

Edit: found the answer here: https://math.stackexchange.com/question ... nal-powers
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Post Reply