Skip to content

Commit

Permalink
Make precision a keyword argument, improve handling of rounding mode. (
Browse files Browse the repository at this point in the history
…JuliaLang#29157)

* Improve handling of precision and rounding mode arguments in BigFloat constructors.

This makes a few changes to avoid unnecessarily calling `setrounding` and `setprecision`. It also changes `precision` to be a keyword argument (though does not yet deprecate the old behaviour).
  • Loading branch information
simonbyrne committed Nov 19, 2018
1 parent f55d517 commit 9a7c79c
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 218 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Language changes
----------------

* the constructor `BigFloat(::BigFloat)` now respects the global precision setting and always
returns a `BigFloat` with precision equal to `precision(BigFloat)` ([#29127]).
returns a `BigFloat` with precision equal to `precision(BigFloat)` ([#29127]). The optional
`precision` argument to override the global setting is now a keyword instead of positional
argument ([#29157]).
* Parser inputs ending with a comma are now consistently treated as incomplete.
Previously they were sometimes parsed as tuples, depending on whitespace ([#28506]).
* `Regex` now behave like a scalar when used in broadcasting ([#29913]).
Expand Down
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ function promote_eltype_op end
one(::CartesianIndex{N}) where {N} = one(CartesianIndex{N})
one(::Type{CartesianIndex{N}}) where {N} = CartesianIndex(ntuple(x -> 1, Val(N)))

MPFR.BigFloat(x, prec::Int) = BigFloat(x; precision=prec)
MPFR.BigFloat(x, prec::Int, rounding::RoundingMode) = BigFloat(x, rounding; precision=prec)
MPFR.BigFloat(x::Real, prec::Int) = BigFloat(x; precision=prec)
MPFR.BigFloat(x::Real, prec::Int, rounding::RoundingMode) = BigFloat(x, rounding; precision=prec)

# END 1.0 deprecations
12 changes: 8 additions & 4 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ macro irrational(sym, val, def)
esym = esc(sym)
qsym = esc(Expr(:quote, sym))
bigconvert = isa(def,Symbol) ? quote
function Base.BigFloat(::Irrational{$qsym})
c = BigFloat()
function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=MPFR.ROUNDING_MODE[]; precision=precision(BigFloat))
c = BigFloat(;precision=precision)
ccall(($(string("mpfr_const_", def)), :libmpfr),
Cint, (Ref{BigFloat}, Int32), c, MPFR.ROUNDING_MODE[])
Cint, (Ref{BigFloat}, MPFR.MPFRRoundingMode), c, r)
return c
end
end : quote
Base.BigFloat(::Irrational{$qsym}) = $(esc(def))
function Base.BigFloat(::Irrational{$qsym}; precision=precision(BigFloat))
setprecision(BigFloat, precision) do
$(esc(def))
end
end
end
quote
const $esym = Irrational{$qsym}()
Expand Down
Loading

0 comments on commit 9a7c79c

Please sign in to comment.