Why a negative number to the power of 2 equals negative?

What do you mean by ‘error’?

1.2 is not an even number.

When the power is even, the minus would be gone [ turned into plus]

why does it have to be a even number?

When the power is even, the minus would be gone [ turned into plus]

I see, you get a nan error. Hold on.

Okay, I printed these:

print(-5 ^ 1.2) -- works
print(5 ^ 1.2) -- works
print((-5) ^ 1.2) -- errors

When i say error i mean it returns nan (not a number).
so when i do

print((-5) ^ 1.2)
=nan

and then i do

print((5) ^ 1.2)
=6.89864830730607

how come the first one doesnt return a number?

I dont see any point having those parenthesis in this case.
Since the power is positive and not even, the partnethesis don’t matter.

print(-5 ^ 1.2) -- works
print(5 ^ 1.2) -- works
print((-5) ^2) -- works
print((-5) ^-2) -- works

The partenthesis only matter when you have either a long expression with multiple operations, or if the power is even and you want negative to become positive.

Also, have you tried to use math.pow too?

but u know how if you do

print(-5 ^ 1.2)

the answer is negative, but what if you wanted to add the paranthesis so the answer is positive (hence a negative times a negative is positive), i tried this on the google calculator and i dont know why it doesn’t work

print((-5) ^ 1.2)

Because the parenthesis dont really do anything here, they dont affect it, and I think this is why the issue occurrs.
But as I’ve said before,

Since 1.2 is not even, raising both 5 and -5 to that power, would result in opposite results.

opposite results? I assume you mean a negative to positive and a positive to negative.
So when I do

-5 ^ 1.2
=-6.898648307306074

it doesnt print a positive, same when doing it to the power of positive 5.

When I print 5 ^ 1.2 , I do get a positive result.

And what I meant is:

5^1.2 = 6.898

-5^1.2 = -6.898

Because complex numbers is not in the domaim of Luau VM number data type so it’ll return NaN instead.

2 Likes

But it doesn’t require complex numbers? The odd roots of a negative number are real, so I assume this just a weird quirk of exponents using floating points.

Edit: see below why I was wrong

There is no quirk because it is not an odd root. (-5)^1.2 = (-5)^(12/10) which as you can see involves the even root of a negative number, which requires complex numbers.

I put in the equation to desmos and it spit back a real answer, and if you try (-5)^0.2 in Roblox, which should be the 5th root of -5, it still spits out nan.

1 Like

can you please explain simply what this means, im having a hard time comprehending this.

I stand corrected then.

Either way the §9.2.1 of IEEE 754:2008 states that

pow (x, y) signals the invalid operation exception for finite x < 0 and finite non-integer y.

(in most cases, “signals the invalid operation excpetion” basically means that it returns NaN)

2 Likes

Ah, you are right. I went in the wrong direction:

image

I’m not sure if the image formatted correctly since I’m on mobile, but the bottom expression is undefined (due to order of operations) despite both being algebraically equivalent to (-5)^(12/10). Interesting to note how google defaults to a complex solution if you search up (-5)^1.2, but gives you a real solution if you do ((-5)^12)^(1/10), so it must be some internal floating point stuff.

image

Lua(u) does not support imaginary numbers.

Coincidentally enough, one of PIL’s examples is a primitive implementation for complex numbers.
https://www.lua.org/pil/15.1.html

A square root is an even root, and you can’t find the square root of -1 using real numbers since 1^2 is 1 and (-1)^2 is also 1. However, your solution to (-5)^1.2 depends on how you approach the problem, since there is both a real and complex solution.

1 Like