Project

General

Profile

Actions

Bug #4394

closed

Bug in floating point multiplication

Added by philomath (Stefan Lembach) almost 14 years ago. Updated over 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
[ruby-core:35222]

Description

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


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #3746: Incorrect Float subtractionRejected08/26/2010Actions
Actions #1

Updated by jballanc (Joshua Ballanco) almost 14 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 
Actions #2

Updated by naruse (Yui NARUSE) almost 14 years ago

  • Status changed from Open to Rejected
Actions #3

Updated by rogerdpack (Roger Pack) almost 14 years ago

You can also do

(12.4 * 1.05).round(2)
=> 13.02

Updated by StoneBritanney (Britanney Stone) over 5 years ago

  • Description updated (diff)

Updated by vslot-vgammer (vslot-vgammer vslot-vgammer) about 4 years ago

  • Tracker changed from Bug to Feature
  • Project changed from Ruby master to 14
  • Subject changed from Bug in floating point multiplication to Здесь Вас ждут большие выигрыши!
  • Description updated (diff)
  • Target version deleted (1.9.2)
  • ruby -v deleted (ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux])
Actions #6

Updated by hsbt (Hiroshi SHIBATA) almost 4 years ago

  • Tracker changed from Feature to Bug
  • Project changed from 14 to Ruby master
  • Subject changed from Здесь Вас ждут большие выигрыши! to Bug in floating point multiplication
  • Description updated (diff)
  • ruby -v set to ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
  • Backport set to 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN

Updated by BillyTooda (BillyTooda BillyTooda) almost 4 years ago

  • File 6.gif added
Actions #8

Updated by hsbt (Hiroshi SHIBATA) almost 4 years ago

  • File deleted (6.gif)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0