Skip to content

Commit

Permalink
Add some hyperbolic function tests for Float16 (#54171)
Browse files Browse the repository at this point in the history
For `sinh` we have an untested codepath according to Coveralls. For
others this is more fallback testing.
  • Loading branch information
kshyatt authored Apr 22, 2024
1 parent 94b9a9a commit b5bfd83
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1109,57 +1109,63 @@ pcnfloat(x) = prevfloat(x), x, nextfloat(x)
import Base.Math: COSH_SMALL_X, H_SMALL_X, H_MEDIUM_X, H_LARGE_X

@testset "sinh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test sinh(zero(T)) === zero(T)
@test sinh(-zero(T)) === -zero(T)
@test sinh(nextfloat(zero(T))) === nextfloat(zero(T))
@test sinh(prevfloat(zero(T))) === prevfloat(zero(T))
@test sinh(T(1000)) === T(Inf)
@test sinh(-T(1000)) === -T(Inf)
@test isnan_type(T, sinh(T(NaN)))
for x in Iterators.flatten(pcnfloat.([H_SMALL_X(T), H_MEDIUM_X(T), H_LARGE_X(T)]))
@test sinh(x) sinh(big(x)) rtol=eps(T)
@test sinh(-x) sinh(big(-x)) rtol=eps(T)
if T (Float32, Float64)
for x in Iterators.flatten(pcnfloat.([H_SMALL_X(T), H_MEDIUM_X(T), H_LARGE_X(T)]))
@test sinh(x) sinh(big(x)) rtol=eps(T)
@test sinh(-x) sinh(big(-x)) rtol=eps(T)
end
end
end
end

@testset "cosh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test cosh(zero(T)) === one(T)
@test cosh(-zero(T)) === one(T)
@test cosh(nextfloat(zero(T))) === one(T)
@test cosh(prevfloat(zero(T))) === one(T)
@test cosh(T(1000)) === T(Inf)
@test cosh(-T(1000)) === T(Inf)
@test isnan_type(T, cosh(T(NaN)))
for x in Iterators.flatten(pcnfloat.([COSH_SMALL_X(T), H_MEDIUM_X(T), H_LARGE_X(T)]))
@test cosh(x) cosh(big(x)) rtol=eps(T)
@test cosh(-x) cosh(big(-x)) rtol=eps(T)
if T (Float32, Float64)
for x in Iterators.flatten(pcnfloat.([COSH_SMALL_X(T), H_MEDIUM_X(T), H_LARGE_X(T)]))
@test cosh(x) cosh(big(x)) rtol=eps(T)
@test cosh(-x) cosh(big(-x)) rtol=eps(T)
end
end
end
end

@testset "tanh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test tanh(zero(T)) === zero(T)
@test tanh(-zero(T)) === -zero(T)
@test tanh(nextfloat(zero(T))) === nextfloat(zero(T))
@test tanh(prevfloat(zero(T))) === prevfloat(zero(T))
@test tanh(T(1000)) === one(T)
@test tanh(-T(1000)) === -one(T)
@test isnan_type(T, tanh(T(NaN)))
for x in Iterators.flatten(pcnfloat.([H_SMALL_X(T), T(1.0), H_MEDIUM_X(T)]))
@test tanh(x) tanh(big(x)) rtol=eps(T)
@test tanh(-x) -tanh(big(x)) rtol=eps(T)
if T (Float32, Float64)
for x in Iterators.flatten(pcnfloat.([H_SMALL_X(T), T(1.0), H_MEDIUM_X(T)]))
@test tanh(x) tanh(big(x)) rtol=eps(T)
@test tanh(-x) -tanh(big(x)) rtol=eps(T)
end
end
end
@test tanh(18.0) tanh(big(18.0)) rtol=eps(Float64)
@test tanh(8.0) tanh(big(8.0)) rtol=eps(Float32)
end

@testset "asinh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test asinh(zero(T)) === zero(T)
@test asinh(-zero(T)) === -zero(T)
@test asinh(nextfloat(zero(T))) === nextfloat(zero(T))
Expand All @@ -1173,7 +1179,7 @@ end
end

@testset "acosh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test_throws DomainError acosh(T(0.1))
@test acosh(one(T)) === zero(T)
@test isnan_type(T, acosh(T(NaN)))
Expand All @@ -1184,7 +1190,7 @@ end
end

@testset "atanh" begin
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
@test_throws DomainError atanh(T(1.1))
@test atanh(zero(T)) === zero(T)
@test atanh(-zero(T)) === -zero(T)
Expand Down

0 comments on commit b5bfd83

Please sign in to comment.