Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math tests: if fma is not available, relax some tests from exact equality to approximate equality #48102

Merged
merged 7 commits into from
Jan 6, 2023
Merged
39 changes: 32 additions & 7 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ function isnan_type(::Type{T}, x) where T
isa(x, T) && isnan(x)
end

# has_fma has no runtime support.
# So we need function wrappers to make this work.
has_fma_Int() = Core.Compiler.have_fma(Int)
has_fma_Float32() = Core.Compiler.have_fma(Float32)
has_fma_Float64() = Core.Compiler.have_fma(Float64)

has_fma = Dict(
Int => has_fma_Int(),
Float32 => has_fma_Float32(),
Float64 => has_fma_Float64(),
)

@testset "clamp" begin
@test clamp(0, 1, 3) == 1
@test clamp(1, 1, 3) == 1
Expand Down Expand Up @@ -482,17 +494,26 @@ end
@test_throws DomainError cospi(convert(T,Inf))
end
@testset "Check exact values" begin
# If the machine supports fused multiply add (fma), we require exact equality.
# Otherwise, we only require approximate equality.
if has_fma[T]
@info "On this machine, FMA is supported for $(T), so we will test for exact equality"
DilumAluthge marked this conversation as resolved.
Show resolved Hide resolved
my_eq = (==)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a different infix comparison operator to make this look nicer.

else
@info "On this machine, FMA is not supported for $(T), so we will test for approximate equality"
my_eq = isapprox
end
@test sind(convert(T,30)) == 0.5
@test cosd(convert(T,60)) == 0.5
@test sind(convert(T,150)) == 0.5
@test sinpi(one(T)/convert(T,6)) == 0.5
@test sincospi(one(T)/convert(T,6))[1] == 0.5
@test my_eq(sinpi(one(T)/convert(T,6)), 0.5)
@test my_eq(sincospi(one(T)/convert(T,6))[1], 0.5)
@test_throws DomainError sind(convert(T,Inf))
@test_throws DomainError cosd(convert(T,Inf))
T != Float32 && @test cospi(one(T)/convert(T,3)) == 0.5
T != Float32 && @test sincospi(one(T)/convert(T,3))[2] == 0.5
T == Rational{Int} && @test sinpi(5//6) == 0.5
T == Rational{Int} && @test sincospi(5//6)[1] == 0.5
T != Float32 && @test my_eq(cospi(one(T)/convert(T,3)), 0.5)
T != Float32 && @test my_eq(sincospi(one(T)/convert(T,3))[2], 0.5)
T == Rational{Int} && @test my_eq(sinpi(5//6), 0.5)
T == Rational{Int} && @test my_eq(sincospi(5//6)[1], 0.5)
end
end
scdm = sincosd(missing)
Expand Down Expand Up @@ -546,7 +567,11 @@ end
@test sinc(0.00099) ≈ 0.9999983878009009 rtol=1e-15
@test sinc(0.05f0) ≈ 0.9958927352435614 rtol=1e-7
@test sinc(0.0499f0) ≈ 0.9959091277049384 rtol=1e-7
@test cosc(0.14) ≈ -0.4517331883801308 rtol=1e-15
if has_fma[Float64]
@test cosc(0.14) ≈ -0.4517331883801308 rtol=1e-15
else
@test cosc(0.14) ≈ -0.4517331883801308 rtol=1e-14
end
@test cosc(0.1399) ≈ -0.45142306168781854 rtol=1e-14
@test cosc(0.26f0) ≈ -0.7996401373462212 rtol=5e-7
@test cosc(0.2599f0) ≈ -0.7993744054401625 rtol=5e-7
Expand Down