Skip to content

Commit

Permalink
fix rounding mode in construction of BigFloat from pi (#55911)
Browse files Browse the repository at this point in the history
The default argument of the method was outdated, reading the global
default rounding directly, bypassing the `ScopedValue` stuff.
  • Loading branch information
nsajko authored Oct 1, 2024
1 parent 03f8523 commit dd31084
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function irrational(sym, val, def)
esym = esc(sym)
qsym = esc(Expr(:quote, sym))
bigconvert = isa(def,Symbol) ? quote
function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=MPFR.ROUNDING_MODE[]; precision=precision(BigFloat))
function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=Rounding.rounding_raw(BigFloat); precision=precision(BigFloat))
c = BigFloat(;precision=precision)
ccall(($(string("mpfr_const_", def)), :libmpfr),
Cint, (Ref{BigFloat}, MPFR.MPFRRoundingMode), c, r)
Expand Down
25 changes: 25 additions & 0 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,28 @@ end
@test prevfloat(f) < i
end
end

@testset "π to `BigFloat` with `setrounding`" begin
function irrational_to_big_float(c::AbstractIrrational)
BigFloat(c)
end

function irrational_to_big_float_with_rounding_mode(c::AbstractIrrational, rm::RoundingMode)
f = () -> irrational_to_big_float(c)
setrounding(f, BigFloat, rm)
end

function irrational_to_big_float_with_rounding_mode_and_precision(c::AbstractIrrational, rm::RoundingMode, prec::Int)
f = () -> irrational_to_big_float_with_rounding_mode(c, rm)
setprecision(f, BigFloat, prec)
end

for c (π, MathConstants.γ, MathConstants.catalan)
for p 1:40
@test (
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundDown, p) < c <
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundUp, p)
)
end
end
end

0 comments on commit dd31084

Please sign in to comment.