Repository navigation
Fix complex asin, acos, tan, tanh and log1p mismatches with std - #1456
DiamonDinoia merged 2 commits into
Conversation
Fix xtensor-stack#1432: asin/acos/asinh/acosh use Kahan's formulas, tan/tanh use FreeBSD's ctanh, log1p keeps the sign of a -0 imaginary part, and atan2(+-0, +-0) no longer returns NaN. Add tests with every input from the issue. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
|
All failing CI jobs except one use libc++, whose trigonometric functions use different method and are less accurate than the current xsimd for large The one exception is clang-cl x64 /fp:fast. The test hits |
|
We can change the tests to be accurate to machine precision multiplied by the conditioning of the problem. Once we do that we can just measure the relative error point-wise to that accuracy. fast-math is best effort user that enable it know that they cannot distinguish inf from a large value. |
|
I do not like testing against generated data from python. |
|
Also, can you check the license of the code you took? I do not want to violate any of them. We can just implement the textbook algorithm for these things and fit the coefficients with Remez. |
DiamonDinoia
left a comment
There was a problem hiding this comment.
Over engineered tests :)
Licences for FreeBSD are BSD-2 and for Julia it is MIT. I will add the Licences and credit on top of each function in the next commit. |
27bf679 to
1208d75
Compare
Check asin, acos, asinh, acosh, tan, tanh and log1p on the inputs of issue 1432 that gave a wrong value or a wrong sign of zero before the fix: signed zeros, inputs where the sign of a zero imaginary part decides the sign of the result, points near the poles of tan and tanh, and large |z|. The expected values are the exact results rounded to double, written in the tests. A result passes if it is within 4 * cond * eps of the expected value, where cond is the condition number |z f'(z) / f(z)|. Infinite values and the sign of zeros must match; with fast-math, infinite values are skipped. Cite the papers whose formulas complex asin and tanh use (Kahan 1987; Hull, Fairgrieve and Tang 1997) instead of the Julia and FreeBSD code. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
1208d75 to
ac774c8
Compare
DiamonDinoia
left a comment
There was a problem hiding this comment.
since there are citations I do not need to restate the equations. Also I can read the equations from the code itself.
I tried to keep the comments to minimum and only explain math and why some steps are necessary and why is it done this way to make it easier to review (and also myself to keep track of what is going on). I tried to keep them to minimum but if you think they are too much i can remove them. But in my opinion it makes the code more clear and it is not too much comment. |
Fixes #1432.
Supersedes #1434, #1435, #1436, #1437, #1438, #1439, #1440.
The implementations are mostly taken from FreeBSD implementation (scalar, converted to simd version) and some from Julia. Main idea is that we always try to sum up variables that have the same sign to avoid cancellations and in some cases we check for some special fixed bounds so that the term won't become Inf. I also added tests, that tests for the inputs mentioned in #1432.
Performance: ns per element, master vs this PR
Lower is faster; the last column of each architecture is PR / master (below 1 means the PR is faster).
asinasinacosacosasinhasinhacoshacoshtantantanhtanhlog1plog1patan2atan2-O3 -DNDEBUG.