ruby / ruby/bigdecimal

BigMath methods returns inaccurate value for rational

Open
#444 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
152
Forks
95
Avg merge
4d 1h
Merged PRs (30d)
10

Description

Because Rational is simply converted to BigDecimal with the given precision. Precision is not that simple.
Though it may not be a responsibility for BigMath.

BigMath.exp: Maximum 19 digits precision loss

BigMath.exp(10**19/3r, 50).to_s[0,52]
#=> "0.31897971307307432918427098497346266668405585204999"
BigMath.exp(10**19/3r, 1000).to_s[0,52]
#=> "0.31897971307307432918427098497347329934115828786096"

BigMath.log: Many precision loss near x=1

BigMath.log(1+1r/12345**9, 50)
#=> 0.15016895257719999999999999999999999998872464284093e-36
BigMath.log(1+1r/12345**9, 1000).mult(1, 50)
#=> 0.1501689525772244244950394251097065861542145195459e-36

BigMath.sin: Many precision loss near x=n*π

x = (31415926535897932384626433832795 * 7 + 1).quo(7 * 10**40)
BigMath.sin(x, 50)
#=> 0.26542232205820974944592307816406286208998628034825e-40
BigMath.sin(x, 1000).mult(1, 50)
#=> 0.26542232248678117801735164959263429066141485177682e-40

Each method requires different Rational to BigDecimal conversion. For example:

  • log: x < 1/2r ? BigDecimal(x, prec) : BigDecimal(x - 1, prec) + 1
  • exp: BigDecimal(x, prec + [BigDecimal(x, 1).exponent, 0].max)
  • sqrt: Nothing special. BigDecimal(x, prec)
  • sin, cos: Needs precision increasing loop

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the BigMath.exp, log, sqrt, sin, and cos entry points, focusing on their Rational-to-BigDecimal conversion. Compare the precision-50 and precision-1000 examples in the issue to understand the demonstrated losses. Done means the reported Rational cases preserve the requested precision without the shown discrepancies.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.