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

BigFloat constructors with given precision and/or rounding mode #17217

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
48 changes: 45 additions & 3 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ type BigFloat <: AbstractFloat
sign::Cint
exp::Clong
d::Ptr{Limb}

function BigFloat()
N = precision(BigFloat)
prec = precision(BigFloat)
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL)
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &z, N)
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &z, prec)
finalizer(z, cglobal((:mpfr_clear, :libmpfr)))
return z
end
# Not recommended for general use

# Not recommended for general use:
function BigFloat(prec::Clong, sign::Cint, exp::Clong, d::Ptr{Void})
new(prec, sign, exp, d)
end
Expand Down Expand Up @@ -118,6 +120,46 @@ end
convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x)
convert(::Type{AbstractFloat}, x::BigInt) = BigFloat(x)

# generic constructor with arbitrary precision:
"""
BigFloat(x, prec::Int)

Create a representation of `x` as a `BigFloat` with precision `prec`.
"""
function BigFloat(x, prec::Int)
setprecision(BigFloat, prec) do
BigFloat(x)
end
end

"""
BigFloat(x, prec::Int, rounding::RoundingMode)

Create a representation of `x` as a `BigFloat` with precision `prec` and rounding mode `rounding`.
"""
function BigFloat(x, prec::Int, rounding::RoundingMode)
setrounding(BigFloat, rounding) do
BigFloat(x, prec)
end
end

"""
BigFloat(x, rounding::RoundingMode)

Create a representation of `x` as a `BigFloat` with the current global precision and rounding mode `rounding`.
"""
function BigFloat(x::Union{Integer, AbstractFloat, String}, rounding::RoundingMode)
BigFloat(x, precision(BigFloat), rounding)
end

"""
BigFloat(x::String)

Create a representation of the string `x` as a `BigFloat`.
"""
BigFloat(x::String) = parse(BigFloat, x)


## BigFloat -> Integer
function unsafe_cast(::Type{Int64}, x::BigFloat, ri::Cint)
ccall((:__gmpfr_mpfr_get_sj,:libmpfr), Cintmax_t,
Expand Down
9 changes: 9 additions & 0 deletions doc/src/stdlib/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,20 @@ The `BigFloat` type implements arbitrary-precision floating-point arithmetic usi

```@docs
Base.precision
Base.MPFR.precision(::Type{BigFloat})
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be listed here twice? Doesn't the line before this cause all methods to be documented?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently the first does not list all methods: http://docs.julialang.org/en/latest/stdlib/numbers.html#Base.precision

Base.MPFR.setprecision
```

### Additional constructors for `BigFloat`
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure this needs to be a separate section from the group right before it

```@docs
Base.MPFR.BigFloat(x, prec::Int)
BigFloat(x::Union{Integer, AbstractFloat, String}, rounding::RoundingMode)
Base.MPFR.BigFloat(x, prec::Int, rounding::RoundingMode)
Base.MPFR.BigFloat(x::String)
```
## Random Numbers


Random number generation in Julia uses the [Mersenne Twister library](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/#dSFMT)
via `MersenneTwister` objects. Julia has a global RNG, which is used by default. Other RNG types
can be plugged in by inheriting the `AbstractRNG` type; they can then be used to have multiple
Expand Down
23 changes: 23 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,3 +873,26 @@ let b = IOBuffer()
end

@test isnan(sqrt(BigFloat(NaN)))

# PR 17217 -- BigFloat constructors with given precision and rounding mode

# test constructors and `big` with additional precision and rounding mode:

for prec in (10, 100, 1000)
for val in ("3.1", pi, "-1.3", 3.1)
let
a = BigFloat(val)
b = BigFloat(val, prec)
c = BigFloat(val, RoundUp)
d = BigFloat(val, prec, RoundDown)
e = BigFloat(val, prec, RoundUp)

@test precision(a) == precision(BigFloat)
@test precision(b) == prec
@test precision(c) == precision(BigFloat)
@test precision(d) == prec
@test precision(e) == prec
(val != 3.1) && @test e > d # rounding has no effect when constructing from Float64
end
end
end