Skip to content

Commit 2b3557a

Browse files
committed
=remove unrequired special casing for bitintegers
1 parent 9841943 commit 2b3557a

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed

base/special/gamma.jl

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,7 @@ this definition is equivalent to the Hurwitz zeta function
344344
``\\sum_{k=0}^\\infty (k+z)^{-s}``. For ``z=1``, it yields
345345
the Riemann zeta function ``\\zeta(s)``.
346346
"""
347-
zeta(s,z)
348-
349-
function zeta(s::Union{Int, ComplexOrReal{Float64}},
350-
z::ComplexOrReal{Float64})
347+
function zeta(s::ComplexOrReal{Float64}, z::ComplexOrReal{Float64})
351348
ζ = zero(promote_type(typeof(s), typeof(z)))
352349

353350
(z == 1 || z == 0) && return oftype(ζ, zeta(s))
@@ -611,10 +608,11 @@ end
611608

612609
# Converting types that we can convert, and not ones we can not
613610
# Float16, and Float32 and their Complex equivalents can be converted to Float64
614-
# and results converted back. Similar for BitIntegers (eg Int32), but no converting back.
611+
# and results converted back.
615612
# Otherwise, we need to make things use their own `float` converting methods
616613
# and in those cases, we do not convert back either as we assume
617614
# they also implement their own versions of the functions, with the correct return types.
615+
# This is the case for BitIntegers (which become `Float64` when `float`ed).
618616
# Otherwise, if they do not implement their version of the functions we
619617
# manually throw a `MethodError`.
620618
# This case occurs, when calling `float` on a type does not change its type,
@@ -627,22 +625,17 @@ end
627625
# Float16 version, by using a precomputed table look-up.
628626

629627

630-
631-
ofpromotedtype(as::Tuple, c) = convert(promote_type(typeof.(as)...), c)
632-
ComplexOrRealUnion(TS...) = Union{(ComplexOrReal{T} for T in TS)...}
633-
634-
const types_le_Float64 = (Float16, Float32, Float64, Base.BitInteger.types...)
635-
636-
for T in types_le_Float64
628+
for T in (Float16, Float32, Float64)
637629
@eval f64(x::Complex{$T}) = Complex128(x)
638630
@eval f64(x::$T) = Float64(x)
639631
end
640632

641633

642634
for f in (:digamma, :trigamma, :zeta, :eta, :invdigamma)
643635
@eval begin
644-
$f(z::ComplexOrRealUnion(Base.BitInteger.types...)) = $f(f64(z))
645-
$f(z::ComplexOrRealUnion(Float16,Float32)) = oftype(z, $f(f64(z)))
636+
function $f(z::Union{ComplexOrReal{Float16}, ComplexOrReal{Float32}})
637+
oftype(z, $f(f64(z)))
638+
end
646639

647640
function $f(z::Number)
648641
x = float(z)
@@ -653,41 +646,19 @@ for f in (:digamma, :trigamma, :zeta, :eta, :invdigamma)
653646
end
654647
end
655648

656-
function polygamma(m::Integer, z::ComplexOrRealUnion(Float16,Float32))
657-
oftype(z, polygamma(m, f64(z)))
658-
end
659649

660-
for T1 in types_le_Float64, T2 in types_le_Float64
661-
if (T1 == T2 == Float64) || (T1 == Int && T2 == Float64)
662-
continue # Avoid redefining base definition
663-
# However this skips `zeta(::Complex{Int}, ::ComplexOrReal{Float64})`
664-
# so that will need to be added back after
665-
end
666-
667-
if T1<:Integer && T2<:Integer
668-
@eval function zeta(s::ComplexOrReal{$T1}, z::ComplexOrReal{$T2})
669-
zeta(f64(s), f64(z)) #Do not promote down to Integers
670-
end
671-
else
672-
@eval function zeta(s::ComplexOrReal{$T1}, z::ComplexOrReal{$T2})
673-
ofpromotedtype((s, z), zeta(f64(s), f64(z)))
674-
end
675-
end
676-
end
650+
for T1 in (Float16, Float32, Float64), T2 in (Float16, Float32, Float64)
651+
(T1 == T2 == Float64) && continue # Avoid redefining base definition
677652

678-
# this is the one definition that is skipped
679-
function zeta(s::Complex{Int}, z::ComplexOrReal{Float64})::Complex{Float64}
680-
zeta(f64(s), f64(z))
681-
end
653+
@eval function zeta(s::ComplexOrReal{$T1}, z::ComplexOrReal{$T2})
654+
ζ = zeta(f64(s), f64(z))
682655

683-
function zeta(s::Integer, z::Number)
684-
t = Int(s) # One could worry here about converting a BigInteger into a Int32/Int64
685-
x = float(z)
686-
if typeof(t) === typeof(s) && typeof(x) === typeof(z)
687-
# There is nothing to fallback to, since this didn't work
688-
throw(MethodError(zeta,(s,z)))
689-
end
690-
zeta(t, x)
656+
if typeof(s) <: Complex || typeof(z) <: Complex
657+
convert(Complex{$T2}, ζ)
658+
else
659+
convert(typeof(z), ζ)
660+
end
661+
end
691662
end
692663

693664

@@ -701,6 +672,18 @@ function zeta(s::Number, z::Number)
701672
zeta(t, x)
702673
end
703674

675+
# It is safe to convert `s` to a float as underflow will occur before precision issues
676+
zeta(s::Integer, z::Number) = zeta(oftype(z, s), z)
677+
function zeta(s::Integer, z::Integer)
678+
x=float(z)
679+
zeta(oftype(x, s), x)
680+
end
681+
682+
683+
function polygamma(m::Integer, z::Union{ComplexOrReal{Float16}, ComplexOrReal{Float32}})
684+
oftype(z, polygamma(m, f64(z)))
685+
end
686+
704687

705688
function polygamma(m::Integer, z::Number)
706689
x = float(z)

test/math.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,6 @@ end
939939

940940
@test Base.Math.f64(complex(1f0,1f0)) == complex(1.0, 1.0)
941941
@test Base.Math.f64(1f0) == 1.0
942-
@test Base.Math.ofpromotedtype((complex(1f0, 1f0), 1.0), 5.0) == complex(5.0)
943942

944943
# no domain error is thrown for negative values
945944
@test invoke(cbrt, Tuple{AbstractFloat}, -1.0) == -1.0
@@ -962,8 +961,12 @@ end
962961
@test_throws MethodError zeta(1.0,big"2.0")
963962
@test_throws MethodError zeta(big"1.0",2.0)
964963

965-
@test typeof(zeta(complex(1),2.0)) == Complex{Float64}
964+
965+
@test typeof(zeta(complex(1), 2.0)) == Complex{Float64}
966966
@test typeof(polygamma(3, 0x2)) == Float64
967967
@test typeof(polygamma(big"3", 2f0)) == Float32
968-
@test typeof(zeta(big"1",2.0)) == Float64 #Is this really desirable behavour?
969-
968+
@test typeof(zeta(big"1", 2.0)) == Float64
969+
@test typeof(zeta(big"1", 2f0)) == Float32
970+
@test typeof(zeta(2f0, 2f0)) == Float32
971+
@test typeof(zeta(2f0, complex(2f0,0f0))) == Complex{Float32}
972+
@test typeof(zeta(complex(1,1), 2f0)) == Complex{Float32}

0 commit comments

Comments
 (0)