Weird floating point under/overflow behavior

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> 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

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

4 Likes

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.

1 Like

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.

2 Likes