Issue #2152 has been updated by Marc-Andre Lafortune.


There are two different issues here.

1) First, there is currently a bug in trunk:

72.9.to_s # ==> "72.90000000000001"

This is plainly wrong. A float is an approximation and it is well-defined defined by how much it is an approximation.

Mathematically speaking, a float can be seen as a tiny range of real numbers; any of these numbers will be approximated to the same float (for example 72.9, 72.90000000000001 and 72.9000000000000000000042)

#to_s (and #inspect) should choose the simplest string representation that is included in the approximate range a float represents.

In other words, if string_rep_1.to_f == string_rep_2.to_f, then the simplest of string_rep_1 and string_rep_2 is to be preferred.

72.9.to_s must return "72.9", the simplest representation.

2) Calculations between floats introduce errors due to these approximations. For example:

(1-0.9) == 0.1 # ==> false

This feature request asks that (1-0.9).to_s  # => "0.1"

I object to this, because 1-0.9 is not == to 0.1 and thus should not have the same string representation.

I would recommend that instead we consider adding (probably in 1.9.3) an optional argument to Float#to_s that would limit the number of decimal showing, for instance.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/2152

----------------------------------------
http://redmine.ruby-lang.org