## Why is 2-2 = 8.8817841970013e-016

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
ITISTHEBKID
Prole
Posts: 8
Joined: Sun Nov 11, 2018 9:45 pm

### Why is 2-2 = 8.8817841970013e-016

Here is my code:

Code: Select all

io.stdout:setvbuf("no")
bope(6,2)
end

function bope(xs,w)
for i=1,100,1 do
numb = (i/xs%1)*xs
if numb == 0 then
numb = xs
end
x = (w * numb)

print(x,w,x-w)
end
end
as you can see at the bottom, it prints x,w and then x-2. After the first iteration, every time x = 2, x-w is equal to a seemingly random number. w is always equal to 2, so why is my program telling me 2-2 = 8.8817841970013e-016?

zorg
Party member
Posts: 2936
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Why is 2-2 = 8.8817841970013e-016

The short answer is that those values are close enough to zero that they are effectively zero; note the e-016, meaning the number's about 0.00000000000000008 whatever- pretty close to zero.

Longer answer is that lua uses 64bit double-precision floating point numbers for its number values, and there are operations on floats that when done in succession will accumulate and propagate errors... the longer that goes on, the larger the error will be.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

ITISTHEBKID
Prole
Posts: 8
Joined: Sun Nov 11, 2018 9:45 pm

### Re: Why is 2-2 = 8.8817841970013e-016

Thanks, I understand now. Is it worth it to put something like

Code: Select all

if x < .01 then
x = 0
end
or will it not impact preformance

ivan
Party member
Posts: 1564
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Why is 2-2 = 8.8817841970013e-016

Also note that the precision will be incorrect for very large numbers too.
You should be fine as long as you don't compare numbers directly, like a == b
That's just how floating point math works.
I don't recommend it, but I've seen things like:

Code: Select all

math.epsilon = 0.001
function isEqual(a, b)
return math.abs(a - b) < math.epsilon
end
Like I said, if you are careful with your floating point numbers, you should be fine.

pgimeno
Party member
Posts: 2211
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Why is 2-2 = 8.8817841970013e-016

To display the actual numbers you're operating with, you can use string.format("%.17g", value). Lua's default format does not always distinguish different values, but that format string does.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Why is 2-2 = 8.8817841970013e-016

As long as you use exact integers for math operations, exact integers will come out - this is a special property of double precision floating point format. Doubles are 64 bit, but 11 bits is reserved for the exponent value, so your integers are limited to 53 bits. It can still hold very large values (in 9 quadrillion range) so it shouldn't be a limiting factor if you use integer math for practical item counting and such.

zorg
Party member
Posts: 2936
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Why is 2-2 = 8.8817841970013e-016

raidho36 wrote:
Thu Apr 25, 2019 11:25 am
As long as you use exact integers for math operations, exact integers will come out - this is a special property of double precision floating point format. (...) if you use integer math for practical item counting and such.
Addition, subtraction, multiplication, "integer division" and powers are the math ops specifically that will give back exact integer output for integer inputs, within the previously mentioned range, of course.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

### Who is online

Users browsing this forum: Bing [Bot] and 28 guests