Skip to content

Commit

Permalink
Add exhaustive tests for Float16 unary functions
Browse files Browse the repository at this point in the history
This tests every `Float16` value for the following functions. `sin,cos,tan,asin,acos,atan,sinh,cosh,tanh,asinh,acosh,atanh,exp,exp2,exp10,expm1,log,log2,log10,log1p,sqrt,cbrt`
Specifically, I think it is reasonable that any Float16 implementation in Base be accurate to .5 ULP since Float16 already is a very unstable type. This test takes about 5 seconds on my computer, so before merging, it would be a good idea to make sure this is acceptable for CI.
  • Loading branch information
oscardssmith authored Feb 9, 2021
1 parent cf7e1fc commit a985656
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ using Test

f = Float16(2.)
g = Float16(1.)

struct FloatIterator{T} end
function Base.iterate(it::FloatIterator{T}, el=T(-Inf)) where T
return el == T(Inf) ? nothing : (el, nextfloat(el))
end
Base.length(::FloatIterator{Float16}) = 2^16 - 2^11

@testset "comparisons" begin
@test f >= g
@test f > g
Expand Down Expand Up @@ -74,12 +81,12 @@ end
@testset "unary ops" begin
@test -f === Float16(-2.)
@test Float16(0.5f0)^2 Float16(0.5f0^2)
@test sin(f) sin(2f0)
@test log10(Float16(100)) == Float16(2.0)
@test sin(ComplexF16(f)) sin(complex(2f0))

# no domain error is thrown for negative values
@test cbrt(Float16(-1.0)) == -1.0
for func in (sin,cos,tan,asin,acos,atan,sinh,cosh,tanh,asinh,acosh,atanh,
exp,exp2,exp10,expm1,log,log2,log10,log1p,sqrt,cbrt)
for x in FloatIterator{Float16}()
@test try func(x) === Float16(func(widen(x))) catch; true end
end
end
end
@testset "binary ops" begin
@test f+g === Float16(3f0)
Expand Down

0 comments on commit a985656

Please sign in to comment.