# Bug with isapprox?

**URL:** https://discourse.julialang.org/t/bug-with-isapprox/7600
**Category:** General Usage
**Created:** [December 7, 2017, 10:32am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600 "2017-12-07T10:32:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)
#### Post date: [December 7, 2017, 10:32am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/1 "2017-12-07T10:32:06Z")

</div>

Hi all,

I don’t understand the behavior of isapprox.

```
 julia> -3.03664471426105e-17 ≈ 0.0
  false

  julia> 1.0 - 3.03664471426105e-17 ≈ 1.0 - 0.0
  true

```

Can someone reproduce/explain what looks like a bug here?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [December 7, 2017, 10:49am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/2 "2017-12-07T10:49:27Z")

</div>

Consider

```julia
julia> eps(0.0)
5.0e-324

julia> eps(1.0)
2.220446049250313e-16

```

and therefore

```julia
julia> 1.0 - 3.03664471426105e-17 == 1.0
true

```

---

<div class="post-metadata">

### Author: ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)
#### Post date: [December 7, 2017, 10:56am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/3 "2017-12-07T10:56:40Z")

</div>

I am very surprised that `eps(0.0)` is so small and I don’t understand why `nextfloat(0.0)` is `5.0e-324`. Any hint?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [December 7, 2017, 10:58am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/4 "2017-12-07T10:58:01Z")

</div>

> **[Subnormal number](https://en.wikipedia.org/wiki/Denormal_number)**
>
> In computer science, subnormal numbers are the subset of denormalized numbers (sometimes called denormals)\[a\] that fill the underflow gap around zero in floating-point arithmetic. Any non-zero number with magnitude smaller than the smallest normal number is subnormal.
> In a normal floating-point value, there are no leading zeros in the significand (mantissa); rather, leading zeros are removed by adjusting the exponent (for example, the number 0.0123 would be written as 1.23 × 10−2). Conversely, a...

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 7, 2017, 1:32pm UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/5 "2017-12-07T13:32:55Z")

</div>

This has nothing to do with the value of `eps(0.0)`. `x ≈ 0.0` will always give `false` for `x ≠ 0`, because `≈` tests only _relative_ error. If `x ≠ 0`, then _no_ significant digits of `x` match `0.0`, so the result of `x ≈ 0.0` is `false`.

In particular, `x ≈ y` tests `norm(x-y) ≤ sqrt(eps(T))*max(norm(x),norm(y))` with `T=typeof(real(x-y))`, which tests whether about half of the significant digits match.

If you call `isapprox(x,y)` explicitly, you can pass an `atol` argument to instead pass an _absolute_ tolerance, i.e. to test whether `norm(x-y) ≤ atol`. But since there is no reasonable way to pick a default value of `atol` (it is dimensionful, i.e. it depends on the _scale_ of `x` and `y`), using `≈` sets `atol` to zero.

Note that `1.0 - 1e-10 ≈ 1.0` gives `true`, because more than half of the significant digits match `1.0`.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [December 7, 2017, 2:51pm UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/6 "2017-12-07T14:51:56Z")

</div>

I stand corrected. Thanks for chiming in! 🙂

---

<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: [December 8, 2017, 4:40am UTC](https://discourse.julialang.org/t/bug-with-isapprox/7600/7 "2017-12-08T04:40:24Z")

</div>

If you are writing tests with the standard `Test` module then you can write

```julia
@test x ≈ y atol=ε

```

The `@test` macro translates that to `isapprox(x, y, atol=ε)`.
