The problem is that some of the numbers, which we can easily write in decimal form, do not have an exact representation in a certain floating-point format implemented by the current equipment. A random way of saying this is that all integers, but not all fractions, because we usually store a fraction with an exponent of 2**e . So, you have 3 options:
To complete. An unlimited result is always really very close, so the rounded result is invariably “perfect”. This is what Javascript does, and many people don’t even realize that JS does everything in a floating point.
Use fixed point arithmetic. Ruby really makes this very easy; it is one of the only languages that seamlessly moves to the Fixignum Bignum class as numbers get larger.
Use a class designed to solve this problem, such as BigDecimal
In order to examine the problem in more detail, we can try to present your "7.3" in binary format. Part 7 is easy, 111, but how do we do it .3? 111.1 is 7.5, too large; 111.01 is 7.25, approaching. It turns out that 111.010011 is the "next nearest lower number", 7.296875, and when we try to fill in the missing .003125, in the end we find out that it is just 111.010011001100110011 ... forever, not represented in our chosen encoding in the final bit string.
Digitaloss
source share