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

make BigFloat(x::BigFloat) respect global precision (fix #20766) #29127

Merged
merged 2 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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