MultiFloats.jl icon indicating copy to clipboard operation
MultiFloats.jl copied to clipboard

`significand`, `frexp` not defined

Open mcabbott opened this issue 2 years ago • 1 comments

julia> significand(Float64x2(123.4))
ERROR: MethodError: no method matching significand(::MultiFloat{Float64, 2})
Closest candidates are:
  significand(::T) where T<:Union{Float16, Float32, Float64} at math.jl:896
  significand(::DoubleFloat{T}) where T<:Union{Float16, Float32, Float64} at ~/.julia/packages/DoubleFloats/h3HrU/src/math/prearith/prearith.jl:68
  significand(::BigFloat) at mpfr.jl:873
...

julia> significand(123.4)
1.928125

Xref https://github.com/JuliaMath/DoubleFloats.jl/issues/148

mcabbott avatar May 09 '22 00:05 mcabbott

More useful is probably frexp, also not defined though:

julia> frexp(Float64x2(123.4))
ERROR: MethodError: no method matching frexp(::MultiFloat{Float64, 2})
Closest candidates are:
  frexp(::T) where T<:Union{Float16, Float32, Float64} at math.jl:921
  frexp(::DoubleFloat{T}) where T<:Union{Float16, Float32, Float64} at ...
  frexp(::BigFloat) at mpfr.jl:866

mcabbott avatar May 10 '22 17:05 mcabbott

Hey @mcabbott, thanks for your interest in MultiFloats.jl! This is a good suggestion, and I am interested in having standard floating-point introspection methods work for MultiFloat types. However, it is actually impossible to implement significand and frexp for the multi-limb representation used by MultiFloats.jl without possible accuracy loss.

The core issue is this: imagine you have a Float64x2 with limbs (1.0e+308, 1.0e-308). If you call significand or frexp on this number, it has to be scaled down by a factor of 10308, which will cause the second limb to underflow to zero. This dramatically reduces the precision of the result.

For this reason, I don't want to provide significand and frexp functions in MultiFloats.jl, as they are typically specified to be lossless operations. However, I recognize that these operations are still useful in a lossy form (e.g., for implementing transcendental functions), so if you have a use case for them, I would be happy to provide MultiFloats.lossy_significand and MultiFloats.lossy_frexp functions.

If you would like this functionality, please feel free to leave another comment and reopen this thread. Until then, I will close this issue.

dzhang314 avatar Sep 08 '24 23:09 dzhang314