Compiler can't subtract numbers properly?

I’m trying to subtract by 0.1 when it reaches 0 it outputs some crazy number then it goes to -0.01. I’m kinda confused.

image

			if cameraOffsetOrient > e or leaning == nil then
				if leaning == nil and e ~= 0 then
					e -= 0.1
				else
					e += 0.1
				end
			end
1 Like

This is known as a floating-point error, it happens in all programming languages that use floats to store number values.

Heres a link that explains it pretty well, its written for Python but the same applies to most programming languages (including Lua, just pretend its saying “Lua” instead of “Python”)

Edit: You also shouldn’t worry about this, it’s likely never going to be an issue as the error is extremely small.

A little more info:

-1.5e-15 is scientific notation - like -1.5 x 10^-15 or -0.0000000000000015.

You shouldn’t compare numbers with == unless you swear they’re always integers.

Use < and > instead. For instance, use the difference to check if two numbers are close:

if math.abs(x - y) < .001 then
    -- they’re close
end