https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112009-09-27T03:12:27ZRuby Issue Tracking SystemRuby master - Bug #2144: Split functionality of Float#inspect and Float#to_shttps://bugs.ruby-lang.org/issues/2144?journal_id=60312009-09-27T03:12:27Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>=begin<br>
Hi</p>
<ol>
<li>I believe the current trunk always return what I think you the "pretty float" style.</li>
</ol>
<p>0.1.to_s # ==> "0.1"</p>
<p>Note that 0.1 is not stored (on most platforms) as exactly 0.1 because it can't be expressed as fixnum * 2 ^ fixnum. Currently, this can be seen with 0.1.to_r which does not return 1/10 (see issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: 0.1.to_r not equal to (1/10) (Closed)" href="https://bugs.ruby-lang.org/issues/1408">#1408</a>)</p>
<ol start="2">
<li>I believe you are mistaken in thinking there is one and only one "accurate" representation of a given float number. It is important to remember that a Float is always an approximation.</li>
</ol>
<p>1.0 has to be understood as 1.0 +/- EPSILON, where the EPSILON is platform dependent. 1.0 is not more equal to 1 than to 1 + EPSILON/2. Indeed, there is no way to distinguish either when they are stored as floats.</p>
<p>To believe that Float#to_s loses data is wrong. If r.to_s returns "1.2", it implies that 1.2 is one of the values in the range of possible values for that floating number. It could have been 1.2000...0006. Or something else. There is no way to know, so #to_s chooses, wisely, to return the simplest value in the range.</p>
<p>Note also that the way a double is stored is platform dependent, at least in theory (I don't know of any that doesn't use the same one currently).</p>
<p>Finally, I'm closing this as this is not a bug report. If you really would like to have a string representation that is the closest approximation to the platform dependent representation of a given float, please open a feature request for it explaining why you would need this and how you would use this.<br>
Thanks</p>
<p>=end</p>