https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112010-07-25T03:11:25ZRuby Issue Tracking SystemRuby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=125282010-07-25T03:11:25Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li></ul><p>=begin<br>
I completely agree that Math::Float <=> "foo" should return nil.</p>
<p>The current behavior is due to r23742 which wanted to address the fact that Float::Infinity <=> BigDecimal("1.0E500") was returning 0 (I think, see rubydev:38681)</p>
<p>To fix Float::Infinity <=> "foo", the minimum that must be done is:</p>
<p>diff --git a/numeric.c b/numeric.c<br>
index eb3d4be..daa5d6d 100644<br>
--- a/numeric.c<br>
+++ b/numeric.c<br>
@@ -1038,7 +1038,7 @@ flo_cmp(VALUE x, VALUE y)<br>
break;</p>
<pre><code> default:
</code></pre>
<ul>
<li>
<pre><code> if (isinf(a) && (!rb_respond_to(y, rb_intern("infinite?")) ||
</code></pre>
</li>
</ul>
<ul>
<li>
<pre><code> if (isinf(a) && (rb_respond_to(y, rb_intern("infinite?")) &&
!RTEST(rb_funcall(y, rb_intern("infinite?"), 0, 0)))) {
if (a > 0.0) return INT2FIX(1);
return INT2FIX(-1);
</code></pre>
</li>
</ul>
<p>The fact that <=> is not consistent with <, etc, is also a problem that need to be fixed. Either the special treatment should be extended to the other comparison operators, or the special treatment for infinity should be removed from <=></p>
<p>I believe the special treatment should be removed altogether:</p>
<p>diff --git a/numeric.c b/numeric.c<br>
index eb3d4be..a6c5360 100644<br>
--- a/numeric.c<br>
+++ b/numeric.c<br>
@@ -1038,11 +1038,6 @@ flo_cmp(VALUE x, VALUE y)<br>
break;</p>
<pre><code> default:
</code></pre>
<ul>
<li>
<pre><code> if (isinf(a) && (!rb_respond_to(y, rb_intern("infinite?")) ||
</code></pre>
</li>
<li>
<pre><code> !RTEST(rb_funcall(y, rb_intern("infinite?"), 0, 0)))) {
</code></pre>
</li>
<li>
<pre><code> if (a > 0.0) return INT2FIX(1);
</code></pre>
</li>
<li>
<pre><code> return INT2FIX(-1);
</code></pre>
</li>
<li>
<pre><code> }
return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
</code></pre>
}<br>
return rb_dbl_cmp(a, b);</li>
</ul>
<p>I understand the intent, but the fact is that Float::INFINITY is a very big value, but since it is the float representation of a lot of big real numbers, like 10<strong>400, 10</strong>40000 or even Infinity itself, I feel that r23742 introduces many inconsistencies. For example, currently:</p>
<p>1.0e200 ** 2 <=> BigDecimal("1.0e99999") # => 1</p>
<p>=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=125302010-07-25T05:47:05Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>=begin<br>
This issue was solved with changeset r28751.<br>
Tomasz, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<p>=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=125312010-07-25T06:01:11Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>yugui (Yuki Sonoda)</i></li></ul><p>=begin</p>
<p>=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=125932010-07-30T01:50:46Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>=begin<br>
Backported at r28788.<br>
=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=125942010-07-30T06:54:13Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li><li><strong>Assignee</strong> deleted (<del><i>yugui (Yuki Sonoda)</i></del>)</li></ul><p>=begin<br>
The patch fixes comparison with non numerics, but doesn't address the rest of the issues:</p>
<ul>
<li>inconsistency with mathematics</li>
<li>inconsistency with other operators like <, <=, ..</li>
</ul>
<p>Is there objection to removing the special test for infinity?</p>
<p>diff --git a/numeric.c b/numeric.c<br>
index 740ef54..ed159ce 100644<br>
--- a/numeric.c<br>
+++ b/numeric.c<br>
@@ -1039,15 +1039,6 @@ flo_cmp(VALUE x, VALUE y)<br>
break;</p>
<pre><code> default:
</code></pre>
<ul>
<li>
<pre><code> if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
</code></pre>
</li>
<li>
<pre><code> if (RTEST(i)) {
</code></pre>
</li>
<li>
<pre><code> int j = rb_cmpint(i, x, y);
</code></pre>
</li>
<li>
<pre><code> j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
</code></pre>
</li>
<li>
<pre><code> return INT2FIX(j);
</code></pre>
</li>
<li>
<pre><code> }
</code></pre>
</li>
<li>
<pre><code> if (a > 0.0) return INT2FIX(1);
</code></pre>
</li>
<li>
<pre><code> return INT2FIX(-1);
</code></pre>
</li>
<li>
<pre><code> }
return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
</code></pre>
}<br>
return rb_dbl_cmp(a, b);</li>
</ul>
<p>=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=126132010-07-30T12:47:55Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> set to <i>2.0.0</i></li></ul><p>=begin<br>
Hi,</p>
<blockquote>
<p>The patch fixes comparison with non numerics, but doesn't address the rest of the issues:</p>
</blockquote>
<p>Indeed. I thought nobu aimed to fix only the obvious wrong condition.</p>
<blockquote>
<p>Is there objection to removing the special test for infinity?</p>
</blockquote>
<p>It looks like a design issue rather then code bug. So I change this<br>
to 1.9.x.</p>
<p>I have no objection against removal of the code in trunk. Though,<br>
I like rather extend the special test to other operators than remove.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a><br>
=end</p> Ruby master - Bug #3609: Float Infinity comparisons in 1.9https://bugs.ruby-lang.org/issues/3609?journal_id=130452010-08-27T13:59:19Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>=begin</p>
<p>=end</p>