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

Refactor names of BigFloat precision functions #13232

Merged
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
12 changes: 11 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ Library improvements
Deprecated or removed
---------------------

* The following function names have been simplified and unified ([#13232]):

* `get_bigfloat_precision` -> `precision(BigFloat)`
* `set_bigfloat_precision` -> `setprecision`
* `with_bigfloat_precision` -> `setprecision`

* `get_rounding` -> `rounding`
* `set_rounding` -> `setrounding`
* `with_rounding` -> `setrounding`

* The method `A_ldiv_B!(SparseMatrixCSC, StrideVecOrMat)` has been deprecated
in favor of versions that require the matrix to be in factored form
([#13496]).
Expand Down Expand Up @@ -987,7 +997,7 @@ Deprecated or removed

* The `Stat` type is renamed `StatStruct` ([#4670]).

* `set_rounding`, `get_rounding` and `with_rounding` now take an additional
* `setrounding`, `rounding` and `setrounding` now take an additional
argument specifying the floating point type to which they apply. The old
behaviour and `[get/set/with]_bigfloat_rounding` functions are deprecated ([#5007]).

Expand Down
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,15 @@ for f in (:remotecall, :remotecall_fetch, :remotecall_wait)
end
end

# 13232
@deprecate with_bigfloat_precision setprecision
@deprecate set_bigfloat_precision(prec) setprecision(prec)
@deprecate get_bigfloat_precision() precision(BigFloat)

@deprecate set_rounding setrounding
@deprecate with_rounding setrounding
@deprecate get_rounding rounding

#13465
@deprecate cov(x::AbstractVector; corrected=true, mean=Base.mean(x)) covm(x, mean, corrected)
@deprecate cov(X::AbstractMatrix; vardim=1, corrected=true, mean=Base.mean(X, vardim)) covm(X, mean, vardim, corrected)
Expand Down
69 changes: 35 additions & 34 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1238,20 +1238,6 @@ For matrices or vectors $A$ and $B$, calculates $Aᴴ / B$
"""
Ac_rdiv_B

doc"""
```rst
.. set_rounding(T, mode)

Set the rounding mode of floating point type ``T``, controlling the
rounding of basic arithmetic functions (:func:`+`, :func:`-`, :func:`*`,
:func:`/` and :func:`sqrt`) and type conversion.

Note that this may affect other types, for instance changing the rounding
mode of ``Float64`` will change the rounding mode of ``Float32``. See
``get_rounding`` for available modes
```
"""
set_rounding

doc"""
linspace(start, stop, n=100)
Expand Down Expand Up @@ -1700,7 +1686,7 @@ julia> Float32(1/3, RoundUp)
0.33333334f0
```

See `get_rounding` for available rounding modes.
See `rounding` for available rounding modes.
"""
Float32

Expand Down Expand Up @@ -3122,7 +3108,7 @@ doc"""
.. round([T,] x, [digits, [base]], [r::RoundingMode])

``round(x)`` rounds ``x`` to an integer value according to the default
rounding mode (see :func:`get_rounding`), returning a value of the same type as
rounding mode (see :func:`rounding`), returning a value of the same type as
``x``. By default (:obj:`RoundNearest`), this will round to the nearest
integer, with ties (fractional values of 0.5) being rounded to the even
integer.
Expand Down Expand Up @@ -3854,7 +3840,7 @@ julia> Float64(pi, RoundUp)
3.1415926535897936
```

See `get_rounding` for available rounding modes.
See `rounding` for available rounding modes.
"""
Float64

Expand Down Expand Up @@ -6222,7 +6208,7 @@ isreadonly

doc"""
```rst
.. get_rounding(T)
.. rounding(T)

Get the current floating point rounding mode for type ``T``, controlling
the rounding of basic arithmetic functions (:func:`+`, :func:`-`,
Expand All @@ -6232,7 +6218,7 @@ Valid modes are ``RoundNearest``, ``RoundToZero``, ``RoundUp``,
``RoundDown``, and ``RoundFromZero`` (``BigFloat`` only).
```
"""
get_rounding
rounding

doc"""
```rst
Expand Down Expand Up @@ -6447,18 +6433,33 @@ Like `broadcast_function`, but for `broadcast!`.
broadcast!_function

doc"""
with_rounding(f::Function, T, mode)
```rst
.. setrounding(T, mode)

Set the rounding mode of floating point type ``T``, controlling the
rounding of basic arithmetic functions (:func:`+`, :func:`-`, :func:`*`,
:func:`/` and :func:`sqrt`) and type conversion.

Note that this may affect other types, for instance changing the rounding
mode of ``Float64`` will change the rounding mode of ``Float32``. See
``rounding`` for available modes
```
"""
setrounding(T, mode)

doc"""
setrounding(f::Function, T, mode)

Change the rounding mode of floating point type `T` for the duration of `f`. It is logically equivalent to:

old = get_rounding(T)
set_rounding(T, mode)
old = rounding(T)
setrounding(T, mode)
f()
set_rounding(T, old)
setrounding(T, old)

See `get_rounding` for available rounding modes.
See `rounding` for available rounding modes.
"""
with_rounding
setrounding(f::Function, T, mode)

doc"""
sleep(seconds)
Expand Down Expand Up @@ -7808,11 +7809,11 @@ Join path components into a full path. If some argument is an absolute path, the
joinpath

doc"""
get_bigfloat_precision()
precision(BigFloat)

Get the precision (in bits) currently used for `BigFloat` arithmetic.
"""
get_bigfloat_precision
precision(::Type{BigFloat})

doc"""
homedir() -> AbstractString
Expand Down Expand Up @@ -8441,16 +8442,16 @@ Compute $e^x$.
exp

doc"""
with_bigfloat_precision(f::Function,precision::Integer)
setprecision(f::Function, precision::Integer)

Change the `BigFloat` arithmetic precision (in bits) for the duration of `f`. It is logically equivalent to:

old = get_bigfloat_precision()
set_bigfloat_precision(precision)
old = precision(BigFloat)
setprecision(BigFloat, precision)
f()
set_bigfloat_precision(old)
setprecision(BigFloat, old)
"""
with_bigfloat_precision
setprecision

doc"""
searchindex(string, substring, [start])
Expand Down Expand Up @@ -9527,11 +9528,11 @@ Test whether a vector is in sorted order. The `by`, `lt` and `rev` keywords modi
issorted

doc"""
set_bigfloat_precision(x::Int64)
setprecision(x::Int64)

Set the precision (in bits) to be used to `BigFloat` arithmetic.
"""
set_bigfloat_precision
setprecision

doc"""
isbits(T)
Expand Down
9 changes: 3 additions & 6 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -912,12 +912,9 @@ export

# bigfloat & precision
precision,
get_bigfloat_precision,
set_bigfloat_precision,
with_bigfloat_precision,
get_rounding,
set_rounding,
with_rounding,
rounding,
setprecision,
setrounding,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

get_zero_subnormals,
set_zero_subnormals,

Expand Down
4 changes: 2 additions & 2 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ end
# This is necessary at least on 32-bit Intel Linux, since fma_llvm may
# have called glibc, and some broken glibc fma implementations don't
# properly restore the rounding mode
Rounding.set_rounding_raw(Float32, Rounding.JL_FE_TONEAREST)
Rounding.set_rounding_raw(Float64, Rounding.JL_FE_TONEAREST)
Rounding.setrounding_raw(Float32, Rounding.JL_FE_TONEAREST)
Rounding.setrounding_raw(Float64, Rounding.JL_FE_TONEAREST)
4 changes: 2 additions & 2 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ end
<(x::Float32, y::Irrational) = x <= Float32(y,RoundDown)
<(x::Irrational, y::Float16) = Float32(x,RoundUp) <= y
<(x::Float16, y::Irrational) = x <= Float32(y,RoundDown)
<(x::Irrational, y::BigFloat) = with_bigfloat_precision(precision(y)+32) do
<(x::Irrational, y::BigFloat) = setprecision(precision(y)+32) do
big(x) < y
end
<(x::BigFloat, y::Irrational) = with_bigfloat_precision(precision(x)+32) do
<(x::BigFloat, y::Irrational) = setprecision(precision(x)+32) do
x < big(y)
end

Expand Down
69 changes: 47 additions & 22 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ module MPFR

export
BigFloat,
get_bigfloat_precision,
set_bigfloat_precision,
with_bigfloat_precision,
bigfloat_str,
setprecision,
big_str

import
Expand All @@ -20,10 +17,10 @@ import
gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai,
eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, get_rounding,
set_rounding, maxintfloat, widen, significand, frexp, tryparse
cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, rounding,
setrounding, maxintfloat, widen, significand, frexp, tryparse

import Base.Rounding: get_rounding_raw, set_rounding_raw
import Base.Rounding: rounding_raw, setrounding_raw

import Base.GMP: ClongMax, CulongMax, CdoubleMax

Expand Down Expand Up @@ -51,7 +48,7 @@ type BigFloat <: AbstractFloat
exp::Clong
d::Ptr{Culong}
function BigFloat()
N = get_bigfloat_precision()
N = precision(BigFloat)
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL)
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &z, N)
finalizer(z, Base.GMP._mpfr_clear_func)
Expand Down Expand Up @@ -681,20 +678,33 @@ cmp(x::CdoubleMax, y::BigFloat) = -cmp(y,x)

signbit(x::BigFloat) = ccall((:mpfr_signbit, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0

function precision(x::BigFloat)
function precision(x::BigFloat) # precision of an object of type BigFloat
return ccall((:mpfr_get_prec, :libmpfr), Clong, (Ptr{BigFloat},), &x)
end

get_bigfloat_precision() = DEFAULT_PRECISION[end]
function set_bigfloat_precision(x::Int)
if x < 2
precision(::Type{BigFloat}) = DEFAULT_PRECISION[end] # precision of the type BigFloat itself

doc"""
setprecision(T, precision)

Set precision of type `T`.
"""
function setprecision(::Type{BigFloat}, precision::Int)
if precision < 2
throw(DomainError())
end
DEFAULT_PRECISION[end] = x
DEFAULT_PRECISION[end] = precision
end

doc"""
setprecision(precision)

Set the precision of `BigFloat`
"""
setprecision(precision::Int) = setprecision(BigFloat, precision)

maxintfloat(x::BigFloat) = BigFloat(2)^precision(x)
maxintfloat(::Type{BigFloat}) = BigFloat(2)^get_bigfloat_precision()
maxintfloat(::Type{BigFloat}) = BigFloat(2)^precision(BigFloat)

to_mpfr(::RoundingMode{:Nearest}) = Cint(0)
to_mpfr(::RoundingMode{:ToZero}) = Cint(1)
Expand All @@ -719,10 +729,11 @@ function from_mpfr(c::Integer)
RoundingMode(c)
end

get_rounding_raw(::Type{BigFloat}) = ROUNDING_MODE[end]
set_rounding_raw(::Type{BigFloat},i::Integer) = ROUNDING_MODE[end] = i
get_rounding(::Type{BigFloat}) = from_mpfr(get_rounding_raw(BigFloat))
set_rounding(::Type{BigFloat},r::RoundingMode) = set_rounding_raw(BigFloat,to_mpfr(r))
rounding_raw(::Type{BigFloat}) = ROUNDING_MODE[end]
setrounding_raw(::Type{BigFloat},i::Integer) = ROUNDING_MODE[end] = i

rounding(::Type{BigFloat}) = from_mpfr(rounding_raw(BigFloat))
setrounding(::Type{BigFloat},r::RoundingMode) = setrounding_raw(BigFloat,to_mpfr(r))

function copysign(x::BigFloat, y::BigFloat)
z = BigFloat()
Expand Down Expand Up @@ -813,16 +824,30 @@ eps(::Type{BigFloat}) = nextfloat(BigFloat(1)) - BigFloat(1)
realmin(::Type{BigFloat}) = nextfloat(zero(BigFloat))
realmax(::Type{BigFloat}) = prevfloat(BigFloat(Inf))

function with_bigfloat_precision(f::Function, precision::Integer)
old_precision = get_bigfloat_precision()
set_bigfloat_precision(precision)
doc"""
setprecision(f::Function, T, precision)

Set the precision of the type `T` for the duration of the function `f`.
Often used as `setprecision(T, precision) do ... end`
"""
function setprecision{T}(f::Function, ::Type{T}, prec::Integer)
old_prec = precision(T)
setprecision(T, prec)
try
return f()
finally
set_bigfloat_precision(old_precision)
setprecision(T, old_prec)
end
end

doc"""
setprecision(f::Function, precision::Integer)

Set the `BigFloat` precision for the duration of the function `f`.
Often used as `setprecision(precision) do ... end`
"""
setprecision(f::Function, precision::Integer) = setprecision(f, BigFloat, precision)

function string(x::BigFloat)
# In general, the number of decimal places needed to read back the number exactly
# is, excluding the most significant, ceil(log(10, 2^precision(x)))
Expand Down
2 changes: 1 addition & 1 deletion base/quadgk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function evalrule(f, a,b, x,w,gw, nrm)
return Segment(a, b, Ik, E)
end

rulekey(::Type{BigFloat}, n) = (BigFloat, get_bigfloat_precision(), n)
rulekey(::Type{BigFloat}, n) = (BigFloat, precision(BigFloat), n)
rulekey(T,n) = (T,n)

# Internal routine: integrate f over the union of the open intervals
Expand Down
Loading