Issue with a calculation in Lua

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue with a calculation in Lua

Jasper Klein
Hi,

There is an issue with a calulation I did with Lua 5.3.5 and the latest  
5.4 beta.

The result of the two lines below is a '-nan'
> b = -1.0702700000075937e-05
> print( b ^( 1 / 2.19921875 ) )

While this line prints the number '-0.005493842385624'
> print( -1.0702700000075937e-05 ^ ( 1 / 2.19921875 ) )

-- Jasper

Reply | Threaded
Open this post in threaded view
|

Re: Issue with a calculation in Lua

Gabriel Bertilson
^ is higher in operator precedence than unary -. So for instance -2^2
is equivalent to -(2^2), whereas b^2, where b is equal to -2, is
equivalent to (-2)^2. They evaluate to -4.0 and 4.0 respectively. Your
examples are similar; they should evaluate to different values.

— Gabriel

On Sun, Oct 27, 2019 at 12:44 PM Jasper Klein <[hidden email]> wrote:

>
> Hi,
>
> There is an issue with a calulation I did with Lua 5.3.5 and the latest
> 5.4 beta.
>
> The result of the two lines below is a '-nan'
> > b = -1.0702700000075937e-05
> > print( b ^( 1 / 2.19921875 ) )
>
> While this line prints the number '-0.005493842385624'
> > print( -1.0702700000075937e-05 ^ ( 1 / 2.19921875 ) )
>
> -- Jasper
>

Reply | Threaded
Open this post in threaded view
|

Re: Issue with a calculation in Lua

Jasper Klein
You are right.

print( ( -1.0702700000075937e-05 ) ^( 1 / 2.19921875 ) ) retults too a  
-nan.
If I understand this right the result is a -nan because it would be a  
complex number.

-- Jasper


Op Sun, 27 Oct 2019 19:15:35 +0100 schreef Gabriel Bertilson  
<[hidden email]>:

> ^ is higher in operator precedence than unary -. So for instance -2^2
> is equivalent to -(2^2), whereas b^2, where b is equal to -2, is
> equivalent to (-2)^2. They evaluate to -4.0 and 4.0 respectively. Your
> examples are similar; they should evaluate to different values.
>
> — Gabriel
>
> On Sun, Oct 27, 2019 at 12:44 PM Jasper Klein <[hidden email]> wrote:
>>
>> Hi,
>>
>> There is an issue with a calulation I did with Lua 5.3.5 and the latest
>> 5.4 beta.
>>
>> The result of the two lines below is a '-nan'
>> > b = -1.0702700000075937e-05
>> > print( b ^( 1 / 2.19921875 ) )
>>
>> While this line prints the number '-0.005493842385624'
>> > print( -1.0702700000075937e-05 ^ ( 1 / 2.19921875 ) )
>>
>> -- Jasper
>>

Reply | Threaded
Open this post in threaded view
|

Re: Issue with a calculation in Lua

Francisco Olarte
Jasper:

On Sun, Oct 27, 2019 at 7:29 PM Jasper Klein <[hidden email]> wrote:
> print( ( -1.0702700000075937e-05 ) ^( 1 / 2.19921875 ) ) retults too a
> If I understand this right the result is a -nan because it would be a
> complex number.

It generally is, IIRC if X=1.07...e-5, Y=1/2.199..., and j=sqrt(-1)
you can write that as (X*e^(j*pi))^Y = X^Y* e^(j*pi*y), which gives
you phase and amplitude ( oops, EE deviant, too many modem code
written lately, I think it's called modulus and argument usually ).

Also, IIRC lua docs state somewhere that exponentiation is done by
pow. man 3 pow fives you a nice break down of special things which can
happen, the first of which is, in my system " If x is a finite value
less than 0, and y is a finite noninteger, a domain error occurs, and
a NaN is returned."

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Issue with a calculation in Lua

Philippe Verdy
In reply to this post by Jasper Klein
No it would be two possible complex numbers for square roots, three for cubic roots, and an infinite number of roots for non integer real powers, here you could expect millions possible values because your power is in fact a rational with many digits).
Even within complex numbers, such exponent is not defined if there's no additional parameter to select the rank of the argument.
So it's logical that it returns an NAN value (the sign given to the NAN is in fact fake, it just keeps the sign of the radicand, i.e. the 1st parameter of the power; the sign of the powerand does not play any role in the sign of any valid complex root that could be returned)
Powers are defined and are returning a single root only for positive radicands, without needing any additional parameter as its argument is null and remains null at any rational power.


Le dim. 27 oct. 2019 à 19:29, Jasper Klein <[hidden email]> a écrit :
You are right.

print( ( -1.0702700000075937e-05 ) ^( 1 / 2.19921875 ) ) retults too a 
-nan.
If I understand this right the result is a -nan because it would be a 
complex number.

-- Jasper


Op Sun, 27 Oct 2019 19:15:35 +0100 schreef Gabriel Bertilson 
<[hidden email]>:

> ^ is higher in operator precedence than unary -. So for instance -2^2
> is equivalent to -(2^2), whereas b^2, where b is equal to -2, is
> equivalent to (-2)^2. They evaluate to -4.0 and 4.0 respectively. Your
> examples are similar; they should evaluate to different values.
>
> — Gabriel
>
> On Sun, Oct 27, 2019 at 12:44 PM Jasper Klein <[hidden email]> wrote:
>>
>> Hi,
>>
>> There is an issue with a calulation I did with Lua 5.3.5 and the latest
>> 5.4 beta.
>>
>> The result of the two lines below is a '-nan'
>> > b = -1.0702700000075937e-05
>> > print( b ^( 1 / 2.19921875 ) )
>>
>> While this line prints the number '-0.005493842385624'
>> > print( -1.0702700000075937e-05 ^ ( 1 / 2.19921875 ) )
>>
>> -- Jasper
>>