# No overflow nor underflow rounding mode for Floats

**URL:** <https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487>\
**Category:** Numerics\
**Created:** [July 19, 2018, 12:08pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487 "2018-07-19T12:08:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![milankl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milankl/32/4198_2.png) [@milankl](https://discourse.julialang.org/u/milankl)\
**Post date:** [July 19, 2018, 12:08pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/1 "2018-07-19T12:08:36Z")

</div>

Hey there,  
I see from the manual that different rounding modes for floats exist. However, I was wondering whether there is a smart way to set or emulate a rounding mode that is similar to `RoundNearest`, but without overflow nor underflow. The smallest representable number is never round to zero and the largest never to infinity. At the moment we have

```julia
julia> a = prevfloat(typemax(Float16))
Float16(6.55e4)

julia> a+a
Inf16

```

causing an overflow and

```julia
julia> b = nextfloat(zero(Float16))
Float16(6.0e-8)

julia> b/2
Float16(0.0)

```

causing underflow. Similarly for Float32, Float64 and negative numbers. So in the first example I would like `a+a` to yield `a` and in the second `b/2` to yield `b`. This is motivated as 2a is closer to a than to infinity … Do you know whether there is any way to set this behaviour?

---

<div class="post-metadata">

**Author:** ![milankl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milankl/32/4198_2.png) [@milankl](https://discourse.julialang.org/u/milankl)\
**Post date:** [August 6, 2018, 11:45am UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/2 "2018-08-06T11:45:45Z")

</div>

Anybody any thoughts on that?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 6, 2018, 3:13pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/3 "2018-08-06T15:13:03Z")

</div>

You could create your own type which wraps a float and then implement the basic operations yourself, using whatever rounding scheme you like. Something like:

```julia
struct MyFloat{F <: AbstractFloat} <: Number
  value::F 
end

Base.:+(f1::MyFloat, f2::MyFloat) = MyFloat(your_particular_rounding_scheme(f1.value + f2.value))

```

for interaction between your new type and Julia’s existing numbers, check out the conversion and promotion section of the docs: [https://docs.julialang.org/en/stable/manual/conversion-and-promotion/](https://docs.julialang.org/en/stable/manual/conversion-and-promotion/)

---

<div class="post-metadata">

**Author:** ![milankl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milankl/32/4198_2.png) [@milankl](https://discourse.julialang.org/u/milankl)\
**Post date:** [August 6, 2018, 3:16pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/4 "2018-08-06T15:16:15Z")

</div>

Thanks for pointing me in this direction, I’ll give this a try!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 6, 2018, 3:19pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/5 "2018-08-06T15:19:20Z")

</div>

I also vaguely remember there being a helpful package designed to make it a bit easier to create custom `Number` types, but i can’t remember what it’s called (nor can google find it). Perhaps someone else here will remember…

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 19, 2018, 2:17pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/6 "2018-08-19T14:17:05Z")

</div>

You can try [FiniteFloats.jl](https://github.com/JeffreySarnoff/FiniteFloats.jl). Infinities are avoided, as are many NaNs. I do not avoid zeros, so `prevfloat(nextfloat(0.0))` will be 0.0. Post an issue if any arises.  
Please let me know if you find it is useful to you.

---

<div class="post-metadata">

**Author:** ![milankl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milankl/32/4198_2.png) [@milankl](https://discourse.julialang.org/u/milankl)\
**Post date:** [August 19, 2018, 3:51pm UTC](https://discourse.julialang.org/t/no-overflow-nor-underflow-rounding-mode-for-floats/12487/7 "2018-08-19T15:51:40Z")

</div>

That looks amazing! I’ll go and see whether this fits my purpose and then come back to you. Thanks!
