# Assert\_equal 1.1035, 1.0035 + 0.1 is false

**URL:** <https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468>\
**Category:** Rails\
**Created:** [January 23, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468 "2009-01-23T17:50:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzimmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jzimmek](https://www.ruby-forum.com/u/jzimmek)\
**Post date:** [January 23, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468/1 "2009-01-23T17:50:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jzimmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jzimmek](https://www.ruby-forum.com/u/jzimmek)\
**Post date:** [January 23, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468/2 "2009-01-23T17:56:08Z")

</div>

On 23 Jan 2009, at 16:49, jzimmek wrote:

> 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

---

<div class="post-metadata">

**Author:** ![jzimmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jzimmek](https://www.ruby-forum.com/u/jzimmek)\
**Post date:** [January 23, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468/3 "2009-01-23T18:00:52Z")

</div>

Crud, he beat me to it.

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

On Jan 23, 10:55 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jzimmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jzimmek](https://www.ruby-forum.com/u/jzimmek)\
**Post date:** [January 23, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468/4 "2009-01-23T18:12:05Z")

</div>

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

On Jan 23, 5:55 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jzimmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jzimmek](https://www.ruby-forum.com/u/jzimmek)\
**Post date:** [January 24, 2009, 5:50am UTC](https://www.ruby-forum.com/t/assert-equal-1-1035-1-0035-0-1-is-false/158468/5 "2009-01-24T05:50:28Z")

</div>

Quoting jzimmek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
