Bug #922
Float == is false when should be true
| Status: | Rejected | Start date: | 12/23/2008 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 0% |
|
| Category: | - | |||
| Target version: | Ruby 1.8.7 | |||
| ruby -v: |
Description
In irb on Ruby 1.8.6 (ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]) and 1.8.7 (ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]) >> 896.5 * 0.83 => 744.095 >> 896.5 * 0.83 == 744.095 => false
History
Updated by Eric Hodel about 3 years ago
On Dec 22, 2008, at 19:55 PM, Amanda Nicholas wrote: > Bug #922: Float == is false when should be true > http://redmine.ruby-lang.org/issues/show/922 > > Author: Amanda Nicholas > Status: Open, Priority: Normal > Target version: Ruby 1.8.7 > > In irb on Ruby 1.8.6 (ruby 1.8.6 (2007-09-24 patchlevel 111) [i486- > linux]) and 1.8.7 (ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]) > >>> 896.5 * 0.83 > => 744.095 >>> 896.5 * 0.83 == 744.095 > => false They're not the same, Ruby truncates the true value for display purposes: irb(main):001:0> a = 896.5* 0.83 => 744.095 irb(main):002:0> b = 744.095 => 744.095 irb(main):004:0> "%0.26f %0.26f" % [a, b] => "744.09499999999991359800333157 744.09500000000002728484105319" See also: http://en.wikipedia.org/wiki/Floating_point
Updated by Dave B about 3 years ago
This might help, also: http://whynotwiki.com/Ruby_/_Numbers#Comparing_Floating-Point_Numbers
Updated by Kurt Stephens about 3 years ago
Perhaps Ruby should do what Common Lisp implementations have been doing:
http://portal.acm.org/citation.cfm?id=989431
http://kurtstephens.com/files/p372-steele.pdf
ftp://netlib.bell-labs.com/netlib/fp/gdtoa.tgz
Perhaps the default Float#to_s formatting should not throw away necessary precision, so that:
some_float == eval("#{some_float}")
is always true.
Updated by Yukihiro Matsumoto about 3 years ago
- Status changed from Open to Rejected
OP is not a bug at all. We will consider about Float#to_s later. Thank you for the info, Kurt.