Ruby 数学运算的精度问题
您知道如何以数学精度解决以下问题吗?
p RUBY_VERSION # => "1.9.1"
p 0.1%1 # => 0.1
p 1.1%1 # => 0.1
p 90.0%1 # => 0.0
p 90.1%1 # => 0.0999999999999943
p 900.1%1 # => 0.100000000000023
p RUBY_VERSION # => "1.9.2"
p 0.1%1 # => 0.1
p 1.1%1 # => 0.10000000000000009
p 90.0%1 # => 0.0
p 90.1%1 # => 0.09999999999999432
p 900.1%1 # => 0.10000000000002274
Do you know how to fix the following issue with math precision?
p RUBY_VERSION # => "1.9.1"
p 0.1%1 # => 0.1
p 1.1%1 # => 0.1
p 90.0%1 # => 0.0
p 90.1%1 # => 0.0999999999999943
p 900.1%1 # => 0.100000000000023
p RUBY_VERSION # => "1.9.2"
p 0.1%1 # => 0.1
p 1.1%1 # => 0.10000000000000009
p 90.0%1 # => 0.0
p 90.1%1 # => 0.09999999999999432
p 900.1%1 # => 0.10000000000002274
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大十进制
正如人所说;
然而,我使用 BigDecimal 类。引用它的简介
举一个例子;
正如您所看到的,确保良好的精度是可能的,但需要付出一些努力。
Big Decimal
As the man said;
I have however had great success using the BigDecimal class. To quote its intro
Taking one of your examples;
As you can see, ensuring decent precision is possible but it requires a little bit of effort.
所有计算机语言都是如此,而不仅仅是 Ruby。这是在二进制计算机上表示浮点数的功能:
每个计算机科学家应该知道的内容关于浮点运算
This is true of all computer languages, not just Ruby. It's a feature of representing floating point numbers on binary computers:
What Every Computer Scientist Should Know About Floating Point Arithmetic
将
0.1
写入浮点总是会导致舍入错误。如果您想要“精确”的十进制表示形式,则应使用Decimal
类型。Writing
0.1
into a floating point will always result in rounding errors. If you want 'precise' decimal representation, you should use theDecimal
type.