Skip to content

Commit

Permalink
Define methods for big(T) (#21218)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano authored and ararslan committed May 8, 2017
1 parent 310b6dd commit 25f241c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,8 @@ end
float(z::Complex{<:AbstractFloat}) = z
float(z::Complex) = Complex(float(real(z)), float(imag(z)))

big(z::Complex{<:AbstractFloat}) = Complex{BigFloat}(z)
big(z::Complex{<:Integer}) = Complex{BigInt}(z)
big(::Type{Complex{T}}) where {T<:Real} = Complex{big(T)}
big(z::Complex{T}) where {T<:Real} = Complex{big(T)}(z)

## Array operations on complex numbers ##

Expand Down
5 changes: 4 additions & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor,
ndigits, promote_rule, rem, show, isqrt, string, powermod,
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, widen, signed, unsafe_trunc, trunc,
iszero
iszero, big

if Clong == Int32
const ClongMax = Union{Int8, Int16, Int32}
Expand Down Expand Up @@ -248,6 +248,9 @@ convert(::Type{Float16}, n::BigInt) = Float16(n,RoundNearest)

promote_rule(::Type{BigInt}, ::Type{<:Integer}) = BigInt

big(::Type{<:Integer}) = BigInt
big(::Type{<:Rational}) = Rational{BigInt}

# Binary ops
for (fJ, fC) in ((:+, :add), (:-,:sub), (:*, :mul),
(:fld, :fdiv_q), (:div, :tdiv_q), (:mod, :fdiv_r), (:rem, :tdiv_r),
Expand Down
1 change: 1 addition & 0 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ macro irrational(sym, val, def)
end

big(x::Irrational) = convert(BigFloat,x)
big(::Type{<:Irrational}) = BigFloat

## specific irrational mathematical constants

Expand Down
4 changes: 3 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import
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, rounding,
setrounding, maxintfloat, widen, significand, frexp, tryparse, iszero
setrounding, maxintfloat, widen, significand, frexp, tryparse, iszero, big

import Base.Rounding: rounding_raw, setrounding_raw

Expand Down Expand Up @@ -252,6 +252,8 @@ promote_rule(::Type{BigFloat}, ::Type{<:Real}) = BigFloat
promote_rule(::Type{BigInt}, ::Type{<:AbstractFloat}) = BigFloat
promote_rule(::Type{BigFloat}, ::Type{<:AbstractFloat}) = BigFloat

big(::Type{<:AbstractFloat}) = BigFloat

function convert(::Type{Rational{BigInt}}, x::AbstractFloat)
if isnan(x); return zero(BigInt)//zero(BigInt); end
if isinf(x); return copysign(one(BigInt),x)//zero(BigInt); end
Expand Down
19 changes: 19 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,22 @@ julia> factorial(big(21))
```
"""
factorial(x::Number) = gamma(x + 1) # fallback for x not Integer

"""
big(T::Type)
Compute the type that represents the numeric type `T` with arbitrary precision.
Equivalent to `typeof(big(zero(T)))`.
```jldoctest
julia> big(Rational)
Rational{BigInt}
julia> big(Float64)
BigFloat
julia> big(Complex{Int})
Complex{BigInt}
```
"""
big(::Type{T}) where {T<:Number} = typeof(big(zero(T)))
5 changes: 5 additions & 0 deletions test/bigfloat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ for T in [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt
@test big(2.0)^T(3) == 8
end

for x in (2f0, pi, 7.8, big(e))
@test big(typeof(x)) == typeof(big(x))
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
end

# issue 15659
@test (setprecision(53) do; big(1/3); end) < 1//3
4 changes: 4 additions & 0 deletions test/bigint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ end

@test typeof(BigInt(BigInt(1))) == BigInt

for x in (Int16(0), 1, 3//4, big(5//6), big(9))
@test big(typeof(x)) == typeof(big(x))
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
end

# Signed addition
@test a+Int8(1) == b
Expand Down

2 comments on commit 25f241c

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

Please sign in to comment.