# Mismatch between nextfloat(0.0) and eps(Float64)

**URL:** <https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104>\
**Category:** Numerics\
**Tags:** float\
**Created:** [June 3, 2024, 9:11am UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104 "2024-06-03T09:11:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillermo\_C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillermo_c/32/209675_2.png) [@Guillermo\_C](https://discourse.julialang.org/u/Guillermo_C)\
**Post date:** [June 3, 2024, 9:11am UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/1 "2024-06-03T09:11:26Z")

</div>

Dear Julia community,

I am testing the representation of floating point values in my machine. This is the behavior I am seeing:

```julia
julia> x = 0.0
0.0
help?> x
(...)
x is of type Float64
(...)
julia> nextfloat(x)
5.0e-324
julia> eps(Float64)
2.220446049250313e-16

```

My question:  
Shouldn’t the outputs of eps(Float64) and nextfloat(x) coincide?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 3, 2024, 9:17am UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/2 "2024-06-03T09:17:26Z")

</div>

The gap between consecutive Float64/32 is not a constant. `eps(Float64)` is defined as `eps(1.0)`:

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

julia> nextfloat(0.0)
5.0e-324

julia> eps(1.0)
2.220446049250313e-16

julia> eps(Float64)
2.220446049250313e-16

julia> nextfloat(1.0)
1.0000000000000002

```

The gap increases for larger Float values.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 3, 2024, 9:55am UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/3 "2024-06-03T09:55:14Z")

</div>

In particular, consider:

```julia
julia> eps(Inf)
NaN

julia> eps(prevfloat(Inf))
1.99584030953472e292

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 3, 2024, 3:21pm UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/4 "2024-06-03T15:21:56Z")

</div>

You can think of the value returned by the `eps` for the type at large as being like a _relative_ epsilon. It gets scaled by the power of two\* of the value you’re looking at. So you have:

```julia-repl
julia> eps(1.0) == eps(Float64)
true

julia> eps(16.0) == 16eps(Float64)
true

julia> eps(0.125) == .125eps(Float64)
true

julia> eps(2.0^-1022) == 2.0^-1022*eps(Float64)
true

julia> eps(2.0^1023) == 2.0^1023*eps(Float64)
true

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [June 3, 2024, 4:19pm UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/5 "2024-06-03T16:19:16Z")

</div>

> [@Guillermo\_C](#):
>
> Shouldn’t the outputs of eps(Float64) and nextfloat(x) coincide?

No. The correct definition is `myEps(::Type{T}) where T = nextfloat(one(T))-one(T)`. `eps` basically counts the significant bits, while `nextfloat(zero(T))` counts the exponent bits (up to shennenigans with subnormals).

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [June 3, 2024, 6:13pm UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/6 "2024-06-03T18:13:54Z")

</div>

[1.1. Floating-point numbers — Fundamentals of Numerical Computation (tobydriscoll.net)](https://tobydriscoll.net/fnc-julia/intro/floating-point.html)

Plugging this here as a pretty good resource for helping to understand floating point representations.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 3, 2024, 6:21pm UTC](https://discourse.julialang.org/t/mismatch-between-nextfloat-0-0-and-eps-float64/115104/7 "2024-06-03T18:21:14Z")

</div>

Also nice:

> **[Floating-point arithmetic](https://hal.science/hal-04095151)**
>
> Floating-point numbers have an intuitive meaning when it comes to physics-based numerical computations, and they have thus become the most common way of approximating real numbers in computers. The IEEE-754 Standard has played a large part in making...
