Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby strange issue with floating point multiplication

Does anybody has solution for this problem in ruby :

let say we have : a = 8.1999999

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

We do it this way ;

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

But what we got is 8199999, why?

The strage things is, we got the correct result if we multiply by 1000, 100000, or 10000000, but not 1000000. Any body know why?

We are using ruby 1.9.2 and try with 1.9.3 as well.

Thanks!

like image 647
hudarsono Avatar asked Jul 12 '12 04:07

hudarsono


People also ask

Can float be multiplied?

The result of multiplying a float and an integer is always going to be a float. Copied! You can use the round() function if you need to round the result to N digits precision after the decimal point.

Can you multiply two floats?

Example: Multiply Two Floating-Point Numbers This ensures the numbers are float , otherwise they will be assigned - type double . first and second are then multiplied using the * operator and the result is stored in a new float variable product .

Is Ruby a float?

In other words, a float in a Ruby program is a number that contains a decimal point. Ruby will consider any number written without decimals as an integer (as in 138 ) and any number written with decimals as a float (as in 138.0 ).


2 Answers

Whenever you get funky numbers in calculations use bigdecimal

require 'bigdecimal'
a = BigDecimal(8.1999999.to_s)
(a.round(2) * 1000000).to_i
like image 74
victrnava Avatar answered Oct 17 '22 05:10

victrnava


It becomes like that because a.round(2) returns a floating point number, thus calculations are not perfect.

For correct result, try following: (10*a).round.to_i * 100000

like image 3
Johan Kotlinski Avatar answered Oct 17 '22 07:10

Johan Kotlinski