Ruby - Multiplication issue

人盡茶涼 提交于 2019-12-04 09:55:30

If you really want to round down to an integer then just

(3 * 2.32).to_i

but I think that's unlikely. Usually you just want to format the slightly imprecise floating point number to something like this

"%0.2f" % (3 * 2.32) 
=> "6.96"

If you really want to work with the exact representation then you can use BigDecimal.

require 'BigDecimal'
(3 * BigDecimal.new("2.32")).to_s("F")
=> "6.96"

PS. Recommended read http://floating-point-gui.de/ DS.

I cannot tell you about Ruby, so please forgive me. But frankly the principles stay the same, so I hope that'll help:

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2.32 * 3
6.959999999999999

As you can see, Python does the same.

System.out.println(2.32 * 3);
6.959999999999999

Java does the same! So what's wrong with it?

Let's try to move to BigDecimals:

System.out.println(new BigDecimal(2.32));
2.319999999999999840127884453977458178997039794921875

What you see here is what actually is meant by 2.32. It's just that in most cases print, or toString(), or whatever your language uses for converting floats to strings, rounds the numbers a bit.

The inexact value, due to the fact that floats are in fact binary-represented reals, not decimal ones—which, in turn, makes those binaries repeating, or recurring. And as you can guess, repeating real in a limited space won't ever be exact.

Actually, I was lying when I said the scary decimal above is what is meant by 2.32— the proper wording would be, "it's the closest decimal Java could get to approximate the actual value of 2.32".

Read more here: http://floating-point-gui.de/

Your problem is just that Floats are not precise:

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

That's not really language specific.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!