This equation is getting the wrong answers from Roblox Lua, why?

print((-200 * 2.55) + (255 * 2))

If you run this in studio it gives 5.6843418860808e-14 as the answer, what?
Is this a bug or is there something really simple here that I am missing?

The correct answer should be 0, no?

1 Like

the correct answer is 0
this is odd

According to the calculater the answer is

local first = -200 * 2.55
local second = 255 * 2

print(first) --> -510
print(second) --> 510
print(first + second) --> 5.6843418860808e-14 

Yeah something weirds going on, some lua engine expert will probably come on explain what’s happening.

print((-200 * 2.55) + (255 * 2))
print(-200 * 2.55 == -510, -200 * 2.55, 255 * 2)
print(-510 + 510)

OUTPUT

5.6843418860808e-14
false        -510.0        510
0

why is this happening…

EDIT:
tested this on the lua demo page

1 Like

Ye, this been bugging me for awhile now, I posted here first in case there’s a specific reason why lua returns that

Either we’re all missing something really simple here or we’ll find out that this is a studio bug :sweat_smile:

It’s likely just a floating point error. Realize that 5.6843418860808e-14 is equal to 0.000000000000056843418860808. It is odd to see a floating point error with those small numbers though :woman_shrugging:.

1 Like

could it be a floating point error?
usually the decimals are smaller then that, but idk

1 Like

put the calculated number in an int value then print the int value value.

This is because the number is actually a negative number, it would be the same as doing 5.68 x 10^-14 (scientific notation).

The real number would be 0.000000000000056843.

1 Like

Today color3s were also facing this issue. When I tried changing the atmosphere color through a script I selected a color3 from the script itself but when I played everything was white and the atmosphere was set to 0,0,0

So to remove the stupid annoying decimal numbers, put it in an int value and print the intvalue’s value .

1 Like

the answer is 0. how that was really odd

1 Like

Okay, this is the last time I’m gonna say this, intvalue’s remove decimals so you can use them

1 Like

you could just use math.round instead of that

1 Like

Up to you, I’m only explaining why the equation is what it is.

1 Like

From the looks of it, its just a floating point error, normally multiplication and decimal based numbers cause this.
Its really:
0.000000000000056843418860808

But yeah pretty common in the sweet sweet 32 bit numbering system Lua has.

3 Likes

Decimal Rounding Errors. Roblox uses floats which can hold values from

1.7 Ă— 10^308 to 1.7 Ă— 10^308

Basically when calculating your result, the computer could not hold the decimal perfectly in binary(0101010)

Your number you got could be rounded to get your true result of zero but the result you got it really close to zero off by not much.

1 Like

0.000000000000056843418860808 was the result from the math, and while technically this is not true, it’s a major problem faced by all computer science people. This result is so close to 0.

1 Like