# Understanding nextfloat() and prevfloat()

**URL:** <https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 5, 2021, 9:41pm UTC](https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265 "2021-10-05T21:41:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![junelac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/junelac/32/29728_2.png) [@junelac](https://discourse.julialang.org/u/junelac)\
**Post date:** [October 5, 2021, 9:41pm UTC](https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265/1 "2021-10-05T21:41:27Z")

</div>

Hello,

I’m currently reading the Julia documentation/tutorial and I think I don’t really understand how nextfloat(x) and prevfloat(x) are computed. Reading the document I thought they would be respectively `x + eps(x)` and `x - eps(x)` , however if it seems to be the case for nextfloat(x), it is not for prevfloat(x).

example:

```julia
julia> 1.0+eps(1.0)
1.0000000000000002

julia> nextfloat(1.0)
1.0000000000000002

```

and

```julia
julia> 1.0-eps(1.0)
0.9999999999999998

julia> prevfloat(1.0)
0.9999999999999999

```

So, how are these number computed ?

Thank you !

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 5, 2021, 9:52pm UTC](https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265/2 "2021-10-05T21:52:10Z")

</div>

The thing you’re missing is that `eps(0.9999999999999999)` is half of `eps(1.0)` due to how floating point numbers work. The way these results are computed by Julia is that `nextfloat(x)` is just the result of reinterpreting `x` as an `Integer`, adding 1, and reinterpreting back as a floating point number (or subtracting for prevfloat).

---

<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:** [October 5, 2021, 10:42pm UTC](https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265/3 "2021-10-05T22:42:41Z")

</div>

To elaborate on that, values that are exactly powers of 2—like `1.0`—are on the cusp between a smaller `eps` size and the next bigger `eps` size. And since `eps(x)` is defined such that `eps(x) = nextfloat(x) - x`, that means that `eps(x)` is going to be bigger than `x - prevfloat(x)` for powers of two; for all other values of `x`, `nextfloat(x) - x` and `x - prevfloat(x)` are equal. There’s a case to be made for either definition of `eps(x)` on the cusp, but the larger value is conventional.

---

<div class="post-metadata">

**Author:** ![junelac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/junelac/32/29728_2.png) [@junelac](https://discourse.julialang.org/u/junelac)\
**Post date:** [October 5, 2021, 11:01pm UTC](https://discourse.julialang.org/t/understanding-nextfloat-and-prevfloat/69265/4 "2021-10-05T23:01:01Z")

</div>

thank you both @Oscar_Smith and @StefanKarpinski I understand now
