# Why does 16//1 and 16.0 not equal 16 in for loop?

**URL:** <https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748>\
**Category:** General Usage\
**Created:** [September 6, 2021, 1:02pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748 "2021-09-06T13:02:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [September 6, 2021, 1:02pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/1 "2021-09-06T13:02:29Z")

</div>

I am getting some behavior I can not explain. The context is that a chamber gets 9/16’ths of its content emptied, initially air. Then the 9/16ths that were removed are replaced with some pure gas. This process is repeted, and I wanted to find out how fast one starts getting pure gas. So I wrote the following script, and included a sanity-check that the air part `a` plus the gas part `g` always sum to 16. But the boolean comparison failed in the following three versions of the script:  
Normal division, comparison with 16 as Int:

```julia
julia> begin
           a = 16
           g = 0
           counter = 0
           for i in 1:2
               a *= 7/16
               g *= 7/16
               g += 9
               counter +=1
               if a+g !== 16
                   println("Error! $a+$g=$(a+g) does not equal 16.")
                   #error("Gas parts do not sum to 16")
               end
               println("Iteration $counter:\nGas to air ratio = $(round(g/a, sigdigits=3))\n\n")
           end
       end
Error! 7.0+9.0=16.0 does not equal 16.
Iteration 1:
Gas to air ratio = 1.29

Error! 3.0625+12.9375=16.0 does not equal 16.
Iteration 2:
Gas to air ratio = 4.22

```

Even though

```julia
julia> 0.005+15.995
16.0

julia> 0.005+15.995 == 16
true

```

Rational division, comparison with 16 as Int:

```julia
julia> begin
           a = 16
           g = 0
           counter = 0
           for i in 1:2
               a *= 7//16
               g *= 7//16
               g += 9
               counter +=1
               if a+g !== 16
                   println("Error! $a+$g=$(a+g) does not equal 16.")
                   #error("Gas parts do not sum to 16")
               end
               println("Iteration $counter:\nGas to air ratio = $(round(g/a, sigdigits=3))\n\n")
           end
       end
Error! 7//1+9//1=16//1 does not equal 16.
Iteration 1:
Gas to air ratio = 1.29

Error! 49//16+207//16=16//1 does not equal 16.
Iteration 2:
Gas to air ratio = 4.22

```

Even though

```julia
julia> 16//1==16
true

```

Rational division, comparison with 16 as float:

```julia
julia> begin
           a = 16
           g = 0
           counter = 0
           for i in 1:2
               a *= 7//16
               g *= 7//16
               g += 9
               counter +=1
               if a+g !== 16.0
                   println("Error! $a+$g=$(a+g) does not equal 16.")
                   #error("Gas parts do not sum to 16")
               end
               println("Iteration $counter:\nGas to air ratio = $(round(g/a, sigdigits=3))\n\n")
           end
       end
Error! 7//1+9//1=16//1 does not equal 16.
Iteration 1:
Gas to air ratio = 1.29

Error! 49//16+207//16=16//1 does not equal 16.
Iteration 2:
Gas to air ratio = 4.22

```

The only successful version was without rationals, and where 16 was defined as a float as opposed to an int:

```julia
julia> begin
           a = 16
           g = 0
           counter = 0
           for i in 1:2
               a *= 7/16
               g *= 7/16
               g += 9
               counter +=1
               if a+g !== 16.0
                   println("Error! $a+$g=$(a+g) does not equal 16.")
                   #error("Gas parts do not sum to 16")
               end
               println("Iteration $counter:\nGas to air ratio = $(round(g/a, sigdigits=3))\n\n")
           end
       end
Iteration 1:
Gas to air ratio = 1.29

Iteration 2:
Gas to air ratio = 4.22

```

I can not explain why they dont all evaluate to `true`. Can you help me understand?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 6, 2021, 1:12pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/2 "2021-09-06T13:12:34Z")

</div>

`!==` == `!(===)`. It checks whether the two values are identical (i.e. inseparable by the computer). You’re looking for `!=`.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [September 6, 2021, 1:16pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/3 "2021-09-06T13:16:45Z")

</div>

Oh snap you’re right! My bad. Thanks!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 6, 2021, 4:26pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/4 "2021-09-06T16:26:29Z")

</div>

But it’s not just about `!=` vs `!==`. You shouldn’t have conditions depend on floating point comparisons, since you will very frequently run into floating point inaccuracies.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 6, 2021, 5:00pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/5 "2021-09-06T17:00:32Z")

</div>

> [@DNF](#):
>
> You shouldn’t have conditions depend on floating point comparisons

What about using `isapprox`?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 6, 2021, 5:16pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/6 "2021-09-06T17:16:42Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [September 6, 2021, 6:09pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/7 "2021-09-06T18:09:31Z")

</div>

> [@DNF](#):
>
> You shouldn’t have conditions depend on floating point comparisons, since you will very frequently run into floating point inaccuracies.

Right. That was one of the things I was checking for by printing the sum. But as a general principle, you are right, `isapprox` is better suited. I just looked up the docstring and realized that the default tolerances for `isapprox` are actually set just right for comparison of floating point numbers. Thanks!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 6, 2021, 8:31pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/8 "2021-09-06T20:31:55Z")

</div>

I felt like the difference between `!==` and `!=` was the more pressing question, but yes, this is better.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [September 6, 2021, 8:42pm UTC](https://discourse.julialang.org/t/why-does-16-1-and-16-0-not-equal-16-in-for-loop/67748/9 "2021-09-06T20:42:16Z")

</div>

`≈` is another way to write `isapprox`.
