-
Notifications
You must be signed in to change notification settings - Fork 23
fix accuracy of logit
#99
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
e0347cc
51ed3c3
0a66f16
b407ae2
200ee6c
c896b88
0e33deb
e7e6226
a1eb57e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module ULPError | ||
export ulp_error, ulp_error_maximum | ||
function ulp_error(accurate::AbstractFloat, approximate::AbstractFloat) | ||
nsajko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# the ULP error is usually not required to great accuracy, so `Float32` should be precise enough | ||
zero_return = 0f0 | ||
inf_return = Inf32 | ||
# handle floating-point edge cases | ||
if !(isfinite(accurate) && isfinite(approximate)) | ||
accur_is_nan = isnan(accurate) | ||
approx_is_nan = isnan(approximate) | ||
if accur_is_nan || approx_is_nan | ||
return if accur_is_nan === approx_is_nan | ||
zero_return | ||
else | ||
inf_return | ||
end | ||
end | ||
if isinf(approximate) | ||
return if isinf(accurate) && (signbit(accurate) == signbit(approximate)) | ||
zero_return | ||
else | ||
inf_return | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case if isinan(accurate) || isnan(approximate)
return accur_is_nan === approx_is_nan ? zero_return : inf_return
elseif isinf(approximate)
return isinf(accurate) && (signbit(accurate) == signbit(approximate)) ? zero_return : inf_return
else
...
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed. Only the cases not handled correctly by the general formula below are handled specially here.
That's basically what the current situation is, except that the branches are wrapped into the top-level single branch ( |
||
acc = if accurate isa Union{Float16, Float32} | ||
# widen for better accuracy when doing so does not impact performance too much | ||
widen(accurate) | ||
else | ||
accurate | ||
end | ||
abs(Float32((approximate - acc) / eps(approximate))::Float32) | ||
end | ||
function ulp_error(accurate::Acc, approximate::App, x::AbstractFloat) where {Acc, App} | ||
acc = accurate(x) | ||
app = approximate(x) | ||
ulp_error(acc, app) | ||
end | ||
nsajko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function ulp_error(func::Func, x::AbstractFloat) where {Func} | ||
ulp_error(func ∘ BigFloat, func, x) | ||
end | ||
function ulp_error_maximum(func::Func, iterator) where {Func} | ||
function f(x::AbstractFloat) | ||
ulp_error(func, x) | ||
end | ||
maximum(f, iterator) | ||
end | ||
nsajko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
Uh oh!
There was an error while loading. Please reload this page.