Skip to content

Commit 22d4d84

Browse files
giordanoararslan
authored andcommitted
More efficient float(T) for rationals and big*, works with Irrational (#21219)
1 parent 86cf0db commit 22d4d84

File tree

6 files changed

+18
-1
lines changed

6 files changed

+18
-1
lines changed

base/complex.jl

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ promote_rule(::Type{Complex{T}}, ::Type{Complex{S}}) where {T<:Real,S<:Real} =
3333

3434
widen(::Type{Complex{T}}) where {T} = Complex{widen(T)}
3535

36+
float(::Type{Complex{T}}) where {T<:AbstractFloat} = Complex{T}
37+
float(::Type{Complex{T}}) where {T} = Complex{float(T)}
38+
3639
"""
3740
real(z)
3841

base/float.jl

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ Float64
272272
```
273273
"""
274274
float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))
275+
float(::Type{T}) where {T<:AbstractFloat} = T
275276

276277
for Ti in (Int8, Int16, Int32, Int64)
277278
@eval begin

base/irrationals.jl

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ convert(::Type{Rational{BigInt}}, x::Irrational) = throw(ArgumentError("Cannot c
3737
end
3838
end
3939

40+
float(::Type{<:Irrational}) = Float64
41+
4042
==(::Irrational{s}, ::Irrational{s}) where {s} = true
4143
==(::Irrational, ::Irrational) = false
4244

base/mpfr.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import
1010
Base: (*), +, -, /, <, <=, ==, >, >=, ^, ceil, cmp, convert, copysign, div,
1111
exp, exp2, exponent, factorial, floor, fma, hypot, isinteger,
1212
isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf,
13-
nextfloat, prevfloat, promote_rule, rem, rem2pi, round, show,
13+
nextfloat, prevfloat, promote_rule, rem, rem2pi, round, show, float,
1414
sum, sqrt, string, print, trunc, precision, exp10, expm1,
1515
gamma, lgamma, log1p,
1616
eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
@@ -118,6 +118,8 @@ end
118118
convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x)
119119
convert(::Type{AbstractFloat}, x::BigInt) = BigFloat(x)
120120

121+
float(::Type{BigInt}) = BigFloat
122+
121123
# generic constructor with arbitrary precision:
122124
"""
123125
BigFloat(x, prec::Int)

base/rational.jl

+2
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,5 @@ iszero(x::Rational) = iszero(numerator(x))
429429
function lerpi(j::Integer, d::Integer, a::Rational, b::Rational)
430430
((d-j)*a)/d + (j*b)/d
431431
end
432+
433+
float{T<:Integer}(::Type{Rational{T}}) = float(T)

test/floatfuncs.jl

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ for elty in (Float32,Float64)
6868
@test round.(elty2,A) == fill(round(elty2,x),(10,10,10))
6969
end
7070
end
71+
72+
@testset "Types" begin
73+
for x in (Int16(0), 1, 2f0, pi, 3//4, big(5//6), 7.8, big(9), big(e))
74+
@test float(typeof(x)) == typeof(float(x))
75+
@test float(typeof(complex(x, x))) == typeof(float(complex(x, x)))
76+
end
77+
end

0 commit comments

Comments
 (0)