Bug #4394

Bug in floating point multiplication

Added by Stefan Lembach over 4 years ago. Updated over 4 years ago.

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

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

Related to Ruby trunk - Bug #3746: Incorrect Float subtraction Rejected 08/26/2010

History

#1 Updated by Joshua Ballanco over 4 years ago

=begin
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

=end

#2 Updated by Yui NARUSE over 4 years ago

  • Status changed from Open to Rejected

#3 Updated by Roger Pack over 4 years ago

=begin
You can also do

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

Also available in: Atom PDF