https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-09-10T10:12:09ZRuby Issue Tracking SystemRuby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417022013-09-10T10:12:09Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>=begin<br>
I can't reproduce this in any version of Ruby that I have installed. What is your Ruby 2.0 patch level?</p>
<p>$ ruby2.0 -ve 'p Rational(2), Rational(3), Rational(2)/Rational(3)'<br>
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]<br>
(2/1)<br>
(3/1)<br>
(2/3)<br>
$ ruby2.0 -ve 'p (Rational(2,7)+Rational(5,7))/((2+5)/7)'<br>
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]<br>
(1/1)<br>
$ ruby2.1 -ve 'p Rational(2), Rational(3), Rational(2)/Rational(3)'<br>
ruby 2.1.0dev (2013-08-27 trunk 42696) [x86_64-linux]<br>
(2/1)<br>
(3/1)<br>
(2/3)<br>
$ ruby2.1 -ve 'p (Rational(2,7)+Rational(5,7))/((2+5)/7)'<br>
ruby 2.1.0dev (2013-08-27 trunk 42696) [x86_64-linux]<br>
(1/1)</p>
<p>Edit: same behaviour on a fresh build of ruby 2.1.0dev (2013-09-10 trunk 42900) [x86_64-linux]<br>
=end</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417072013-09-10T13:38:07Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Category</strong> changed from <i>core</i> to <i>lib</i></li></ul><p>=begin<br>
Rather, it seems caused by ((%mathn%)).</p>
<p>$ ~/ruby/1.9.3/bin/ruby -e 'p Rational(2)*Rational(1,2)'<br>
(1/1)<br>
$ ~/ruby/1.9.3/bin/ruby -rmathn -e 'p Rational(2)*Rational(1,2)'<br>
1</p>
<p>$ ~/ruby/2.0.0/bin/ruby -e 'p Rational(2)*Rational(1,2)'<br>
(1/1)<br>
$ ~/ruby/2.0.0/bin/ruby -rmathn -e 'p Rational(2)*Rational(1,2)'<br>
1<br>
=end</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417082013-09-10T13:56:39Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Requiring only 'mathn/rational' causes this behavior.<br>
It's a bug to use 'mathn/rational' solely.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417252013-09-11T00:53:17Zdavid_macmahon (David MacMahon)davidm@astro.berkeley.edu
<ul></ul><p>But your previous example required just mathn:</p>
<p>$ ruby -rmathn -e 'p Rational(2,1)'<br>
2</p>
<p>It seems like a mathn bug to me.</p>
<p>Dave</p>
<p>On Sep 9, 2013, at 9:56 PM, nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Rational canonicalization unexpectedly converts to Fixnum (Closed)" href="https://redmine.ruby-lang.org/issues/8883">#8883</a> has been updated by nobu (Nobuyoshi Nakada).</p>
<p>Status changed from Open to Rejected</p>
<h2>Requiring only 'mathn/rational' causes this behavior.<br>
It's a bug to use 'mathn/rational' solely.</h2>
<p>Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Rational canonicalization unexpectedly converts to Fixnum (Closed)" href="https://redmine.ruby-lang.org/issues/8883">#8883</a>: Rational canonicalization unexpectedly converts to Fixnum<br>
<a href="https://bugs.ruby-lang.org/issues/8883#change-41708" class="external">https://bugs.ruby-lang.org/issues/8883#change-41708</a></p>
<p>Author: melquiades (Paul Cantrell)<br>
Status: Rejected<br>
Priority: Normal<br>
Assignee:<br>
Category: lib<br>
Target version:<br>
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]<br>
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN</p>
<p>The documentation for Rational (<a href="http://www.ruby-doc.org/core-2.0.0/Rational.html" class="external">http://www.ruby-doc.org/core-2.0.0/Rational.html</a>) states that the result of creating or doing arithmetic on Rationals returns Rationals, as one would expect. Examples from the docs:</p>
<p>Rational(1) #=> (1/1)<br>
3.to_r #=> (3/1)<br>
Rational(-2, 9) * Rational(-9, 2) #=> (1/1)</p>
<p>These all work as documented in 1.9. In 2.0, however, they all return Fixnum:</p>
<p>Rational(1) #=> 1<br>
3.to_r #=> 3<br>
Rational(-2, 9) * Rational(-9, 2) #=> 1</p>
<p>This leads to unexpected behavior:</p>
<p>Rational(2) / Rational(3) # => 0 ...but returns (2/3) in 1.9</p>
<p>That behavior is potentially dangerous. Math that may <em>usually</em> work, but suddenly start suffering from truncation errors depending on intermediate results. For example:</p>
<p>def should_always_return_one(a, b, c)<br>
(Rational(a, c) + Rational(b, c)) / (a + b) * c<br>
end</p>
<p>Under 1.9:</p>
<p>should_always_return_one(2, 3, 7) #=> (1/1)<br>
should_always_return_one(2, 4, 7) #=> (1/1)<br>
should_always_return_one(2, 5, 7) #=> (1/1)<br>
should_always_return_one(2, 6, 7) #=> (1/1)</p>
<p>Under 2.0:</p>
<p>should_always_return_one(2, 3, 7) #=> 1<br>
should_always_return_one(2, 4, 7) #=> 1<br>
should_always_return_one(2, 5, 7) #=> 0 Oops!<br>
should_always_return_one(2, 6, 7) #=> 1</p>
<p>Either the docs are wrong, or this is a bug. I vote bug. Whether arithmetic expressions truncate the result should not depend on whether intermediate values just happen to be integers! Such behavior renders Rational almost too dangerous to use in situations where exact results are required. (Yes, I realize that requiring 'mathn' fixes this, but even with such a workaround as an option, this is dangerously broken. See also <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: mathn/rational destroys Fixnum#/, Fixnum#quo and Bignum#/, Bignum#quo (Closed)" href="https://redmine.ruby-lang.org/issues/2121">#2121</a>.) Note that floating point arithmetic does <em>not</em> exhibit this behavior.</p>
<p>--<br>
<a href="http://bugs.ruby-lang.org/" class="external">http://bugs.ruby-lang.org/</a></p>
</blockquote> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417262013-09-11T07:26:25Zmarcandre (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>david_macmahon (David MacMahon) wrote:</p>
<blockquote>
<p>But your previous example required just mathn:</p>
<p>$ ruby -rmathn -e 'p Rational(2,1)'<br>
2</p>
<p>It seems like a mathn bug to me.</p>
</blockquote>
<p>Agreed.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417272013-09-11T07:28:31Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Mmm, sorry, misread.</p>
<p>I think the idea is that the buggy part (Rational(2) / Rational(3) # => 0) won't happen if you require 'mathn'</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417302013-09-11T08:53:10Zdavid_macmahon (David MacMahon)davidm@astro.berkeley.edu
<ul></ul><p>OK. I agree that requiring mathn avoids that buggy part. Thanks for clarifying. I guess I'm just a little uncomfortable with Rationals and Fixnums being promoted/demoted as needed, but maybe it's all OK and I'm just being paranoid.</p>
<p>While playing around with this, I see that integer Floats also have some special handling:</p>
<a name="Without-mathn"></a>
<h1 >Without mathn...<a href="#Without-mathn" class="wiki-anchor">¶</a></h1>
<p>$ ruby -e 'p [1/2.0, 1/2.5]'<br>
[0.5, 0.4]</p>
<a name="With-mathn"></a>
<h1 >With mathn...<a href="#With-mathn" class="wiki-anchor">¶</a></h1>
<p>$ ruby -r mathn -e 'p [1/2.0, 1/2.5]'<br>
[(1/2), 0.4]</p>
<p>Oddly though, this can result in non-reduced Rationals:</p>
<p>$ ruby -r mathn -e 'p [2/2.0, 2/2.5]'<br>
[(2/2), 0.8]</p>
<p>Weird.</p>
<p>Also, why do integer Floats not get changed to Fixnums like Rational and Complex do?</p>
<p>$ ruby -r mathn -e 'p Rational(1).class'<br>
Fixnum</p>
<p>$ ruby -r mathn -e 'p Complex(1).class'<br>
Fixnum</p>
<p>$ ruby -r mathn -e 'p Float(1).class'<br>
Float</p>
<p>Thanks,<br>
Dave</p>
<p>On Sep 10, 2013, at 3:28 PM, marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Rational canonicalization unexpectedly converts to Fixnum (Closed)" href="https://redmine.ruby-lang.org/issues/8883">#8883</a> has been updated by marcandre (Marc-Andre Lafortune).</p>
<p>Status changed from Open to Rejected</p>
<p>Mmm, sorry, misread.</p>
<p>I think the idea is that the buggy part (Rational(2) / Rational(3) # => 0) won't happen if you require 'mathn'</p>
<hr>
<p>Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Rational canonicalization unexpectedly converts to Fixnum (Closed)" href="https://redmine.ruby-lang.org/issues/8883">#8883</a>: Rational canonicalization unexpectedly converts to Fixnum<br>
<a href="https://bugs.ruby-lang.org/issues/8883#change-41727" class="external">https://bugs.ruby-lang.org/issues/8883#change-41727</a></p>
<p>Author: melquiades (Paul Cantrell)<br>
Status: Rejected<br>
Priority: Normal<br>
Assignee:<br>
Category: lib<br>
Target version:<br>
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]<br>
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN</p>
<p>The documentation for Rational (<a href="http://www.ruby-doc.org/core-2.0.0/Rational.html" class="external">http://www.ruby-doc.org/core-2.0.0/Rational.html</a>) states that the result of creating or doing arithmetic on Rationals returns Rationals, as one would expect. Examples from the docs:</p>
<p>Rational(1) #=> (1/1)<br>
3.to_r #=> (3/1)<br>
Rational(-2, 9) * Rational(-9, 2) #=> (1/1)</p>
<p>These all work as documented in 1.9. In 2.0, however, they all return Fixnum:</p>
<p>Rational(1) #=> 1<br>
3.to_r #=> 3<br>
Rational(-2, 9) * Rational(-9, 2) #=> 1</p>
<p>This leads to unexpected behavior:</p>
<p>Rational(2) / Rational(3) # => 0 ...but returns (2/3) in 1.9</p>
<p>That behavior is potentially dangerous. Math that may <em>usually</em> work, but suddenly start suffering from truncation errors depending on intermediate results. For example:</p>
<p>def should_always_return_one(a, b, c)<br>
(Rational(a, c) + Rational(b, c)) / (a + b) * c<br>
end</p>
<p>Under 1.9:</p>
<p>should_always_return_one(2, 3, 7) #=> (1/1)<br>
should_always_return_one(2, 4, 7) #=> (1/1)<br>
should_always_return_one(2, 5, 7) #=> (1/1)<br>
should_always_return_one(2, 6, 7) #=> (1/1)</p>
<p>Under 2.0:</p>
<p>should_always_return_one(2, 3, 7) #=> 1<br>
should_always_return_one(2, 4, 7) #=> 1<br>
should_always_return_one(2, 5, 7) #=> 0 Oops!<br>
should_always_return_one(2, 6, 7) #=> 1</p>
<p>Either the docs are wrong, or this is a bug. I vote bug. Whether arithmetic expressions truncate the result should not depend on whether intermediate values just happen to be integers! Such behavior renders Rational almost too dangerous to use in situations where exact results are required. (Yes, I realize that requiring 'mathn' fixes this, but even with such a workaround as an option, this is dangerously broken. See also <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: mathn/rational destroys Fixnum#/, Fixnum#quo and Bignum#/, Bignum#quo (Closed)" href="https://redmine.ruby-lang.org/issues/2121">#2121</a>.) Note that floating point arithmetic does <em>not</em> exhibit this behavior.</p>
<p>--<br>
<a href="http://bugs.ruby-lang.org/" class="external">http://bugs.ruby-lang.org/</a></p>
</blockquote> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417322013-09-11T13:09:34Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>david_macmahon (David MacMahon) wrote:</p>
<blockquote>
<p>While playing around with this, I see that integer Floats also have some special handling:</p>
</blockquote>
<p>Right. Floats are inexact while Integers & Rational are exact, and so are Complex with exact components. Rational(1/1) and 1 should yield the same mathematical result, but with floats that can be tricky. For example there are infinitely many different bigdecimals that will map to 1.0 (say 1.000....1 and 1.000...2 with enough zeros), but they don't behave exactly the same way, for example if you substract 1), so we can't freely map them.</p>
<blockquote>
<p>Oddly though, this can result in non-reduced Rationals:</p>
<p>$ ruby -r mathn -e 'p [2/2.0, 2/2.5]'<br>
[(2/2), 0.8]</p>
</blockquote>
<p>Oh oh, that's a bug. It's not even related to 'mathn'. I opened a new issue about this: <a href="https://bugs.ruby-lang.org/issues/8894" class="external">https://bugs.ruby-lang.org/issues/8894</a></p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=417342013-09-11T13:29:09Zdavid_macmahon (David MacMahon)davidm@astro.berkeley.edu
<ul></ul><p>On Sep 10, 2013, at 9:09 PM, marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Rational canonicalization unexpectedly converts to Fixnum (Closed)" href="https://redmine.ruby-lang.org/issues/8883">#8883</a> has been updated by marcandre (Marc-Andre Lafortune).</p>
<p>david_macmahon (David MacMahon) wrote:</p>
<blockquote>
<p>While playing around with this, I see that integer Floats also have some special handling:</p>
</blockquote>
<p>Right. Floats are inexact while Integers & Rational are exact, and so are Complex with exact components. Rational(1/1) and 1 should yield the same mathematical result, but with floats that can be tricky. For example there are infinitely many different bigdecimals that will map to 1.0 (say 1.000....1 and 1.000...2 with enough zeros), but they don't behave exactly the same way, for example if you substract 1), so we can't freely map them.</p>
</blockquote>
<p>That's all fine from a numerical/mathematical point of view, but it still seems like there is something missing from the duck typing:</p>
<p>$ ruby -e 'p (1/1.0).nan?'<br>
false</p>
<p>$ ruby -r mathn -e 'p (1/1.1).nan?'<br>
false</p>
<p>$ ruby -r mathn -e 'p (1/1.0).nan?'<br>
-e:1:in <code><main>': undefined method </code>nan?' for (1/1):Rational (NoMethodError)</p>
<p>Though admittedly this is getting a bit far from the OP.</p>
<blockquote>
<blockquote>
<p>Oddly though, this can result in non-reduced Rationals:</p>
<p>$ ruby -r mathn -e 'p [2/2.0, 2/2.5]'<br>
[(2/2), 0.8]</p>
</blockquote>
<p>Oh oh, that's a bug. It's not even related to 'mathn'. I opened a new issue about this: <a href="https://bugs.ruby-lang.org/issues/8894" class="external">https://bugs.ruby-lang.org/issues/8894</a></p>
</blockquote>
<p>Thanks!</p>
<p>Dave</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=426302013-10-27T11:44:11Zmelquiades (Paul Cantrell)cantrell@pobox.com
<ul></ul><p>Somewhere in all the discussion, the actual bug got lost. This issue shouldn't be closed.</p>
<p>To clarify:</p>
<p>(1) The bug occurs when you do <em>not</em> include mathn, and has nothing to do with mathn.</p>
<p>(2) The bug occurs when you include <em>nothing</em> at all:</p>
<pre><code>$ ~/.rvm/rubies/ruby-2.0.0-p247/bin/ruby -e 'p Rational(2) / Rational(3)'
0
</code></pre>
<p>That is clearly wrong. The should_always_return_one example demonstrates why this behavior is terribly dangerous, and is probably causing mathematically incorrect results in production code right now for poor unsuspecting souls out there in the world.</p>
<p>(3) The bug does not occur in 1.9.3:</p>
<pre><code>$ ~/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -e 'p Rational(2) / Rational(3)'
(2/3)
</code></pre>
<p>Bottom line: promoting the results of Rational calculations to Fixnum is never safe without mathn, not ever, and Ruby should never do it.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=426382013-10-28T10:38:28Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Hi</p>
<p>melquiades (Paul Cantrell) wrote:</p>
<blockquote>
<p>(2) The bug occurs when you include <em>nothing</em> at all:</p>
<pre><code>$ ~/.rvm/rubies/ruby-2.0.0-p247/bin/ruby -e 'p Rational(2) / Rational(3)'
0
</code></pre>
</blockquote>
<p>I can't reproduce this, with ruby 2.0.0p247, p195 nor trunk.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=426392013-10-28T12:48:08Znagachika (Tomoyuki Chikanaga)nagachika00@gmail.com
<ul></ul><p>Hello, melquiades</p>
<p>Don't you build your binary with --with-static-linked-ext ?<br>
A similar issue is reported when extension library mathn/rational is statically linked.<br>
See <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: String#to_r fails after moving ruby to other OSX system (Closed)" href="https://redmine.ruby-lang.org/issues/8879">#8879</a></p>
<p>If so, require "mathn" explicitly ease the problem.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=427312013-11-03T11:10:38Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Closed</i></li></ul> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=428852013-11-13T00:05:15Znagachika (Tomoyuki Chikanaga)nagachika00@gmail.com
<ul><li><strong>Backport</strong> changed from <i>1.9.3: UNKNOWN, 2.0.0: UNKNOWN</i> to <i>1.9.3: UNKNOWN, 2.0.0: DONE</i></li></ul><p>r43449, r43514 and r43525 are backported to ruby_2_0_0 at r43656.</p> Ruby master - Bug #8883: Rational canonicalization unexpectedly converts to Fixnumhttps://redmine.ruby-lang.org/issues/8883?journal_id=430162013-11-19T16:43:55Zmelquiades (Paul Cantrell)cantrell@pobox.com
<ul></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/404">@nagachika (Tomoyuki Chikanaga)</a>: Yes, your guess is correct. I am using rvm, which passes --with-static-linked-ext.</p>
<p>I verified that patch 43656 does indeed fix the issue:</p>
<pre><code>$ rvm install 2.0.0-patch43656 --patch changeset_r43656.diff
...
~/.rvm/rubies/ruby-2.0.0-p247-patch43656/bin/ruby -e 'p Rational(2) / Rational(3)'
(2/3)
</code></pre>
<p>Hooray!</p>
<p>(Apologies for my slow responses. Apparently I'm not receiving email notifications on this thread, despite having watched it.)</p>