assert_equal 1.1035, 1.0035 + 0.1 is false

some of my tests failed when comparing two floats.

the comparison of:

1.1035 == 1.0035 + 0.1

evaluates to false - i do not really understand why.

must depend on the value range, because

1.103 == 1.003 + 0.1

evaluates to true.

would be great if anyone could help me getting this working and maybe
giving an explanation why ruby do not handle the == operator for float
as one would expect (coming from java).

tested on:

1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

regards
jan zimmek

some of my tests failed when comparing two floats.

the comparison of:

1.1035 == 1.0035 + 0.1

evaluates to false - i do not really understand why.

must depend on the value range, because

1.103 == 1.003 + 0.1

basically because 0.1 cannot be exactly represented in binary.

assert_in_delta is good for this sort of stuff.

Fred

Crud, he beat me to it.

You're going to have to either change the logic or change the testing
procedure for it.

thanks for clarification ... have found some further explanations/
solutions and will change my testing-logic.

Quoting jzimmek <jan.zimmek@web.de>:

some of my tests failed when comparing two floats.

the comparison of:

1.1035 == 1.0035 + 0.1

evaluates to false - i do not really understand why.

must depend on the value range, because

1.103 == 1.003 + 0.1

In general, exact comparisons of floating point numbers is a bad idea. A
better idea is something along the lines of:

(value1 - value2).abs <= epsilon

epsilon may be a fixed value of a fraction of the values, maybe

epsilon = (value1 + value2) / 100

i.e. 1/50 the average of the two. The best solution depends on the particular
problem.

Jeffrey