Math.pow or "^" operator returns "nan" if the Number value is stored in a variable, but not otherwise?

If I run the following

print(-0.53220192342997 ^ 1.45)

the output is:

  -0.40069215323119

However, if I run the following:

local n = -0.53220192342997
print(n ^ 1.45)

the output is:

nan

Does anybody have any ideas as to why this is happening? It’s pretty much game-breaking for me right now.

2 Likes

Can you wrap both operands in the attempt without the variable in parenthesis and tell me if the output changes? Not at my computer right now. Betting it has something to do with operator precedence. Your first case shouldn’t even work because you’re taking the root of a negative number.

Taking the root? Isn’t ^ for exponents? What does the root have to do with exponents?

1 Like

Fractional exponents can also be expressed using a root.

x^(3/2) == x^1.5 == sqrt(x^3)

Since you have a decimal number, a simplified case like above is easier to understand.

When x is negative, an odd power of it is also negative. Taking the even root of that then doesn’t work.

Yes, it looks like applying the power when that number isn’t negative and then negating the result produces what you’re seeing. For some reason the subtraction operator has a lower precedence than the exponentiation operator.

2 Likes

think you’re right, looks the following produces NaN

print((-0.53220192342997) ^ 1.45)

also found this explanation

also x^(3/2) == x^1.5 == sqrt(x)(x^3) <–clarification about taking square root of negative number

2 Likes

I guess the real question, then, is: since the first example works, how can I alter the second example in order to make it work as well?

Basically what is happening is that the computer thinks you mean

print(-1 * (0.53220192342997 ^ 1.45))

This will give you an answer because you are taking the root of a positive number, all of your calculations are positive, and then you are making it negative at the end.

Whereas when you store -0.53220192342997 as a variable, it leaves no ambiguity, and therefore you are trying to take the root of a negative number.

print((-0.53220192342997) ^ 1.45)

The way to make it work with the variable would just be to imitate what is happening in the first case, ie:

  1. Do all of your calculations with positive numbers

  2. Multiply by -1 after the calculation has been completed

    local num = 0.53220192342997
    local pow = 1.45
    local ans = num ^ pow
    ans = ans * -1
    print(ans)

or, in one line:

print(-1 * (0.53220192342997 ^ 1.45))
6 Likes

You probably need to preserve the original sign, not just multiply by -1 in all cases.

It looks like we have math.sign().

3 Likes

HAHA
That’s hilarious

Code:

warn((-123) ^ 4 == -123 ^ 4)

Output:
22:04:25.318 - false

This would make for a good trick question / riddle

Whenever I enter a negative number I receive nan as the answer. Does it need to be stored as a variable?

Raising a negative number to a power that’s not a whole number is likely to produce NaN, which is explained above. Storing it in a variable will not change that.

The problem in this thread was that - is evaluated after ^, resulting it OPs math to appear to work. The OP needs to do this operation on math.abs of the number, then multiply by the number’s math.sign to avoid the NaN issue.

1 Like