https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112021-02-16T03:25:20ZRuby Issue Tracking SystemRuby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=904072021-02-16T03:25:20Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>IEEE754 follows the extended real number system that is a real number system with positive and negative Infinities. So, at least, <code>Float::INFINITY.real?</code> can return <code>true</code>. <code>BigDecimal</code> also employs the extended real number system, so <code>BigDecimal::INFINITY.real?</code> can be <code>true</code>, too.</p>
<p><code>Float::NAN</code> and <code>BigDecimal::NAN</code> are not-a-number, so <code>real?</code> of them may be reasonable to be <code>false</code> depending on the definition of the method.</p> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=904312021-02-16T11:37:00Zchrisseaton (Chris Seaton)chris@chrisseaton.com
<ul></ul><p>I think 'real' in this context just means 'not complex'.</p> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=904692021-02-18T00:48:16Zjtannas (Joel Tannas)
<ul></ul><p>Sorry for the slow reply - I had to go get a lesson from our resident mathematician.</p>
<p>mrkn (Kenta Murata) wrote in <a href="#note-1">#note-1</a>:</p>
<blockquote>
<p>IEEE754 follows the extended real number system that is a real number system with positive and negative Infinities.</p>
</blockquote>
<p>The definition of that number set is that it's the set of all real numbers plus the infinities. The infinities are still not real, even though they're in the set. <a href="https://mathworld.wolfram.com/AffinelyExtendedRealNumbers.html" class="external">https://mathworld.wolfram.com/AffinelyExtendedRealNumbers.html</a></p>
<p>chrisseaton (Chris Seaton) wrote in <a href="#note-2">#note-2</a>:</p>
<blockquote>
<p>I think 'real' in this context just means 'not complex'.</p>
</blockquote>
<p>That's how the code is written, but it conflicts with the actual definitions of complex & real. Going by the official definition, complex numbers include all real numbers. <a href="https://math.stackexchange.com/questions/304207/difference-between-imaginary-and-complex-numbers/304211#304211" class="external">https://math.stackexchange.com/questions/304207/difference-between-imaginary-and-complex-numbers/304211#304211</a><br>
Changing the code to completely match the formal definitions would be a big change though, so I don't really know what the best option is.</p> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=918912021-05-11T03:38:35Zsawa (Tsuyoshi Sawada)
<ul></ul><p>It does not make sense to discuss the mathematical definition of real numbers in this context because digital computers cannot handle (the entire) real numbers in the first place. Hence, we are dealing with floating point numbers, which are approximation, or a quotient set, of real numbers, but not real numbers themselves.There is no real numbers to begin with.</p>
<p>Whenever you see the word "real" in the context of Ruby, you need to understand that its use departs from the mathematical definition. The best you can propose is to avoid the word "real" and change the method name into something else.</p> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=919002021-05-11T19:02:25Zjtannas (Joel Tannas)
<ul></ul><p>Fair enough - If we're not worrying about following the mathematical definitions too closely then we can gloss over a lot of these details about the definition of "real numbers" and leave it as-is. Instead, how about a new method <code>#rational?</code> that responds with a boolean on whether a number will respond successfully to <code>#rationalize</code>?</p>
<ul>
<li>
<code>#rationalize</code> fails for complex numbers, NaN, and the infinities so <code>rational?</code> would give the desired behaviour</li>
<li>it meshes nicely with the existing <code>Rational</code> class</li>
<li>it gives users a simple check for if a number is "well behaved"</li>
</ul> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=920292021-05-20T05:24:09Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>FYI: numpy also says inf is a real.</p>
<pre><code>>>> import numpy
>>> import math
>>> numpy.isreal(math.inf)
True
</code></pre> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=920312021-05-20T05:30:03Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/10378">Feature #10378</a>: [PATCH 0/3] It's better (1 + 0i).real? return true</i> added</li></ul> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=920382021-05-20T07:55:23Zuniversato (Yoshimine Sato)
<ul></ul><p>FYI:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">p</span> <span class="no">Float</span><span class="o">::</span><span class="no">INFINITY</span><span class="p">.</span><span class="nf">real</span> <span class="c1">#=> Infinity</span>
<span class="nb">p</span> <span class="no">Float</span><span class="o">::</span><span class="no">INFINITY</span><span class="p">.</span><span class="nf">imag</span> <span class="c1">#=> 0</span>
<span class="nb">p</span> <span class="no">Float</span><span class="o">::</span><span class="no">NAN</span><span class="p">.</span><span class="nf">real</span> <span class="c1">#=> NaN</span>
<span class="nb">p</span> <span class="no">Float</span><span class="o">::</span><span class="no">NAN</span><span class="p">.</span><span class="nf">imag</span> <span class="c1">#=> 0</span>
</code></pre>
<p>Python and Octave have same behavior. Probably MATLAB does too.<br>
If we change the behavior of <code>real?</code>, we should also consider the behavior of <code>real</code> a bit</p> Ruby master - Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`https://bugs.ruby-lang.org/issues/17631?journal_id=920692021-05-21T07:07:02Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Traditionally, <code>real</code> just means being floating-point numbers in Computer Science. Other languages treat infinity and <code>NaN</code> in similar manner.</p>
<p>Matz.</p>