Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] Lua/pil/ makes a pretty boastful statement about Lua's real number type, is it true?

There is a widespread misconception about floating-point arithmetic errors and some people fear that even a simple increment can go weird with floating-point numbers. The fact is that, when you use a double to represent an integer, there is no rounding error at all (unless the number is greater than 100,000,000,000,000). Specifically, a Lua number can represent any long integer without rounding problems. Moreover, most modern CPUs do floating-point arithmetic as fast as (or even faster than) integer arithmetic.

Tldr: Lua can handle big numbers without floating-point errors.

However, in Roblox, I get these errors pretty often. Is there some kind of disconnect mutating the numbers in Roblox (For example, NumberValue.Value or Integer.Value properties)?

Is Lua's manual just wrong here? I'm just interested in the validity, I'm not sure I've ever heard anyone say anything about it or backed this statement up.

(Definitely have heard people warning about floating-point errors, of course.)

Also, does this apply to decimals and fractions as well?

Source: https://www.lua.org/pil/2.3.html

1 comment

0

wut

1 answer

Ah, I misinterpreted it a bit.

It is indeed true that Lua can handle large numbers, but only integers is what it meant.

There is a widespread misconception about floating-point arithmetic errors and some people fear that even a simple increment can go weird with floating-point numbers. The fact is that, when you use a double to represent an integer, there is no rounding error at all (unless the number is greater than 100,000,000,000,000)

Emphasis on the quote: "The fact is that when you use a double to represent an integer..."

So, yes, Lua indeed can handle large integers without floating-point errors. However, it cannot get very far with decimals. Because Lua uses doubles to represent all real numbers, there are no rounding errors or floating-point errors with large integers, unlike a lot of other languages. Furthermore, it appears as though Roblox does mutate integers a bit when it comes to floating-point errors, as they do happen with NumberValue objects, IntValues, and so forth, but it does not happen in the actual Lua VM. I'm unsure of why it happens, but it's probably reasonable to blame the engine that Roblox uses.

(Source) https://www.lua.org/pil/2.3.html

This discussion is preserved as it was. New answers, comments, and votes are closed.