Skip to content

Implement major math functions #336

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

tompng
Copy link
Member

@tompng tompng commented Jun 1, 2025

Implement these methods

BigMath.cbrt(x, prec)
BigMath.hypot(x, y, prec)
BigMath.tan(x, prec)
BigMath.asin(x, prec)
BigMath.acos(x, prec)
BigMath.atan2(y, x, prec)
BigMath.sinh(x, prec)
BigMath.cosh(x, prec)
BigMath.tanh(x, prec)
BigMath.asinh(x, prec)
BigMath.acosh(x, prec)
BigMath.atanh(x, prec)
BigMath.log2(x, prec)
BigMath.log10(x, prec)

Remaining methods

Math.singleton_methods - BigMath.singleton_methods
# => [:erf, :erfc, :gamma, :lgamma, :frexp, :ldexp]

@tompng tompng force-pushed the implement_major_math_functions branch from 1a93b02 to 5ebf5dc Compare June 8, 2025 06:30
@mrkn mrkn added this to the v3.4 milestone Jun 17, 2025
Calculate `tan(x, prec)` by `sin(x, prec) / cos(x, denominator_prec)`.
If the precision of cos is not enough, increase denominator_prec and recalculate cos again.
@tompng tompng force-pushed the implement_major_math_functions branch from 5ebf5dc to f29ba89 Compare June 20, 2025 18:45
@tompng tompng force-pushed the implement_major_math_functions branch from f29ba89 to 316d713 Compare June 20, 2025 18:56
return BigDecimal::NAN if x.nan?
return BigDecimal::INFINITY if x.infinite? == 1

prec2 = prec + BigDecimal.double_fig * 3 / 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why do we decide what prec2 is needed? It differs between the functions and I'm wondering how it's calculated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, BigMath functions calculate with prec+BigDecimal.double_fig precision.

I think BigMath.log2(1024, prec) is expected to return exactly 10. To calculate exact value for 2**n numbers, we need to round the result to precision = prec+BigDecimal.double_fig.
log(x)/log(2) should be calculated with prec + BigDecimal.double_fig + extra precision.
I decided to use extra=BigDeciaml.double_fig/2 here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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.

3 participants