0

Using ruby version 2.0.0p247 or 1.9.3p286

In irb console

9.99  * 100 = 999.0

and

29.99 * 100 = 2999.0

but

19.99 * 100 = 1998.9999999999998

Can anyone explain what is going on here? I get that it could be a bug in ruby core but surely the logic is the same for the above 3 calculations?

Ali
  • 56,466
  • 29
  • 168
  • 265

4 Answers4

5

Directly from the ruby Float documentation:

Float objects represent inexact real numbers using the native architecture's double-precision floating point representation.

Floating point has a different arithmetic and is an inexact number. So you should know its esoteric system. see following:

http://docs.sun.com/source/806-3568/ncg_goldberg.html
https://github.com/rdp/ruby_tutorials_core/wiki/ruby-talk-faq#floats_imprecise http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Example from the Ruby Tutorials Core

Example of odd behavior:

>> (2.0-1.1) == 0.9
=> false
RamC
  • 1,287
  • 1
  • 11
  • 15
bjhaid
  • 9,592
  • 2
  • 37
  • 47
5

In order to correctly calculate, make sure that you use the BigDecimal data type, particularly important for financial stuff.

19.99.to_d * 100.0.to_d
David Aldridge
  • 51,479
  • 8
  • 68
  • 96
1

To answer this specific question:

but surely the logic is the same for the above 3 calculations?

It is. But the stored value is not a directly copy of the decimal representation that you input as a Float literal in the console. Instead it is converted to an internal form. To understand what is going on, you need to see the internal binary representation used to store the values that you are manipulating. Then you will see it is 100% consistent, but not 100% precise.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
0

The result of multiplication of the Floating point number to Integer

19.99 * 100

is really:

# => 1998.9999999999998

Because of usage the 64-bit double-precision floating point to store the number, it is known issue of accuracy. However you can get the normalized float:

puts "%.1f" % (19.99 * 100)
# => 1999.0
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69