Skip to content

Commit

Permalink
make BigFloat(x::BigFloat) respect global precision (fix #20766) (#29127
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rfourquet authored and simonbyrne committed Oct 26, 2018
1 parent 665a69d commit c887c74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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]).
* Parser inputs ending with a comma are now consistently treated as incomplete.
Previously they were sometimes parsed as tuples, depending on whitespace ([#28506]).

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 c887c74

Please sign in to comment.