Multiplication issue: dividing and multiplying by the same decimal does not return original number [duplicate]

喜夏-厌秋 提交于 2021-01-29 20:42:09

问题


I am aware of the Floating Point precision issues on multiple languages, but I thought I was only going to encounter those issues when multiplying very small or big numbers.

This simple math is incorrect

(byebug) 30*36.3/36.3
30.000000000000004

Why is this happening and what is the suggested way around it? I don't want to have to use the .to_i function since not always I will be dealing with integers, but I'm okay to rounding the result to up to 2 decimals.


回答1:


Well your sample of the problem does not cover even smallest part of the issue.

Depending on number of bits used to represent float number following operation

n = 1/3

n will be represented as 0.333333333333..... if you multiply it by 3 it never be 1 but 0.99999999999.....

For your sample you could use puts 30*(36.3/36.3) to get exact result.

Probably you already aware that operation with float numbers is more compute intensive that operation with integer numbers.

Otherwise if you would like to preserve precision then you have to use two part numbers -- numerator (top) and denominator (bottom) both integer numbers.

Create the object which will store numerator and denominator.

In such case 36.3 will be represented as 363 and 10

Now operate as you would operate on fractions (using integer numbers what make calculation much faster)

30.0 => 300, 10
36.3 => 363, 10

30.0*36.3/36.3 = 300/10*363/363*10/10
          = nominator(300*363*10)/denominator(10*363*10)
          = nominator(1089000)/denominator(36300)
          = Fraction.new(1089000,36300)

To simplify such calculations in future, create class Fraction and define methods for operators *,/,+,-,**,=.

a = Fraction.new(363,10)
b = Fraction.new(300,10)
c = Fraction.new(0,0)

c = a/b


来源:https://stackoverflow.com/questions/60083787/multiplication-issue-dividing-and-multiplying-by-the-same-decimal-does-not-retu

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