Skip to content

Commit dd31084

Browse files
authored
fix rounding mode in construction of BigFloat from pi (#55911)
The default argument of the method was outdated, reading the global default rounding directly, bypassing the `ScopedValue` stuff.
1 parent 03f8523 commit dd31084

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

base/irrationals.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function irrational(sym, val, def)
216216
esym = esc(sym)
217217
qsym = esc(Expr(:quote, sym))
218218
bigconvert = isa(def,Symbol) ? quote
219-
function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=MPFR.ROUNDING_MODE[]; precision=precision(BigFloat))
219+
function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=Rounding.rounding_raw(BigFloat); precision=precision(BigFloat))
220220
c = BigFloat(;precision=precision)
221221
ccall(($(string("mpfr_const_", def)), :libmpfr),
222222
Cint, (Ref{BigFloat}, MPFR.MPFRRoundingMode), c, r)

test/rounding.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,28 @@ end
470470
@test prevfloat(f) < i
471471
end
472472
end
473+
474+
@testset "π to `BigFloat` with `setrounding`" begin
475+
function irrational_to_big_float(c::AbstractIrrational)
476+
BigFloat(c)
477+
end
478+
479+
function irrational_to_big_float_with_rounding_mode(c::AbstractIrrational, rm::RoundingMode)
480+
f = () -> irrational_to_big_float(c)
481+
setrounding(f, BigFloat, rm)
482+
end
483+
484+
function irrational_to_big_float_with_rounding_mode_and_precision(c::AbstractIrrational, rm::RoundingMode, prec::Int)
485+
f = () -> irrational_to_big_float_with_rounding_mode(c, rm)
486+
setprecision(f, BigFloat, prec)
487+
end
488+
489+
for c (π, MathConstants.γ, MathConstants.catalan)
490+
for p 1:40
491+
@test (
492+
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundDown, p) < c <
493+
irrational_to_big_float_with_rounding_mode_and_precision(c, RoundUp, p)
494+
)
495+
end
496+
end
497+
end

0 commit comments

Comments
 (0)