Strange Ruby issue with floating point multiplication

Does anyone have a solution to this problem in ruby:

let's say we have: a = 8.1999999

We wanted to round it by 2 decimal numbers, which is 8.20, and multiply it by 1,000,000 to become 8,200,000

We do this as follows:

(a.round(2) * 1000000).to_i

But what happened is 8199999, why?

Stressful things, we got the correct result if we multiply by 1000, 100000 or 10000000, but not 1,000,000. Does any body know why?

We are using ruby ​​1.9.2 and try with 1.9.3.

Thank!

+5
source share
3 answers

Whenever you get funky numbers in calculations use bigdecimal

require 'bigdecimal'
a = BigDecimal(8.1999999.to_s)
(a.round(2) * 1000000).to_i
+7
source

, a.round(2) , .

: (10 * a).round.to_i * 100000

+3

, 1 . , 8.2 . 8.2 irb #round(2), , 8.2, . , 8.2, .

. , , -8,2 , , , . : (a * 1000000).round

, , . ; .

.

x/2 n. , - x/(2 n * 5 m). 5 m , 2 n. , m == 0 , . , 1.25 , 5/(2 2 * 5 0), 0,1 , 1/(2 0 * 5 1). , 1.01.. 1.99 3 : 1,25, 1,50 1,75.

Since 8.2 does not have an exact representation, it repeats itself in binary forever, never adding exactly to 8.2. It goes on ad infinitum as 1100110011 ...


1. But note that you may need to a.round(1)use 2. Instead, the parameter #roundis the number of digits of the digits you want, not the number of significant digits. In this case, the result was the same, and it did not matter.

0
source

All Articles