Skip to content

Commit 25f241c

Browse files
giordanoararslan
authored andcommitted
Define methods for big(T) (#21218)
1 parent 310b6dd commit 25f241c

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

base/complex.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ end
880880
float(z::Complex{<:AbstractFloat}) = z
881881
float(z::Complex) = Complex(float(real(z)), float(imag(z)))
882882

883-
big(z::Complex{<:AbstractFloat}) = Complex{BigFloat}(z)
884-
big(z::Complex{<:Integer}) = Complex{BigInt}(z)
883+
big(::Type{Complex{T}}) where {T<:Real} = Complex{big(T)}
884+
big(z::Complex{T}) where {T<:Real} = Complex{big(T)}(z)
885885

886886
## Array operations on complex numbers ##
887887

base/gmp.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor,
99
ndigits, promote_rule, rem, show, isqrt, string, powermod,
1010
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
1111
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, widen, signed, unsafe_trunc, trunc,
12-
iszero
12+
iszero, big
1313

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

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

251+
big(::Type{<:Integer}) = BigInt
252+
big(::Type{<:Rational}) = Rational{BigInt}
253+
251254
# Binary ops
252255
for (fJ, fC) in ((:+, :add), (:-,:sub), (:*, :mul),
253256
(:fld, :fdiv_q), (:div, :tdiv_q), (:mod, :fdiv_r), (:rem, :tdiv_r),

base/irrationals.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ macro irrational(sym, val, def)
131131
end
132132

133133
big(x::Irrational) = convert(BigFloat,x)
134+
big(::Type{<:Irrational}) = BigFloat
134135

135136
## specific irrational mathematical constants
136137

base/mpfr.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import
1616
eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
1717
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
1818
cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, rounding,
19-
setrounding, maxintfloat, widen, significand, frexp, tryparse, iszero
19+
setrounding, maxintfloat, widen, significand, frexp, tryparse, iszero, big
2020

2121
import Base.Rounding: rounding_raw, setrounding_raw
2222

@@ -252,6 +252,8 @@ promote_rule(::Type{BigFloat}, ::Type{<:Real}) = BigFloat
252252
promote_rule(::Type{BigInt}, ::Type{<:AbstractFloat}) = BigFloat
253253
promote_rule(::Type{BigFloat}, ::Type{<:AbstractFloat}) = BigFloat
254254

255+
big(::Type{<:AbstractFloat}) = BigFloat
256+
255257
function convert(::Type{Rational{BigInt}}, x::AbstractFloat)
256258
if isnan(x); return zero(BigInt)//zero(BigInt); end
257259
if isinf(x); return copysign(one(BigInt),x)//zero(BigInt); end

base/number.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,22 @@ julia> factorial(big(21))
211211
```
212212
"""
213213
factorial(x::Number) = gamma(x + 1) # fallback for x not Integer
214+
215+
"""
216+
big(T::Type)
217+
218+
Compute the type that represents the numeric type `T` with arbitrary precision.
219+
Equivalent to `typeof(big(zero(T)))`.
220+
221+
```jldoctest
222+
julia> big(Rational)
223+
Rational{BigInt}
224+
225+
julia> big(Float64)
226+
BigFloat
227+
228+
julia> big(Complex{Int})
229+
Complex{BigInt}
230+
```
231+
"""
232+
big(::Type{T}) where {T<:Number} = typeof(big(zero(T)))

test/bigfloat.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ for T in [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt
77
@test big(2.0)^T(3) == 8
88
end
99

10+
for x in (2f0, pi, 7.8, big(e))
11+
@test big(typeof(x)) == typeof(big(x))
12+
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
13+
end
14+
1015
# issue 15659
1116
@test (setprecision(53) do; big(1/3); end) < 1//3

test/bigint.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ end
5757

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

60+
for x in (Int16(0), 1, 3//4, big(5//6), big(9))
61+
@test big(typeof(x)) == typeof(big(x))
62+
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
63+
end
6064

6165
# Signed addition
6266
@test a+Int8(1) == b

0 commit comments

Comments
 (0)