From b5bfd83a3d0ee55f27fbf18dfb9761a3f284fd99 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Mon, 22 Apr 2024 09:12:09 -0400 Subject: [PATCH] Add some hyperbolic function tests for Float16 (#54171) For `sinh` we have an untested codepath according to Coveralls. For others this is more fallback testing. --- test/math.jl | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/math.jl b/test/math.jl index 421c5387a522f..53468b76c16ae 100644 --- a/test/math.jl +++ b/test/math.jl @@ -1109,7 +1109,7 @@ 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)) @@ -1117,15 +1117,17 @@ import Base.Math: COSH_SMALL_X, H_SMALL_X, H_MEDIUM_X, H_LARGE_X @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) @@ -1133,15 +1135,17 @@ end @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)) @@ -1149,9 +1153,11 @@ end @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) @@ -1159,7 +1165,7 @@ end 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)) @@ -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))) @@ -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)