Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some irrationals can't be converted to BigFloat using a rounding mode #34103

Open
Kolaru opened this issue Dec 14, 2019 · 3 comments
Open

Some irrationals can't be converted to BigFloat using a rounding mode #34103

Kolaru opened this issue Dec 14, 2019 · 3 comments
Labels
domain:maths Mathematical functions kind:bug Indicates an unexpected problem or unintended behavior

Comments

@Kolaru
Copy link

Kolaru commented Dec 14, 2019

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
@StefanKarpinski StefanKarpinski added kind:bug Indicates an unexpected problem or unintended behavior domain:maths Mathematical functions labels Dec 14, 2019
@kamalojasv181
Copy link
Contributor

I would really like to work on this issue.

@kamalojasv181
Copy link
Contributor

kamalojasv181 commented Jan 26, 2020

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.

@JeffreySarnoff
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:maths Mathematical functions kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants