Julia: Some irrationals can't be converted to `BigFloat` using a rounding mode

Created on 14 Dec 2019  Â·  3Comments  Â·  Source: JuliaLang/julia

Some irrationals numbers from Base.MathConstants produce an error when we try to convert them to BigFloat using a rounding mode. Namely euler constant and the golden ratio do, as it can be seen in the code below.

julia> using Base.MathConstants

julia> irrationals = (π, e, γ, φ, catalan)
(π, ℯ, γ, φ, catalan)

julia> BigFloat(e, RoundUp)
ERROR: MethodError: no method matching BigFloat(::Irrational{:ℯ}, ::Base.MPFR.MPFRRoundingMode; precision=256)
Closest candidates are:
  BigFloat(::BigFloat, ::Base.MPFR.MPFRRoundingMode; precision) at mpfr.jl:184
  BigFloat(::Int32, ::Base.MPFR.MPFRRoundingMode; precision) at mpfr.jl:204
  BigFloat(::UInt32, ::Base.MPFR.MPFRRoundingMode; precision) at mpfr.jl:204
  ...
Stacktrace:
 [1] #BigFloat#21(::Int32, ::Type{BigFloat}, ::Irrational{:ℯ}, ::RoundingMode{:Up}) at .\mpfr.jl:260
 [2] BigFloat(::Irrational{:ℯ}, ::RoundingMode{:Up}) at .\mpfr.jl:260
 [3] top-level scope at REPL[21]:1

julia> for irr in irrationals
           try
               BigFloat(irr, RoundUp)
               println("Success for $irr")
           catch MethodError
               println("Failure for $irr")
           end
       end
Success for π
Failure for ℯ
Success for γ
Failure for φ
Success for catalan
bug maths

Most helpful comment

BigFloats as a type wrap the _MPFR_ library MPFR docs. See mpfr.jl for how Julia does this. MPFR exports four arbitrary precision constants (search their docs for "_mpfr_const_"): log2, pi, euler, catalan. This explains why your pass/fail rounding test passes using π, γ, catalan.

Looking at the way irrationals are defined irrationals.jl and how the irrational constants in mathconstants.jl are given gives you a starting point in determining what needs be done to support ℯ and φ properly. Balance that part of it with the specifics of rounding in mpfr.jl to get a better sense of what to modify and how to approach solving this issue.

Why is ℯ an irrational known to Base.MathConstants? It made the cut :)
Why is exp(1) a Float64? All transcendental math functions return Float64 values when the argument to the function is a Float64 or an Int. typeof( exp(BigFloat(1)) ) == BigFloat though. There are no common functions that return Irrational values -- Irrationals are used to develop values rather than used as a fully supported computational type.

All 3 comments

I would really like to work on this issue.

So according to me there could be two things wrong:
1 Difference in declaration of e and other irrationals not throwing error.
2 Difference in interaction of e and other irrationals with roundup(since bigfloat works perfectly without roundup)

Can someone please guide me to a pointer explaining the declaration of e?
Also kindly tell me what method that gets called when we do bigfloat roundup with say pi?
Also why is e an Irrational and exp(1) a Float64?

Any other information relevant to this issue and solving it will be really helpful.

BigFloats as a type wrap the _MPFR_ library MPFR docs. See mpfr.jl for how Julia does this. MPFR exports four arbitrary precision constants (search their docs for "_mpfr_const_"): log2, pi, euler, catalan. This explains why your pass/fail rounding test passes using π, γ, catalan.

Looking at the way irrationals are defined irrationals.jl and how the irrational constants in mathconstants.jl are given gives you a starting point in determining what needs be done to support ℯ and φ properly. Balance that part of it with the specifics of rounding in mpfr.jl to get a better sense of what to modify and how to approach solving this issue.

Why is ℯ an irrational known to Base.MathConstants? It made the cut :)
Why is exp(1) a Float64? All transcendental math functions return Float64 values when the argument to the function is a Float64 or an Int. typeof( exp(BigFloat(1)) ) == BigFloat though. There are no common functions that return Irrational values -- Irrationals are used to develop values rather than used as a fully supported computational type.

Was this page helpful?
0 / 5 - 0 ratings