dzhang314 / MultiFloats.jl

Fast, SIMD-accelerated extended-precision arithmetic for Julia
MIT License
75 stars 10 forks source link

bug: weird issues with big MultiFloats #38

Closed nsajko closed 1 week ago

nsajko commented 1 year ago
julia> using MultiFloats

julia> MultiFloats.use_standard_multifloat_arithmetic(20)

julia> g(n::Integer) = rand(MultiFloat{Float64, n})
g (generic function with 1 method)

julia> x = g(20)
-2.2914735159347884e+302

julia> 1 < x
false

julia> x < -1
true

julia> Float64(x)
0.6714350546186427

So it seems there are multiple issues here:

  1. rand produces a number of huge magnitude
  2. rand produces negative number
  3. the conversion to Float64 (producing a positive number) is not consistent with the < operator
dzhang314 commented 1 week ago

Hey @nsajko, thanks for your interest in MultiFloats.jl! I finally got around to looking at this today (sorry it took a while), and it turns out this was happening because a Float64x{20} is so long, the exponent underflows past -1024, causing the final limb to overflow to an extremely large number. I've fixed this by adding an explicit underflow check and returning 0.0 if it's hit.

Please note that Float64x{20} is not useful because it is so long, the final component is essentially guaranteed to underflow out of the allowed exponent range for a Float64. In practical usage, you don't want to go past about Float64x8.

You will see the fix in the next release of MultiFloats.jl.