Skip to content

Commit

Permalink
make BigFloat(x::BigFloat) respect global precision (fix #20766)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Oct 4, 2018
1 parent 60cff80 commit fc7f70a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ New language features
Language changes
----------------

* the constructor `BigFloat(::BigFloat)` now respects the global precision setting and always
returns a `BigFloat` with precision equal to `precision(BigFloat)` ([#29127]).


Standard Library Changes
------------------------
Expand Down
12 changes: 11 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ BigFloat(x)
widen(::Type{Float64}) = BigFloat
widen(::Type{BigFloat}) = BigFloat

BigFloat(x::BigFloat) = x
function BigFloat(x::BigFloat)
if precision(BigFloat) == precision(x)
x
else
z = BigFloat()
ccall((:mpfr_set, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32),
z, x, ROUNDING_MODE[])
z
end
end


# convert to BigFloat
for (fJ, fC) in ((:si,:Clong), (:ui,:Culong))
Expand Down
14 changes: 14 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ import Base.MPFR

@test typeof(BigFloat(1//1)) == BigFloat
@test typeof(BigFloat(one(Rational{BigInt}))) == BigFloat

# BigFloat constructor respects global precision when not specified
let prec = precision(BigFloat) < 16 ? 256 : precision(BigFloat) ÷ 2
xs = Real[T(1) for T in (Int, BigInt, Float32, Float64, BigFloat)]
f = xs[end]
@test BigFloat(f) === f # no-op when precision of operand is the same as global's one
setprecision(prec) do
@test precision(xs[end]) != prec
for x in xs
@test precision(BigFloat(x)) == prec
@test precision(BigFloat(x, prec ÷ 2)) == prec ÷ 2
end
end
end
end
@testset "basic arithmetic" begin
tol = 1e-12
Expand Down

0 comments on commit fc7f70a

Please sign in to comment.