BigMath methods returns inaccurate value for rational

未关闭
#444 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
tooling

调研方向

从 BigMath.exp、log、sqrt、sin 和 cos 入口开始,重点关注它们从 Rational 到 BigDecimal 的转换。比较 issue 中 precision-50 和 precision-1000 的示例,以了解所展示的精度损失。当报告的 Rational 案例能够保留所请求的精度,且不存在所展示的差异时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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
主要语言
C
星标
152
派生
99
平均合并
6 小时 44 分钟
30 天内合并 PR
2

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/bigdecimal 的其他 Issue

查看 ruby/bigdecimal 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。