-
Notifications
You must be signed in to change notification settings - Fork 22
add logabstanh
#86
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
add logabstanh
#86
Changes from 2 commits
e8d075a
528545d
5cf0058
95639d1
3364d20
0b6d9f9
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 | ||||
---|---|---|---|---|---|---|
|
@@ -149,6 +149,31 @@ end | |||||
""" | ||||||
$(SIGNATURES) | ||||||
|
||||||
Return `log(abs(tanh(x)))`, carefully evaluated without intermediate calculation of `tanh(x)`. | ||||||
|
||||||
The implementation ensures `logabstanh(-x) = logabstanh(x)`. | ||||||
""" | ||||||
function logabstanh(x::Real) | ||||||
return log1p(-2/((exp(2abs(x))+1))) | ||||||
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. As indicated by the special cases close to 0 for Float32 and Float64, maybe a safer default would be
Suggested change
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. I think the version I wrote stays more accurate overall but I'll do a test 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. I don't think it's possible to do such an analysis? You can't realistically test any subtype of 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. for any |
||||||
end | ||||||
function logabstanh(x::Float32) | ||||||
abs_x = abs(x) | ||||||
if abs_x < 0.0625f0 | ||||||
return log(abs_x) - x*x*(1f0/3) | ||||||
oscardssmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end | ||||||
return log1p(-2/((exp(2abs_x)+1))) | ||||||
oscardssmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end | ||||||
function logabstanh(x::Float64) | ||||||
abs_x = abs(x) | ||||||
if abs_x < 0x1p-5 | ||||||
return log(abs_x) + evalpoly(x*x, (0, -1/3, 7/90, -62/2835)) | ||||||
end | ||||||
return log1p(-2/((exp(2abs_x)+1))) | ||||||
oscardssmith marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end | ||||||
|
||||||
""" | ||||||
$(SIGNATURES) | ||||||
|
||||||
Return `log(1+x^2)` evaluated carefully for `abs(x)` very small or very large. | ||||||
""" | ||||||
log1psq(x::Real) = log1p(abs2(x)) | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.