# How to prevent Inf or NaN at low floating number precision

**URL:** <https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705>\
**Category:** General Usage\
**Created:** [June 28, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705 "2021-06-28T16:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yingqiuz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingqiuz/32/25165_2.png) [@yingqiuz](https://discourse.julialang.org/u/yingqiuz)\
**Post date:** [June 28, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705/1 "2021-06-28T16:23:35Z")

</div>

Hi

I am programming at Float32 to improve the speed. In my calculations, however, Inf or NaNs occur occasionally due to the use of `exp.(x)`. For example `x=90.30891f0; exp(x)` will yield Inf32, even if x is fine.

I was wondering if there is a way to prevent this behaviour, without increase the floating number precision? In general, I have no idea where these values might occur.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 28, 2021, 4:25pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705/2 "2021-06-28T16:25:31Z")

</div>

I mean…

```julia
julia> prevfloat(typemax(Float32))
3.4028235f38

julia> exp(90.30891)
1.6621158068743495e39

```

`x` is fine but `exp(x)` is simply outside of `Float32`’s range, nothing Julia can do here sorry

---

<div class="post-metadata">

**Author:** ![yingqiuz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingqiuz/32/25165_2.png) [@yingqiuz](https://discourse.julialang.org/u/yingqiuz)\
**Post date:** [June 28, 2021, 4:27pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705/3 "2021-06-28T16:27:59Z")

</div>

Thank you. Maybe I’ll go back to Float64…  
Just a bit curious why Pytorch use Float32 as default, since using exp at Float32 can easily cause an overflow.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 28, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705/4 "2021-06-28T16:28:48Z")

</div>

> [@yingqiuz](#):
>
> Just a bit curious why Pytorch use Float32 as default

🤷‍♂️ speed

because ML typically don’t need Float64 as their “weights” and their data are “normalized”

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 28, 2021, 4:44pm UTC](https://discourse.julialang.org/t/how-to-prevent-inf-or-nan-at-low-floating-number-precision/63705/5 "2021-06-28T16:44:08Z")

</div>

If you need exp(x) as an intermediate value but you’re going to mix it with another calculation, then there are ways to do the entire calculation in a more stable way.

An example `exp(x)/exp(y) = exp(x-y)` and similar things. This is pretty standard numerical methods stuff. If on the other hand you just need to output the value of exp(x) for a large x, then you’ll have to switch to a higher precision. For example `exp(convert(Float64,x))`

You can work with Float32 but convert it before applying exp or other functions that grow large.
