Issue with precision of Ruby math operations

后端 未结 3 506
心在旅途
心在旅途 2021-01-23 22:18

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 9         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 22:53

    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

提交回复
热议问题