Project

General

Profile

Actions

Bug #7245

closed

Float.round result not as expected

Added by alexr (Alexander Rusa) over 11 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.3p286 (2012-10-12 revision 37164) [x86_64-darwin12.2.0]
Backport:
[ruby-core:48593]

Description

In my second example below I would expect "1.01" as result.

1.9.3-p286 :001 > 1.05.round(1)
=> 1.1
1.9.3-p286 :002 > 1.005.round(2)
=> 1.0
1.9.3-p286 :003 > 1.006.round(2)
=> 1.01

Is this a bug or am I wrong?

Updated by mame (Yusuke Endoh) over 11 years ago

  • Status changed from Open to Rejected

This is the spec.

Float is internally stored in binary representation which cannot represent 1.005 precisely.
So the literal "1.005" becomes an approximate Float object actually less than its mathematical value.

$ ruby -e 'p (1.005 * 3)'
3.0149999999999997

--
Yusuke Endoh

Actions

Also available in: Atom PDF

Like0
Like0