Actions
Bug #4394
closedBug in floating point multiplication
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
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
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
Updated by naruse (Yui NARUSE) almost 14 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 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])
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
Like0
Like0Like0Like0Like0Like0Like0Like0Like0