Skip to content

Commit

Permalink
Errors for custom AbstractFloats (#38401)
Browse files Browse the repository at this point in the history
* Errors for custom AbstractFloats

* Update test/math.jl

Co-authored-by: Daniel Karrasch <[email protected]>

* Update base/math.jl

Co-authored-by: Simon Byrne <[email protected]>

* Achieve concordance

* Disable tests

* Actually disable the tests without breaking docs

* Re enable tests

Co-authored-by: Daniel Karrasch <[email protected]>
Co-authored-by: Simon Byrne <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2020
1 parent 52883ca commit 7ef65ad
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
9 changes: 6 additions & 3 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ Accurately compute ``e^x-1``.
expm1(x)
expm1(x::Float64) = ccall((:expm1,libm), Float64, (Float64,), x)
expm1(x::Float32) = ccall((:expm1f,libm), Float32, (Float32,), x)
expm1(x::Real) = expm1(float(x))

"""
exp2(x)
Expand Down Expand Up @@ -401,8 +400,12 @@ julia> exp10(2)
"""
exp10(x::AbstractFloat) = 10^x

for f in (:sinh, :cosh, :tanh, :atan, :asinh, :exp, :expm1)
@eval ($f)(x::AbstractFloat) = error("not implemented for ", typeof(x))
for f in (:sin, :cos, :tan, :sinh, :cosh, :tanh, :atan, :acos, :asin, :asinh, :acosh, :atanh, :expm1, :log, :log1p)
@eval function ($f)(x::Real)
xf = float(x)
x === xf && throw(MethodError($f, (x,)))
return ($f)(xf)
end
end

# functions with special cases for integer arguments
Expand Down
6 changes: 5 additions & 1 deletion base/special/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ end
# The solution is to do the scaling back in 2 steps as just messing with the exponent wouldn't work.
for (func, base) in (:exp2=>Val(2), :exp=>Val(:ℯ), :exp10=>Val(10))
@eval begin
($func)(x::Real) = ($func)(float(x))
function ($func)(x::Real)
xf = float(x)
x === xf && throw(MethodError($func, (x,)))
return ($func)(xf)
end
function ($func)(x::T) where T<:Float64
N_float = muladd(x, LogBo256INV($base, T), MAGIC_ROUND_CONST(T))
N = reinterpret(uinttype(T), N_float) % Int32
Expand Down
6 changes: 0 additions & 6 deletions base/special/hyperbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ function sinh(x::T) where T<:Union{Float32,Float64}
E = exp(absx)
return copysign(T(.5)*(E - 1/E),x)
end
sinh(x::Real) = sinh(float(x))

COSH_SMALL_X(::Type{T}) where T= one(T)

Expand Down Expand Up @@ -127,7 +126,6 @@ function cosh(x::T) where T<:Union{Float32,Float64}
E = exp(absx)
return T(.5)*(E + 1/E)
end
cosh(x::Real) = cosh(float(x))

# tanh methods
TANH_LARGE_X(::Type{Float64}) = 44.0
Expand Down Expand Up @@ -162,7 +160,6 @@ function tanh(x::T) where T<:Union{Float32, Float64}
k = exp(abs2x)
return copysign(1 - 2/(k+1), x)
end
tanh(x::Real) = tanh(float(x))

# Inverse hyperbolic functions
AH_LN2(::Type{Float64}) = 6.93147180559945286227e-01
Expand Down Expand Up @@ -203,7 +200,6 @@ function asinh(x::T) where T <: Union{Float32, Float64}
end
return copysign(w, x)
end
asinh(x::Real) = asinh(float(x))

# acosh methods
@noinline acosh_domain_error(x) = throw(DomainError(x, "acosh(x) is only defined for x ≥ 1."))
Expand Down Expand Up @@ -242,7 +238,6 @@ function acosh(x::T) where T <: Union{Float32, Float64}
return log(x) + AH_LN2(T)
end
end
acosh(x::Real) = acosh(float(x))

# atanh methods
@noinline atanh_domain_error(x) = throw(DomainError(x, "atanh(x) is only defined for |x| ≤ 1."))
Expand Down Expand Up @@ -284,4 +279,3 @@ function atanh(x::T) where T <: Union{Float32, Float64}
end
return copysign(t, x)
end
atanh(x::Real) = atanh(float(x))
5 changes: 0 additions & 5 deletions base/special/log.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,3 @@ function log1p(x::Float32)
end
end

for f in (:log,:log1p)
@eval begin
($f)(x::Real) = ($f)(float(x))
end
end
6 changes: 0 additions & 6 deletions base/special/trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function sin(x::T) where T<:Union{Float32, Float64}
return -cos_kernel(y)
end
end
sin(x::Real) = sin(float(x))

# Coefficients in 13th order polynomial approximation on [0; π/4]
# sin(x) ≈ x + S1*x³ + S2*x⁵ + S3*x⁷ + S4*x⁹ + S5*x¹¹ + S6*x¹³
Expand Down Expand Up @@ -121,7 +120,6 @@ function cos(x::T) where T<:Union{Float32, Float64}
end
end
end
cos(x::Real) = cos(float(x))

const DC1 = 4.16666666666666019037e-02
const DC2 = -1.38888888888741095749e-03
Expand Down Expand Up @@ -230,7 +228,6 @@ function tan(x::T) where T<:Union{Float32, Float64}
return tan_kernel(y,-1)
end
end
tan(x::Real) = tan(float(x))

@inline tan_kernel(y::Float64) = tan_kernel(DoubleFloat64(y, 0.0), 1)
@inline function tan_kernel(y::DoubleFloat64, k)
Expand Down Expand Up @@ -449,7 +446,6 @@ function asin(x::T) where T<:Union{Float32, Float64}
t = (T(1.0) - absx)/2
return asin_kernel(t, x)
end
asin(x::Real) = asin(float(x))

# atan methods
ATAN_1_O_2_HI(::Type{Float64}) = 4.63647609000806093515e-01 # atan(0.5).hi
Expand Down Expand Up @@ -499,7 +495,6 @@ atan_q(w::Float32) = w*@horner(w, -1.9999158382f-01, -1.0648017377f-01)
atan_p(x², x⁴), atan_q(x⁴)
end

atan(x::Real) = atan(float(x))
function atan(x::T) where T<:Union{Float32, Float64}
# Method
# 1. Reduce x to positive by atan(x) = -atan(-x).
Expand Down Expand Up @@ -723,7 +718,6 @@ function acos(x::T) where T <: Union{Float32, Float64}
return T(2.0)*(df + (zRz*s + c))
end
end
acos(x::Real) = acos(float(x))

# multiply in extended precision
function mulpi_ext(x::Float64)
Expand Down
11 changes: 11 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1188,3 +1188,14 @@ end
@test (@inferred hypot(3, 4im)) === 5.0
@test (@inferred hypot(3, 4im, 12)) === 13.0
end

struct BadFloatWrapper <: AbstractFloat
x::Float64
end

@testset "not impelemented errors" begin
x = BadFloatWrapper(1.9)
for f in (sin, cos, tan, sinh, cosh, tanh, atan, acos, asin, asinh, acosh, atanh, exp, log1p, expm1, log) #exp2, exp10 broken for now
@test_throws MethodError f(x)
end
end

0 comments on commit 7ef65ad

Please sign in to comment.