https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112011-09-09T18:24:48ZRuby Issue Tracking SystemRuby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=206962011-09-09T18:24:48Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>This seems not a bug:<br>
printf "%.40f\n", 2.5e-22 #=> 0.0000000000000000000002499999999999999769</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=206992011-09-09T22:26:59Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Additional to say, 2.5e-22 express the range<br>
from 0.0000000000000000000002499999999999999534<br>
to__ 0.0000000000000000000002500000000000000003</p>
<p>The center of them is 0.0000000000000000000002499999999999999769.<br>
So of course 0.0000000000000000000002_499999999999999769.round(22) will be 2.0e-22.</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=207002011-09-09T22:29:10Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>One more additional to say, those arguments depend on IEEE 754.</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=207102011-09-10T10:00:08Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Open</i></li></ul><p>Hi,</p>
<p>Yui NARUSE wrote:</p>
<blockquote>
<p>Additional to say, 2.5e-22 express the range<br>
from 0.0000000000000000000002499999999999999534<br>
to__ 0.0000000000000000000002500000000000000003</p>
<p>The center of them is 0.0000000000000000000002499999999999999769.<br>
So of course 0.0000000000000000000002_499999999999999769.round(22) will be 2.0e-22.</p>
</blockquote>
<p>That's a good point.</p>
<p>This doesn't make the other examples right though (e.g. 3.0e-31.round(31) should be equal to itself, not to 3.0000000000000003e-31)</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=207172011-09-10T15:53:12Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>ruby -v</strong> changed from <i>r33186</i> to <i>-</i></li></ul><p>(2011/09/10 10:00), Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> has been updated by Marc-Andre Lafortune.</p>
<p>Status changed from Rejected to Open</p>
<p>Hi,</p>
<p>Yui NARUSE wrote:</p>
<blockquote>
<p>Additional to say, 2.5e-22 express the range<br>
from 0.0000000000000000000002499999999999999534<br>
to__ 0.0000000000000000000002500000000000000003</p>
<p>The center of them is 0.0000000000000000000002499999999999999769.<br>
So of course 0.0000000000000000000002_499999999999999769.round(22) will be 2.0e-22.</p>
</blockquote>
<p>That's a good point.</p>
<p>This doesn't make the other examples right though (e.g. 3.0e-31.round(31) should be equal to itself, not to 3.0000000000000003e-31)</p>
</blockquote>
<p>irb(main):017:0> 3.0/10**31<br>
=> 3.0000000000000003e-31</p>
<p>This is in short, use BigDecimal.</p>
<p>--<br>
NARUSE, Yui <a href="mailto:naruse@airemix.jp" class="email">naruse@airemix.jp</a></p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=207582011-09-13T09:30:45Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>r33186</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=208422011-09-16T02:13:02Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Open</i></li><li><strong>Target version</strong> changed from <i>1.9.3</i> to <i>1.9.4</i></li><li><strong>ruby -v</strong> changed from <i>-</i> to <i>r33186</i></li></ul><p>On Mon, Sep 12, 2011 at 8:30 PM, Yui NARUSE <a href="mailto:naruse@airemix.jp" class="email">naruse@airemix.jp</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> has been updated by Yui NARUSE.</p>
<p>Status changed from Open to Rejected</p>
<p>r33186</p>
</blockquote>
<p>I'm not sure, are you trying to be rude or irritating by rejecting for a second time this issue in this way?</p>
<p>If so, please stop.</p>
<p>If not, would you be kind enough to explain the following:</p>
<ul>
<li>What makes you believe r33186/issue <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> is related with this issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a>?</li>
<li>Why do I have to repeat <a href="/issues/5272">[ruby-core:39296]</a> that issues <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> and <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> are not related?</li>
<li>Why do you think I opened two different issues in the first place (and fixed only one of them)?</li>
<li>Is there anything in the examples given in the two issues, or in the specs I wrote that makes it possible to confuse both issues?</li>
<li>Why do you feel it is appropriate to reject this issue by simply referring to one of my commits, without any explanation whatsoever?</li>
<li>Don't you feel that even if the commit addressed the issue, the issue should have been Closed, not Rejected?</li>
</ul>
<p>In addition, may I ask that you do not reject this issue a third time? I will set it myself to rejected if a compelling reason is given. Note that "Use BigDecimal" is not such a reason. Nor is <code>3.0/10**31 == 3.0000000000000003e-31</code>. Indeed, why stop there, why not have <code>Float("3.0e-31") == 3.0000000000000003e-31</code> and invoque the same arguments?</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=208492011-09-16T14:50:10Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>I'm not sure, are you trying to be rude or irritating by rejecting for a second time this issue in this way?</p>
<p>If so, please stop.</p>
</blockquote>
<p>Ah sorry, I intended to close this ticket without comment.<br>
"r33186" is wrong comment.<br>
(though r33186 is not valid ruby -v, you must copy and paste full <code>ruby -v</code> output)</p>
<blockquote>
<p>If not, would you be kind enough to explain the following:</p>
<ul>
<li>What makes you believe r33186/issue <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> is related with this issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a>?</li>
<li>Why do I have to repeat <a href="/issues/5272">[ruby-core:39296]</a> that issues <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> and <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> are not related?</li>
<li>Why do you think I opened two different issues in the first place (and fixed only one of them)?</li>
<li>Is there anything in the examples given in the two issues, or in the specs I wrote that makes it possible to confuse both issues?</li>
<li>Why do you feel it is appropriate to reject this issue by simply referring to one of my commits, without any explanation whatsoever?</li>
<li>Don't you feel that even if the commit addressed the issue, the issue should have been Closed, not Rejected?</li>
</ul>
</blockquote>
<p>Yeah, <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> and <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> are different.<br>
<a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Float#round doesn't round big values (Closed)" href="https://redmine.ruby-lang.org/issues/5272">#5272</a> is truely a bug of ruby, but <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round returns the wrong floats for higher precision (Closed)" href="https://redmine.ruby-lang.org/issues/5273">#5273</a> is not.</p>
<blockquote>
<p>In addition, may I ask that you do not reject this issue a third time? I will set it myself to rejected if a compelling reason is given. Note that "Use BigDecimal" is not such a reason. Nor is <code>3.0/10**31 == 3.0000000000000003e-31</code>. Indeed, why stop there, why not have <code>Float("3.0e-31") == 3.0000000000000003e-31</code> and invoque the same arguments?</p>
</blockquote>
<p>I can't your English of this line, I think you are saying you can't understand <a href="/issues/5273">[ruby-core:39443]</a>.<br>
I add little more explanation.</p>
<p>First of all, ISO C doesn't specify the detail of floating point numbers.<br>
So my discussion of this comment follows IEEE 754 non x87 floating point numbers.</p>
<p>IEEE 754 double (binary64) is sign 1bit, exponent 11bit, and fraction 52bit,<br>
and its internal reprensentation is binary (not decimal).<br>
When a number is converted to float, the number's precision is limited to this and<br>
lost information over decimal/binary conversion.</p>
<p>For example, 3.0 / 10**31 is considered as 3.0 * 1.0e-31 (this is not accurate).<br>
This is 3.0 * 0.000000000000000000000000000000100000000000000008333642060759<br>
It become 0.000000000000000000000000000000300000000000000025000926182276.<br>
If you show this by inspect, it is rounded and showed as 3.0000000000000003e-31.<br>
And it is a different number from 3.0e-31 in Float.</p>
<p>If you use BigDecimal, such binary/decimal conversion is not done and you can specify its precision.<br>
So you can get expected result:<br>
irb(main):092:0> BigDecimal("2.5e-31").round(31).to_s<br>
=> "0.3E-30"</p>
<p>If you are still hard to understand my explaration, I doubt you don't understand floating point numbers.<br>
If so, please read following documents.<br>
<a href="http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html" class="external">http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html</a><br>
<a href="http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#floats_imprecise" class="external">http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#floats_imprecise</a><br>
<a href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" class="external">http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems</a></p>
<p>I assign this ticket to you, please close this after you understand.</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=208502011-09-16T15:28:59Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Yui NARUSE wrote:</p>
<blockquote>
<p>I can't your English of this line, I think you are saying you can't understand <a href="/issues/5273">[ruby-core:39443]</a>.</p>
</blockquote>
<p>No, I understood what you are saying. I meant the tour argument is not valid justification for the current behavior.</p>
<p>You say that 3.0e-31.round(31) should be 3.0/10**31 == 3.0000000000000003e-31.</p>
<p>I say it should be 3.0e-31.</p>
<p>If we follow your argument, then it would be ok if <code>Float("3.0e-31") == 3.0000000000000003e-31</code> but that is clearly not so.</p>
<p>I say that <code>any_float.round(prec) == Float("#{some_int}.0e-#{prec}")</code>, by definition (for positive prec).</p>
<p>Do you have any reason why you think that 3.0000000000000003e-31 is preferable to 3.0e-31? Do you truly think it is the expected result and the most useful result?</p>
<p>Note that in addition to commonsense and mathematics, javascript also agrees with me:</p>
<pre><code>3.0e-31.toPrecision(1) == 3.0000000000000003e-31.toPrecision(1) == "3e-31"
</code></pre> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=370332013-02-26T10:19:09Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> changed from <i>1.9.4</i> to <i>2.6</i></li></ul> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=678972017-11-22T10:12:30Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> changed from <i>marcandre (Marc-Andre Lafortune)</i> to <i>naruse (Yui NARUSE)</i></li></ul> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=681102017-12-01T13:23:36Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Open</i></li><li><strong>Assignee</strong> deleted (<del><i>naruse (Yui NARUSE)</i></del>)</li></ul> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=687072017-12-25T18:15:01Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=712432018-03-27T08:20:32Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/14635">Bug #14635</a>: Float#round(n) returns a wrong result when n is big</i> added</li></ul> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=712452018-03-27T08:34:31Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul><p>I think there are two issues in this one ticket: <code>3.0e-31.round(31) #=> 3.0000000000000003e-31</code> and <code>2.5e-22.round(22) #=> 2.0e-22</code>.</p>
<p>I created another ticket (<a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Float#round(n) returns a wrong result when n is big (Closed)" href="https://redmine.ruby-lang.org/issues/14635">#14635</a>) about the former issue with some investigation.</p>
<p>I think that we cannot fix the latter issue for the reason that Naruse-san said. <code>2.5e-22</code> represents the range of <code>0.249999999999999953374475e-21</code> and <code>0.250000000000000000394250e-21</code>, so the implementation cannot determine which <code>0.2e-38</code> and <code>0.3e-38</code> are preferable.</p>
<p>So unfortunately, <code>2.5e-39.round(39)</code> would be <code>2.0e-39</code>. Anyway, <code>2.0000000000000002e-39</code> is a wrong result, I think.</p>
<p>I'm closing this ticket because there are no other issue in this ticket. Marc-Andre, let me know if I am wrong.</p> Ruby master - Bug #5273: Float#round returns the wrong floats for higher precisionhttps://redmine.ruby-lang.org/issues/5273?journal_id=712562018-03-27T17:20:36Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>Marc-Andre, let me know if I am wrong.</p>
</blockquote>
<p>I agree with everything you wrote :-)</p>