# Weird floating point under/overflow behavior

**URL:** <https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340>\
**Category:** General Usage\
**Created:** [March 31, 2024, 1:23am UTC](https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340 "2024-03-31T01:23:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pxshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pxshen/32/38776_2.png) [@pxshen](https://discourse.julialang.org/u/pxshen)\
**Post date:** [March 31, 2024, 1:23am UTC](https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340/1 "2024-03-31T01:23:07Z")

</div>

Expect to underflow to 0 and overflow to biggest value, but results below seem opposite? I train ML and had `nan` from dividing by big # in gradients

```julia
julia> 1f0/2^64
Inf32

julia> 1f0/(2^64)
Inf32

julia> 1f0*(2^64)
0.0f0

julia> 1/(2^64)
Inf

julia> 1.0*(2^64)
0.0

```

---

<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:** [March 31, 2024, 1:26am UTC](https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340/2 "2024-03-31T01:26:28Z")

</div>

This isn’t about floating point — it’s the integers! Check the value of `2^64`.

> [@Julia messes up integer exponents?](https://discourse.julialang.org/t/julia-messes-up-integer-exponents/20773):
>
> I was using avogadro’s number in a calculation and couldn’t figure out why the results were not matching my matlab or python code… Turns out using integers as exponents gets you in trouble in Julia?They start off innocent but at higher powers, but really not that big for science (Avogadro’s number is very popular!!! as are many other constants…) julia\> 10^1 10 julia\> 10^2 100 julia\> 10^5 100000 julia\> 10^15 1000000000000000 julia\> 10^20 7766279631452241920 julia\> 10^16 10000000000000000 julia\> …

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 31, 2024, 2:05am UTC](https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340/3 "2024-03-31T02:05:57Z")

</div>

pxshen might’ve expected `^(::Int, ::Int)` to return a Float64 because negative powers like `5^-4` do work that way in the REPL. I’m not actually sure how because reflection gives the integer powers method that throws an error on negative exponents, and powers of non-literal integers does accordingly throw on negative exponents.

---

<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:** [March 31, 2024, 3:00am UTC](https://discourse.julialang.org/t/weird-floating-point-under-overflow-behavior/112340/4 "2024-03-31T03:00:28Z")

</div>

> [@Benny](#):
>
> I’m not actually sure how because reflection gives the integer powers method that throws an error on negative exponents, and powers of non-literal integers does accordingly throw on negative exponents.

OT, but this is because the parsing or lowering stage of the compiler transforms `^`, where the exponent is a literal, to `Base.literal_pow` calls. IMO that was a bad idea, for the reason you give.
