# Float representation of the same number may not be equal?

**URL:** <https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 19, 2019, 1:01pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276 "2019-11-19T13:01:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![LarkyJulia](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@LarkyJulia](https://discourse.julialang.org/u/LarkyJulia)\
**Post date:** [November 19, 2019, 1:01pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/1 "2019-11-19T13:01:30Z")

</div>

When programming before, I sometimes get problem like:  
a=3.1415926  
b=3.1415926  
a==b  
it output false, why? check it out and found b is actually 3.1415926000000001 something like that.  
so I always have to replace a==b with something like abs(a-b)\<10e-10  
I noticed Julia has many builtin functions like eps(), prevfloat(), nextfloat(), isequal()  
Can any of these functions avoid the above situation? or Julia has something else to do this? Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 19, 2019, 1:05pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/2 "2019-11-19T13:05:00Z")

</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:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 19, 2019, 1:09pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/3 "2019-11-19T13:09:05Z")

</div>

> [@LarkyJulia](#):
>
> so I always have to replace a==b with something like abs(a-b)\<10e-10

See e.g. [Comparing Floating Point Numbers, 2012 Edition | Random ASCII – tech blog of Bruce Dawson](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) if you want to read more about floating point comparisons.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2019, 1:11pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/4 "2019-11-19T13:11:30Z")

</div>

> [@LarkyJulia](#):
>
> a=3.1415926  
> b=3.1415926  
> a==b

Also, this should never be the case in any languages. It’s possible that some don’t **show** enough digits but none of them should generate different results from the same assignment.

---

<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:** [November 19, 2019, 1:24pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/5 "2019-11-19T13:24:18Z")

</div>

Wow, that’s good reading. Thanks!

---

<div class="post-metadata">

**Author:** ![LarkyJulia](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@LarkyJulia](https://discourse.julialang.org/u/LarkyJulia)\
**Post date:** [November 19, 2019, 1:40pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/6 "2019-11-19T13:40:16Z")

</div>

Thanks, looks like you guys all suggesting this in Julia to judge if two floats are equal?:

abs(a-b)\<n\*eps(float), with n an appropriate small number .

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 19, 2019, 1:46pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/7 "2019-11-19T13:46:21Z")

</div>

Not really. Best practice is to think about the context and then use the relevant comparison. Eg even `==` can be the best choice for unit testing a simple calculation constructed to be exact.

See eg `isapprox`, which has two relevant parameters and is generally a reasonable solution.

Also, for the record,

```julia
julia> 3.1415926000000001 == 3.1415926
true

```

in Julia (not enough precision to represent that last digit), so you must have been doing something different. Generally, unless they are rounded in the context of a container, different `Float64`s will print differently.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2019, 1:53pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/8 "2019-11-19T13:53:37Z")

</div>

Not really.

FWIW, you need to define you problem first.

If your problem is having numbers that prints the same compares equal, then that’s already the case in julia.

If your problem is to have numbers that prints the same in certain other languages compares equal, well, you need to figure out how those languages print numbers. It’s most likely just printing a fixed number of digits in which case you are approximately comparing with a fixed relative tolerrance

If you have a real problem with some known rounding error, you should just check for that tolerance.

---

<div class="post-metadata">

**Author:** ![LarkyJulia](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@LarkyJulia](https://discourse.julialang.org/u/LarkyJulia)\
**Post date:** [November 19, 2019, 1:56pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/9 "2019-11-19T13:56:03Z")

</div>

> [@Tamas\_Papp](#):
>
> 3.1415926000000001 == 3.1415926

Wow, I didn’t even try that!!! 😁That happened to be 1e-16 smaller than eps(Float64)

---

<div class="post-metadata">

**Author:** ![LarkyJulia](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@LarkyJulia](https://discourse.julialang.org/u/LarkyJulia)\
**Post date:** [November 19, 2019, 2:00pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/10 "2019-11-19T14:00:13Z")

</div>

Thanks, I see!

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 19, 2019, 9:42pm UTC](https://discourse.julialang.org/t/float-representation-of-the-same-number-may-not-be-equal/31276/11 "2019-11-19T21:42:44Z")

</div>

I find this particularly painful in matrices

```julia
julia> A = [1.0 2.0; 3.0 4.0]; invA = inv(A)
2×2 Array{Float64,2}:
 -2.0 1.0
  1.5 -0.5

julia> println(invA)
[-1.9999999999999996 0.9999999999999998; 1.4999999999999998 -0.4999999999999999]

julia> B=(x->round(x,digits=14)).(invA)
2×2 Array{Float64,2}:
 -2.0 1.0
  1.5 -0.5

julia> println(B)
[-2.0 1.0; 1.5 -0.5]

```

```julia
julia> C=(x->round(x,sigdigits=14)).(invA)
2×2 Array{Float64,2}:
 -2.0 1.0
  1.5 -0.5

julia> println(C)
[-2.0 1.0; 1.5 -0.5]

```
