Repository navigation
Conversation
`1 - cos(x)` loses significant digits as `cos(x)` approaches one: the relative error is about `2e-16/x^2`, so `versin(1e-6)` has 4 correct digits, `versin(1e-7)` has 3, and below `|x| ~ 1.5e-8` the result is exactly `0` (e.g., `versin(1e-10)` returns `0` instead of `5e-21`). Use the half-angle identities `versin(x) = 2*sin(x/2)^2` and `haversin(x) = sin(x/2)^2`, which are within 2 ULP everywhere. Regenerate the fixtures in extended precision and add fixtures for small `|x|`.
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
5 of 7 tasks
Member
|
@anandkaranubc Mind taking a look at this one, as well? Same comments as #15611 (comment). |
- document the half-angle method in the JSDoc and C doc comments instead of an inline comment - use `abs2` to square - generate fixtures from the textbook formula `1 - cos(x)` evaluated at 4096 bits, rather than from the identity used by the implementation, and separate comments from expressions in the Julia runners
Contributor
Author
|
Same changes as in #15611, in 814681b: a |
6 of 7 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request:
math/base/special/versinandmath/base/special/haversin(JS and C) for arguments near multiples of2*pi, including small|x|.Both functions evaluate
1 - cos(x). Whencos(x)is close to one, the subtraction cancels, so the relative error is about2e-16 / x^2, and below|x| ~ 1.5e-8the result is exactly0. This is the region the functions exist for: the haversine formula is used in place of the law of cosines precisely becausesin^2(x/2)stays accurate for small angles.The fix uses the exact half-angle identities:
versin(x) = 2*sin(x/2)^2haversin(x) = sin(x/2)^2x/2is exact, andsinis accurate over the whole range (including hugex, where it does its own argument reduction), so both are within 2 ULP of a 256-bit reference everywhere I tested, from|x| = 1e-300to2^1000*pi/2. Ondevelop, the error near the zeros is unbounded.Related Issues
No.
Questions
No.
Other
1.0 .- cos.( x )formula, so they recorded the cancellation and the tests compared them withstrictEqual. The runners now evaluate the same textbook formula,1 - cos(x), at 4096 bits and round toFloat64, so the fixtures are independent of the identity the implementation uses. The existing ranges are unchanged, and there are newsmall_negative.jsonandsmall_positive.jsonfixtures (|x|log-spaced over[1e-300, 1]), which the old fixtures didn't cover at all. The fixture tests now allow 2 ULP, and the worst case is 2 ULP.test.jsandtest.native.jsfor both packages (32,005 assertions each).vercosandhavercoshave the same problem near odd multiples ofpi.1 + cos(x)is off by up to 2.4 million ULP there, and2*cos(x/2)^2fixes it the same way. The single-precision variants (versinf,haversinf, ...) do too. I'll send those separately to keep this PR small.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written primarily by Claude Code. The bug was found by comparing every
stats/base/distsfunction against SciPy on random parameters.@stdlib-js/reviewers