Skip to content

Commit

Permalink
Improve handling of precision and rounding mode arguments in BigFloat…
Browse files Browse the repository at this point in the history
… 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 Sep 12, 2018
1 parent 7142712 commit 567b152
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 176 deletions.
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 567b152

Please sign in to comment.