# Float comparison error

**URL:** <https://discourse.julialang.org/t/float-comparison-error/11779>\
**Category:** General Usage\
**Created:** [June 19, 2018, 10:38am UTC](https://discourse.julialang.org/t/float-comparison-error/11779 "2018-06-19T10:38:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [June 19, 2018, 10:38am UTC](https://discourse.julialang.org/t/float-comparison-error/11779/1 "2018-06-19T10:38:47Z")

</div>

Inside a project I have been working in, I detected an anomaly when I make comparisons between two floats numbers. I have reproduced this error in a very basic loop just to show the problem in a simple way :

```julia
clearconsole()
list_temp = [0.40,0.30,0.20,0.10]
acc = 0 
for number in list_temp
    acc = acc + number 
    println("number = ", number ," ","acc = ", acc)
    if acc == 0.90
        println("detected")
    end
end

```

I got the following output

```julia
number = 0.4 acc = 0.4
number = 0.3 acc = 0.7
number = 0.2 acc = 0.8999999999999999
number = 0.1 acc = 0.9999999999999999

```

That means, the condition doesn’t not is accomplished even tough it was supposed to be truth. the variable **Acc** would take the value 0.9 instead of 0.8999999. Because of this, the condition is not satisfied and I got a logical problem in my code. Anyone knows how to deal with it ? I could use something like truncate but I would like to stay with a simple condition and the numbers I will deal with are not big… Thanks in advance.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 19, 2018, 10:43am UTC](https://discourse.julialang.org/t/float-comparison-error/11779/2 "2018-06-19T10:43:47Z")

</div>

Use `≈` (`\approx`) for floating point number comparisons.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [June 19, 2018, 11:34am UTC](https://discourse.julialang.org/t/float-comparison-error/11779/3 "2018-06-19T11:34:44Z")

</div>

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678):
>
> Sometimes people are surprised by the results of floating-point calculations such as julia\> 5/6 0.8333333333333334 # shouldn't the last digit be 3? julia\> 2.6 - 0.7 - 1.9 2.220446049250313e-16 # shouldn't the answer be 0? These are not bugs in Julia. They’re consequences of the IEEE-standard 64-bit binary representation of floating-point numbers that is burned into computer hardware, which Julia and many other languages use by default. Brief explanation You can t…

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [June 19, 2018, 1:52pm UTC](https://discourse.julialang.org/t/float-comparison-error/11779/4 "2018-06-19T13:52:17Z")

</div>

And if you use `\approx`, check the documentation for the tolerances you want to use.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 19, 2018, 3:18pm UTC](https://discourse.julialang.org/t/float-comparison-error/11779/5 "2018-06-19T15:18:31Z")

</div>

Note the ability to provide an absolute tolerance `atol`, a relative tolerance `rtol` or both. The default relative tolerance is often good but be aware that zero is only approximately equal to itself with the defaults.
