## Bug #4394

### Bug in floating point multiplication

**Description**

=begin

I stumbled upon the following odd rounding error in Float multiplication while using irb to calculate the tip for the pizza service ;-):

12.4 * 1.05 yields 13.020000000000001, but should be 13.02

=end

**Related issues**

### History

#### Updated by jballanc (Joshua Ballanco) almost 9 years ago

This is how floating point math works. See here for more info: http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

If you want exact decimal math, try:

ruby-1.9.2-p136 :001 > require 'bigdecimal' => true ruby-1.9.2-p136 :002 > (BigDecimal('12.4') * BigDecimal('1.05')).to_f => 13.02

#### Updated by naruse (Yui NARUSE) almost 9 years ago

**Status**changed from*Open*to*Rejected*

Learn floating point numbers.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

http://docs.sun.com/source/806-3568/ncg_goldberg.html

http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#floats_imprecise

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

#### Updated by rogerdpack (Roger Pack) almost 9 years ago

You can also do

(12.4 * 1.05).round(2)

=> 13.02

#### Updated by StoneBritanney (Britanney Stone) 5 months ago

**Description**updated (diff)

"Google":http://www.google.com/