Skip to content

Fix precision of BigMath.sin(x,prec) and BigMath.cos(x,prec) for large x #346

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

Merged
merged 1 commit into from
Jun 10, 2025

Conversation

tompng
Copy link
Member

@tompng tompng commented Jun 8, 2025

For large x, sin and cos is calculated:

sin(x) = sin(x % (2*PI))
cos(x) = cos(x % (2*PI))

When x is large, PI(prec) is not precise enough to calculate x%(2*PI). Extra precision is needed.

To calculate BigMath.sin(31415926535, 4), 31415926535 % (2 * PI) == 5.385285... needs prec=4.
31415926535 % (2 * 3.141) == 1.969... is not enough.
31415926535 % (2 * 3.1415926535) == 0 is even not enough.
31415926535 % (2 * 3.14159265358979) == 5.385 is OK
Precision of PI needs to be >= prec + x.exponent

For large x, sin and cos is calculated:
sin(x) = sin(x % (2*PI))
cos(x) = cos(x % (2*PI))
When x is large, PI(prec) is not precise enough to calculate `x%(2*PI)`. Extra precision is needed.
@tompng tompng force-pushed the sin_cos_large_x_precision branch from 4d94676 to 9746083 Compare June 8, 2025 18:09
@tompng tompng merged commit f8928e2 into ruby:master Jun 10, 2025
78 checks passed
@tompng tompng deleted the sin_cos_large_x_precision branch June 10, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant