1 not equal to 1?

I have a table of decimal values that I’m looping though and adding to a variable called total. If the values don’t add up to be 1 then I show a warning message. The message says it equals 1 though so I’m at a loss.

Here is what it looks like when I loop though the table, add the value to the total variable and then print it 

sceneBonus(261): 0.1 sceneBonus(261): 0.2 sceneBonus(261): 0.3 sceneBonus(261): 0.39 sceneBonus(261): 0.44 sceneBonus(261): 0.51 sceneBonus(261): 0.6 sceneBonus(261): 0.65 sceneBonus(261): 0.73 sceneBonus(261): 0.83 sceneBonus(261): 0.91 sceneBonus(261): 1

Here is the check I’m doing it 

if(total ~= "1" and total ~= 1) then print("The odds don't add up to 1 it equals: " .. total) end

No matter what I try this is what gets printed 
 

The odds don't add up to 1 it equals: 1

It has to be something so simple that I’m not seeing anyone have any ideas?

So I just did a math.floor(total) and it equals 0…wtf

if total ~= 1 then print("The odds don't add up to 1 it equals: " .. total) end

Yeah I had that originally but it wasn’t working.

I figured it out though, if you add decimals in lua it’s not percise so all the numbers == 1 technically but lua had the number at .999999999999 

I ended up multiplying each number in the loop by 100 and then I divided the total by 100 to see if that was == 1 and it worked. I guess when adding decimals .2 maybe actually be .20000000000000001 

The reason for your issue is probably that Lua uses a float/double as datatype which can’t represent all numbers because of a limited precission.

In my experience, most of the time, the comparision is done by using an epsilon value as a range within you consider your number to be the same.

[lua]

if math.abs(total - 1.0) < 0.000001 then

end

[/lua]

Full integer numbers are less problematic as they can be represented up to a certain number without error so if your sample numbers are the range your usually using, scaling them up to integers (*100) is a decent solution too. You might compare the result with 100 instead of scaling them down again as this may still cause errors if, at some stage if you’re comparing to totals other than 1.

Here’s an article explaining the situation better than any forum answer would ever be able to do, so might be an interesting read.

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Personally, I avoid floating point numbers where possible if accuracy is important.  Do what most financial systems do and only store integers.  e.g. $0.99 is stored as 99, $10.99 is stored as 1099, etc

But notice that integeres are also stored as doubles in LUA. If the number gets to long (big) the problem will accure too.

Yeah but integers can be represented up to around 2^50 (52 or 53 iirc) which is a huge number and usually much much more than most will ever need in any kind of code/calculation.

That said, with Lua 5.3, there’s native support for integers in the Language, so there’s probably a small chance this will, at some point in the future, be included in Corona as well.

yep, 2^53.  (re IEE 754 double representation of “integer”)

it’s the first integer value that can’t be incremented by 1, or worded in reverse:  2^53-1 is the greatest integer value that CAN be incremented by 1.

print(2^53 == 2^53+1) -- true&nbsp;(did not increment)

.

This explains iT Well: https://youtu.be/PZRI1IfStY0

if total ~= 1 then print("The odds don't add up to 1 it equals: " .. total) end

Yeah I had that originally but it wasn’t working.

I figured it out though, if you add decimals in lua it’s not percise so all the numbers == 1 technically but lua had the number at .999999999999 

I ended up multiplying each number in the loop by 100 and then I divided the total by 100 to see if that was == 1 and it worked. I guess when adding decimals .2 maybe actually be .20000000000000001 

The reason for your issue is probably that Lua uses a float/double as datatype which can’t represent all numbers because of a limited precission.

In my experience, most of the time, the comparision is done by using an epsilon value as a range within you consider your number to be the same.

[lua]

if math.abs(total - 1.0) < 0.000001 then

end

[/lua]

Full integer numbers are less problematic as they can be represented up to a certain number without error so if your sample numbers are the range your usually using, scaling them up to integers (*100) is a decent solution too. You might compare the result with 100 instead of scaling them down again as this may still cause errors if, at some stage if you’re comparing to totals other than 1.

Here’s an article explaining the situation better than any forum answer would ever be able to do, so might be an interesting read.

https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Personally, I avoid floating point numbers where possible if accuracy is important.  Do what most financial systems do and only store integers.  e.g. $0.99 is stored as 99, $10.99 is stored as 1099, etc

But notice that integeres are also stored as doubles in LUA. If the number gets to long (big) the problem will accure too.

Yeah but integers can be represented up to around 2^50 (52 or 53 iirc) which is a huge number and usually much much more than most will ever need in any kind of code/calculation.

That said, with Lua 5.3, there’s native support for integers in the Language, so there’s probably a small chance this will, at some point in the future, be included in Corona as well.

yep, 2^53.  (re IEE 754 double representation of “integer”)

it’s the first integer value that can’t be incremented by 1, or worded in reverse:  2^53-1 is the greatest integer value that CAN be incremented by 1.

print(2^53 == 2^53+1) -- true&nbsp;(did not increment)

.

This explains iT Well: https://youtu.be/PZRI1IfStY0