Skip to content

Commit 228afb2

Browse files
committed
add project.toml and tests
1 parent be15cb9 commit 228afb2

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

Project.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name = "FunctionAccuracyTests"
2+
uuid = "c7c8a6d5-64ef-4165-b05f-11d5e183af77"
3+
authors = ["Oscar Smith <oscardssmith@gmail.com>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
8+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9+
10+
[compat]
11+
Printf = "1"
12+
Test = "1"
13+
julia = "1"
14+
15+
[extras]
16+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
17+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18+
19+
[targets]
20+
test = ["Printf", "Test"]

src/FunctionAccuracyTests.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
module FunctionAccuracyTests
2+
13
using Base.Math: significand_bits, exponent_bias
24
using Test, Printf
35

6+
export FloatIterator, test_acc
7+
48
struct FloatIterator{T}<:AbstractVector{T}
59
min::T
610
max::T
711
FloatIterator{T}(min, max) where T = min > max ? error("max less than min") : new{T}(min,max)
812
end
913

14+
FloatIterator(min::T,max::T) where T = FloatIterator{T}(min,max)
1015
FloatIterator{T}() where T = FloatIterator{T}(T(-Inf),T(Inf))
1116
Base.iterate(it::FloatIterator) = (it.min, nextfloat(it.min))
1217
function Base.iterate(it::FloatIterator{T}, el) where T
@@ -96,6 +101,8 @@ function test_acc(fun_table::Dict, xx; tol=1.5, debug = true, tol_debug = 5)
96101
t = @test rmax <= tol
97102
end
98103
end
99-
test_acc(fun_table::Dict, xx; tol = 1.5, debug = true, tol_debug = 5) = test_acc(eltype(xx), fun_table, xx; tol = 1.5 debug = true, tol_debug = 5)
100-
test_acc(f::Function, xx; tol = 1.5 debug = true, tol_debug = 5) = test_acc(Dict(f=>f), xx; tol = 1.5 debug = true, tol_debug = 5)
101-
test_acc(f::Function, min, max; tol = 1.5 debug = true, tol_debug = 5) = test_acc(Dict(f=>f), FloatIterator(min,max); tol = 1.5 debug = true, tol_debug = 5)
104+
105+
test_acc(f::Function, xx; tol = 1.5, debug = true, tol_debug = 5) = test_acc(Dict(f=>f), xx; tol = 1.5, debug = true, tol_debug = 5)
106+
test_acc(f::Function, min, max; tol = 1.5, debug = true, tol_debug = 5) = test_acc(Dict(f=>f), FloatIterator(min,max); tol = 1.5, debug = true, tol_debug = 5)
107+
108+
end # module

test/runtests.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Printf, Test
2+
using FunctionAccuracyTests
3+
4+
MAX_EXP(n::Val{2}, ::Type{Float32}) = 128.0f0
5+
MAX_EXP(n::Val{2}, ::Type{Float64}) = 1024.0
6+
MAX_EXP(n::Val{:ℯ}, ::Type{Float32}) = 88.72284f0
7+
MAX_EXP(n::Val{:ℯ}, ::Type{Float64}) = 709.7827128933841
8+
MAX_EXP(n::Val{10}, ::Type{Float32}) = 38.53184f0
9+
MAX_EXP(n::Val{10}, ::Type{Float64}) = 308.25471555991675
10+
11+
# min_exp = T(-(exponent_bias(T)+significand_bits(T)) * log(base, big(2)))
12+
MIN_EXP(n::Val{2}, ::Type{Float32}) = -150.0f0
13+
MIN_EXP(n::Val{2}, ::Type{Float64}) = -1075.0
14+
MIN_EXP(n::Val{:ℯ}, ::Type{Float32}) = -103.97208f0
15+
MIN_EXP(n::Val{:ℯ}, ::Type{Float64}) = -745.1332191019412
16+
MIN_EXP(n::Val{10}, ::Type{Float32}) = -45.1545f0
17+
MIN_EXP(n::Val{10}, ::Type{Float64}) = -323.60724533877976
18+
19+
for (func, base) in (exp2=>Val(2), exp=>Val(:ℯ), exp10=>Val(10))
20+
for T in (Float32, Float64)
21+
xx = range(MIN_EXP(base,T), MAX_EXP(base,T), length = 10^6);
22+
test_acc(func, xx)
23+
end
24+
end
25+
26+
27+
exp10(Float16(-3.764))
28+
exp10(Float16(-1.76))
29+
asinh(Float16(258.5))
30+
asinh(Float16(-258.5))
31+
@testset "Float16" begin
32+
@testset "$func" for func in (atan,sinh,cosh,tanh,asinh,
33+
exp,exp2,exp10,expm1,cbrt)
34+
test_acc(func, -Inf16, Inf16, tol=.501)
35+
end
36+
@testset "$func" for func in (sin, cos, tan)
37+
test_acc(func, -floatmax(Float16),floatmax(Float16), tol=.501)
38+
end
39+
@testset "$func" for func in (asin, acos, atanh)
40+
test_acc(func, -one(Float16),one(Float16), tol=.501)
41+
end
42+
test_acc(acosh, one(Float16), Inf16, tol=.501)
43+
@testset "$func" for func in (log, log2, log10, sqrt)
44+
test_acc(func, zero(Float16), Inf16, tol=.501)
45+
end
46+
test_acc(log1p, -one(Float16), Inf16, tol=.501)
47+
end

0 commit comments

Comments
 (0)