-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_calculator.jl
More file actions
242 lines (182 loc) · 6.03 KB
/
error_calculator.jl
File metadata and controls
242 lines (182 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
module Err
include("functions.jl")
using .Functions
using Printf
neg_zeros = Dict{String, Any}()
neg_zeros["binary16"] = UInt16(0x8000)
neg_zeros["binary32"] = UInt32(0x80000000)
neg_zeros["binary64"] = UInt64(0x8000000000000000)
formats = Dict{String, Any}()
formats["binary16"] = Float16
formats["binary32"] = Float32
formats["binary64"] = Float64
uint_formats = Dict{Any, Any}()
uint_formats["binary16"] = UInt16
uint_formats["binary32"] = UInt32
uint_formats["binary64"] = UInt64
uint_formats[Float16] = UInt16
uint_formats[Float32] = UInt32
uint_formats[Float64] = UInt64
subn_mask = Dict{Any, Any}()
subn_mask[Float16] = UInt16(0x03FF)
subn_mask[Float32] = UInt32(0x007FFFFF)
subn_mask[Float64] = UInt64(0x000FFFFFFFFFFFFF)
"""
Calculate the error in ulps between a floating-point number y
and a BigFloat number z, and return it as a BigFloat number.
"""
function get_ulp_error(y::Union{Float16, Float32, Float64}, z::BigFloat)
d = abs(y - z)
wrap = typeof(y)
rn = convert(wrap, z)
ulp = eps(rn)
# Reduce ulp if a power of two was reached by rounding up.
if abs(rn) > abs(z) && (reinterpret(Err.uint_formats[wrap], rn) &
Err.subn_mask[wrap] == 0) && abs(rn) != floatmin(wrap)
ulp = ulp/2
end
return d / ulp
end
"""
Calculate how many floating-point values are in the provided range, inclusive.
"""
function number_of_floats_in_interval(start_float, end_float, format)
u_format = Err.uint_formats[format]
if sign(start_float) != sign(end_float)
return (reinterpret(u_format, abs(start_float))
+ reinterpret(u_format, abs(end_float)) + 1)
else
return ((max(reinterpret(u_format, start_float),
reinterpret(u_format, end_float)) -
min(reinterpret(u_format, start_float),
reinterpret(u_format, end_float))) + 1)
end
end
"""
Move n number of steps from the given float x.
"""
function nextfloatn(x, n, format)
u_format = Err.uint_formats[format]
neg_zero = Err.neg_zeros[format]
if (sign(x) == -1)
x_int = reinterpret(u_format, x) - u_format(n)
else
x_int = reinterpret(u_format, x) + u_format(n)
end
y = reinterpret(typeof(x), x_int)
if sign(x) != sign(y) && !iszero(x)
if sign(x) == 1
return typeof(x)(Inf)
else
x_int = neg_zero - x_int
y = reinterpret(typeof(x), x_int)
end
else
return y
end
end
"""
Given an input value x in one of the three floating-point formats,
calculate y, the approximation of the function for that format, and
z, the high precision correctly rounded variant in BigFloat (MPFR).
"""
function calculate_function(x, func, rounding, fastmath_on)
# Note: here the rounding mode could be changed before calling func, but
# Julia currently does not provide separate mathematical functions with
# different rounding modes.
if fastmath_on
y = @fastmath(getfield(Base.Math, Symbol(func))(x))
else
y = getfield(Base.Math, Symbol(func))(x)
end
if isinf(y)
@warn "Overflow in the matematical function output detected:\
$func. Skipping input $x."
return (0.0, y, NaN)
end
bigx = BigFloat(x)
z = getfield(Base.Math, Symbol(func))(bigx)
# Calculate the error in ulps.
error = get_ulp_error(y, z)
return (error, y, z)
end
"""
Go through every floating-point value in the provided range and evaluate
the maximum ulp error for the given function.
"""
function function_max_error_exhaustive(
func, format, rounding, fastmath_on, start_float, end_float)
max_error = 0.0
max_input = 0.0;
max_output = 0.0;
max_ref_out::BigFloat = 0.0;
number_of_tests = 0;
f_format = Err.formats[format]
x = start_float
while x <= end_float
(error, y, z) = calculate_function(x, Symbol(func), rounding, fastmath_on)
number_of_tests = number_of_tests + 1
# Update max error and corresponding values.
if error > max_error
max_error = error
max_input = x
max_output = y
max_ref_out = z
end
x = nextfloat(x);
end
return (max_error, max_input, max_output, max_ref_out, number_of_tests)
end
"""
Go through floating-point values in the provided range using a fixed-sized stepping
and evaluate the maximum ulp error for the given function.
"""
function function_max_error_fixed_step(
func, format, rounding, fastmath_on, start_float, end_float, tests_to_do)
max_error = 0.0
max_input = 0.0;
max_output = 0.0;
max_ref_out::BigFloat = 0.0;
u_format = Err.uint_formats[format]
f_format = Err.formats[format]
x = start_float
step_size = max(floor(number_of_floats_in_interval(x, end_float, format)/tests_to_do), 1);
while x <= end_float
(error, y, z) = calculate_function(x, Symbol(func), rounding, fastmath_on)
# Update max error and corresponding values.
if error > max_error
max_error = error
max_input = x
max_output = y
max_ref_out = z
end
x = nextfloatn(x, step_size, format)
end
return (max_error, max_input, max_output, max_ref_out, tests_to_do)
end
"""
Go through every floating-point value in the provided array and evaluate
the maximum ulp error for the given function.
"""
function function_max_error_special_inputs(
func, format, rounding, fastmath_on, input_set)
max_error = 0.0
max_input = 0.0;
max_output = 0.0;
max_ref_out::BigFloat = 0.0;
number_of_tests = 0;
f_format = Err.formats[format]
for x in input_set
(error, y, z) = calculate_function(x, Symbol(func), rounding, fastmath_on)
number_of_tests = number_of_tests + 1
# Update max error and corresponding values.
if error > max_error
max_error = error
max_input = x
max_output = y
max_ref_out = z
end
end
return (max_error, max_input, max_output, max_ref_out, number_of_tests)
end
end