Basic Value compare question...

I am trying to compare 2 values and they appear to be equal but are still being evaluated as different.

What am i doing wrong? Any ideas? I added tonumber() just to be sure I wasn’t converting one to a string somewhere.

--Check to see if the current health and the target health differ if tonumber( characterStatus.current[statusColor .. "Health"] ) ~= tonumber( characterStatus.target[statusColor .. "Health"] ) then --Current and Target Heath amounts differ if statusColor == "monster" then print( "\nMonster Amounts Differ ~~~~~~~~~~ =" .. characterStatus.current[statusColor .. "Health"] .. characterStatus.target[statusColor .. "Health"] .. "=" ) end end  

The output is “Monster Amounts Differ   ~~~~~~~~~~ =99=”

First thing I’d check is what values are actually being compared, by just print()-ing them. :slight_smile:

Brent

So… printing

print(characterStatus.current[statusColor … “Health”] )  ----------- 9

print(characterStatus.current[statusColor … “Health”] % 1) --------- 7.1054253546001e-015

How is this happening? I use math.ceil on it in places where I am doing calculations on it and don’t want any decimals in there. Is that related to the issue?

9 % 1 should have produced 0.  However, Lua doesn’t have integers, it has double floats.  And there is some internal rounding to make things look like integers.  Your calculation produced  0.000000000000071054 which was probably big enough for print to decided to not round it off.  There was a big forum thread about this about a month ago.  There is some good reading in there:

http://forums.coronalabs.com/topic/41508-mathfloor-issue/#entry215823

In particular the next to last post.  I’m not sure how your 9 got to be a 9, but there could be floating rounding errors getting in there somewhere.  math.floor might be a better choice than math.ceil.

Rob

First thing I’d check is what values are actually being compared, by just print()-ing them. :slight_smile:

Brent

So… printing

print(characterStatus.current[statusColor … “Health”] )  ----------- 9

print(characterStatus.current[statusColor … “Health”] % 1) --------- 7.1054253546001e-015

How is this happening? I use math.ceil on it in places where I am doing calculations on it and don’t want any decimals in there. Is that related to the issue?

9 % 1 should have produced 0.  However, Lua doesn’t have integers, it has double floats.  And there is some internal rounding to make things look like integers.  Your calculation produced  0.000000000000071054 which was probably big enough for print to decided to not round it off.  There was a big forum thread about this about a month ago.  There is some good reading in there:

http://forums.coronalabs.com/topic/41508-mathfloor-issue/#entry215823

In particular the next to last post.  I’m not sure how your 9 got to be a 9, but there could be floating rounding errors getting in there somewhere.  math.floor might be a better choice than math.ceil.

Rob