-
Notifications
You must be signed in to change notification settings - Fork 285
Improve precision of sine and cosine library models #8743
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: develop
Are you sure you want to change the base?
Conversation
Handle special values (0, ±π/2, ±π, ±3π/2, ±2π) with precise ranges and and use quadrant-based range narrowing to reduce over-approximation. Fixes: diffblue#6999
|
|
||
| // cos(0) should be exactly 1 | ||
| c = cos(0.0); | ||
| assert(c >= 0.999999999 && c <= 1.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compare with 1, as suggested in the comment.
|
|
||
| // cos(0) should be exactly 1 | ||
| c = cos(0.0); | ||
| assert(c >= 0.999999999 && c <= 1.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this allows around 22 bits difference so only a bit more accurate than float. I would recommend a tighter bound.
| c = cos(0.0); | ||
| assert(c >= 0.999999999 && c <= 1.0); | ||
|
|
||
| // cos(π/2) should be approximately 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I would try computing each of these using something like CORE-MATH and then adding an error in the range of a few ULP. Happy to give this a go if useful.
|
|
||
| // ========== COSINE TESTS ========== | ||
|
|
||
| // Test 7: cos(0) ≈ 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These just repeat the above.
| // Test 13: sinf with special values | ||
| { | ||
| float s0 = sinf(0.0f); | ||
| float s_pi_half = sinf(3.14159265f / 2.0f); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File is called cosf, but tests sinf.
| ^VERIFICATION SUCCESSFUL$ | ||
| -- | ||
| ^warning: ignoring | ||
| ^warning: ignoring |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spurious newline change
|
|
||
| // Test range narrowing for [-π/2, π/2] | ||
| c = cos(M_PI / 4.0); // 45 degrees | ||
| assert(c >= 0.0 && c <= 1.0); // Should be in [0, 1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ^SIGNAL=0$ | ||
| ^VERIFICATION SUCCESSFUL$ | ||
| -- | ||
| ^warning: ignoring No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spurious newline change
| ^VERIFICATION SUCCESSFUL$ | ||
| -- | ||
| ^warning: ignoring | ||
| ^warning: ignoring |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spurious newline change
| do | ||
| // sin(0) should be exactly 0 | ||
| s = sin(0.0); | ||
| assert(s >= -1e-10 && s <= 1e-10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
|
|
||
| // ========== COSINE TESTS ========== | ||
|
|
||
| // Test 7: cos(0) ≈ 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a duplicate of line 10.
| double x; | ||
| __CPROVER_assume(x >= -M_PI / 2.0 + 0.1 && x <= M_PI / 2.0 - 0.1); | ||
| double c = cos(x); | ||
| assert(c >= 0.0 && c <= 1.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, these can be tightened quite a bit.
|
|
||
| // Handle exact special values | ||
| // sin(0) = 0 | ||
| if(x >= -epsilon && x <= epsilon) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would handle the special case 0 in addition to that.
martin-cs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some pedantry. Hope it is helpful.
| const long double two_pi = 6.28318530717958647692L; | ||
|
|
||
| // For small epsilon for floating-point comparisons | ||
| const long double epsilon = 1e-12L; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this number?
|
|
||
| // For small epsilon for floating-point comparisons | ||
| const long double epsilon = 1e-12L; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that it would be possible to do a range reduction here and it might get you more accurate results.
Handle special values (0, ±π/2, ±π, ±3π/2, ±2π) with precise ranges and and use quadrant-based range narrowing to reduce over-approximation.
Fixes: #6999