0.1 + 2.8 ~= 1.1 + 1.8

If you type print(1.1 + 1.7) into your script, the result is 2.8
If you type print(1.1 + 1.8) into your script, the result is 2.9000000000000004
If you type print(1.1 + 2.8) into your script, the result is 3.9
You can test it on the Roblox command line, and the results are as inconsistent as the above, which I think can be very confusing for developers,I found it when I adjusted my game’s numerical parameters

I think the print function should be uniform in its output, either by keeping the number of decimal places long or by simplifying the number of decimal places, not by uncertain number of decimal places

this issue isnt unique to lua. its a product of floating point arithmetic in code. you can use string.format to basically cut off the digits to a specified amount

4 Likes

You’re right, but I hope the print function does that for us

that would actually be a bad thing because then the actual value wont be shown. that tiny amount at the end is. being considered by the code and can build up. if print cut it off we wouldnt know where this extra value is coming from. print should show exactly what the value is

3 Likes

1.1 + 1.8 = 2.9000000000000004 But that’s not accurate from a human point of view. Almost any game development doesn’t needs so many zeros, am I right?

1 Like

You can see a very funny result below :rofl:

This is not a matter of human-readable format, or humor. The reason you are getting this is because of floating point precision.

This is occurring in all programming languages that use floating point numbers.

Read more here: Precision (computer science) - Wikipedia

Also, there are more posts about this topic. Please use the search bar before posting.

2 Likes

I can see where you’re coming from, but its something you have to deal with as a developer.

What happens here is the floating-point error. Super interesting subject in mathematics and computer science. Long story short computers cant store the exact value of some decimal numbers.

As someone stated already you’ll have to use .format when working with float values.

You’re right, but floating-point errors are bound to happen. For example, running 0.1 + 0.2 in languages like Python or JavaScript, would return:

image

1 Like

Unfortunately, these anomalies are ‘normal’ when it comes to floating-point and you will have to live with them.
You can use my ‘Round’ function described in this topic, to avoid this problem.

1 Like

Floating point errors were mentioned in a fairly recent Announcement about how Roblox was handling numbers.
I can’t remember which post it was, but maybe try searching Floating point error and search in the Announcements forum.

3 Likes

Also, when comparing float values with the equal operation you’ll have to use a small error of margin in your code. Example:

local A = 0.1 + 0.2
local B = 0.3
local err = 1e-6

print(math.abs(A - B) <= err)
1 Like

Since nobody ever mentions it, this is why it happens. In our number system, base 10, what is the answer to 1/3?
The answer is 0.3333333333 repeating infinitely. We don’t have infinite space on the paper, so let’s round it off at 5 decimal places.
0.33333
Multiply by 3 again and you get 0.99999. it’s not 1.
This is a similar rounding error that exists in our every day base 10 number system.
What’s more is that 1/3*2 and 2/3 are different numbers.
(1/3 = 0.33333) * 2 = 0.66666
2/3 = 0.66667

In binary or base 2, this type of error caused by numbers repeating infinitely is a lot more common. Like tape measures in Inches, binary numbers work best when dividing by 2. Halves, quarters, eighths, sixteenths, thirty-seconds, sixty-fourths, etc. If your number can’t be represented with these inverse powers of two, it must get rounded.

5 Likes

I think that’s the simplest way to do it, thank you, but I hope Roblox staff can handle it for us. We can just focus on game development and not focus on the details

so mathematically on a base 10 number system youre right. Computers use binary. This is the product of that. That value is what value the computer is using. It shouldnt matter what a human thinks it should be, what matters is what the value ACTUALLY is for the machine.

1 Like
function Round(Number, Digits)

local Num = Number * 10 ^ Digits

Num = Num>=0 and math.floor(Num+0.5) or math.ceil(Num-0.5)

Num = Num / (10 ^ Digits)

return Num

end

This method comes from @ rogeriodec_games, Should be built into Roblox as a friendly game engine

It is, math.round exists in Luau. You just need to handle the digit rounding:

local a = 0.13285358942
a = math.round(a * 1000) / 1000 --> 0.133
3 Likes

I really don’t think you understand how the floating point error happens and why it happens as a byproduct. Do some research on it and you will understand why they can’t just “fix” it.