<link rel="self" href="https://bugs.ruby-lang.org/issues/5321.atom"/>
<link rel="alternate" href="https://bugs.ruby-lang.org/"/>
<id>https://bugs.ruby-lang.org/</id>
<icon>https://bugs.ruby-lang.org/favicon.ico?1582270010</icon>
<updated>2011-10-18T09:16:39Z</updated>
<author>
<name>Ruby Issue Tracking System</name>
</author>
<entry>
<title>Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=213822011-10-18T09:16:39Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Project</strong> changed from <i>Ruby master</i> to <i>CommonRuby</i></li><li><strong>Category</strong> deleted (<del><i>core</i></del>)</li><li><strong>Target version</strong> deleted (<del><i>3.0</i></del>)</li></ul> Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=214912011-10-23T17:21:10Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Project</strong> changed from <i>CommonRuby</i> to <i>Ruby master</i></li></ul> Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216432011-10-30T07:09:30Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I like this proposal. -Alexey.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216482011-10-30T08:57:31Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>Why is BigDecimal considered inexact? Unlike a float, a value in a BigDrcimal is always represented exactly. The value itself may be and estimate for various reasons (irrational, repeating, ...), but that is a property of the value, not a property of BigDecimal. Or put another way, BigDecimal is not inexact since if it is possible to exactly represent the number in base 10, BigDecimal can represent it exactly.</p>
<p>Perhaps the idea of inexact is too fuzzy here...even Float can be exact if the value can be exactly represented in base 2 decimal form with Float's precision.</p>
<p>It seems to me that the interesting characteristic of these classes is not whether the value they represent is exact, but whether arithmetic operations using them can be done exactly. By that definition, only Float is clearly inexact; even if the two operands of a floating-point arithmetic operation are exact, the result may not be. The other types do not have this property.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216502011-10-30T21:29:58Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Charles, this is from the documentation of BigDecimal: "If a computation results in a value which is too small to be represented as a BigDecimal within the currently specified limits of precision, zero must be returned.".<br>
Thus the operations on BigDecimal are not exact. (See also BigDecimal::ROUND_MODE.)</p>
<p>For me it is easier to think simply whether a class is <em>intended</em> to represent numbers exactly.<br>
Clearly it is better to represent a number exactly whenever the exact value can possibly be defined, can possibly be encoded into a sequence of bytes, when this representation would not take up all of the computer's memory, when carrying out exact computations would not be taking too much time, and if additionally a corresponding class exists in Ruby.<br>
One would probably use Float or BigDecimal to store the distance between CERN and Gran Sasso, but Rational to store an image aspect ratio when this aspect ratio is rational.</p>
<p>The formal definition of exactness probably is the following: "Each operation must be carried out exactly or return NaN or nil, or return some other <em>exceptional</em> value."</p>
<p>-Alexey.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216882011-10-31T20:23:07Zregularfry (Alex Young)alex@blackkettle.org
<ul></ul><p>On 30/10/11 12:29, Alexey Muranov wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Introducing Numeric#exact? and Numeric#inexact? (Rejected)" href="https://bugs.ruby-lang.org/issues/5321">#5321</a> has been updated by Alexey Muranov.</p>
<p>Charles, this is from the documentation of BigDecimal: "If a computation results in a value which is too small to be represented as a BigDecimal within the currently specified limits of precision, zero must be returned.".<br>
Thus the operations on BigDecimal are not exact. (See also BigDecimal::ROUND_MODE.)</p>
<p>For me it is easier to think simply whether a class is <em>intended</em> to represent numbers exactly. The formal definition of exactness probably is the following: "Each operation must be carried out exactly or return NaN or nil, or return some other <em>exceptional</em> value."</p>
</blockquote>
<p>The problem with this is that the values <em>are</em> exact, and <em>some</em> of the<br>
operations are inexact. Some of the operations are <em>sometimes</em> inexact.<br>
Flagging a class at a time as exact or inexact just sounds wrong to me.<br>
Better would be flagging values that have had inexact operations<br>
performed on them (or exact operations with an inexact operand) at any<br>
time in their history, but I've no idea how you'd do that while keeping<br>
any sort of respectable performance.</p>
<p>-- <br>
Alex</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216912011-10-31T21:10:48Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p><a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Introducing Numeric#exact? and Numeric#inexact? (Rejected)" href="https://bugs.ruby-lang.org/issues/5321">#5321</a> と重複です。あちらで議論しましょう。<br>
あちらの議論がまとまった上での作業は大歓迎です。</p>
<p>-- <br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216932011-10-31T21:13:10Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Assigned</i></li></ul><p>Oops, sorry, I've closed this ticket by error. Reopen.</p>
<p>-- <br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=216972011-10-31T21:55:07Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Hello,</p>
<p>2011/9/14 Kenta Murata <a href="mailto:muraken@gmail.com">muraken@gmail.com</a>:</p>
<blockquote>
<p>Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances.<br>
So, if we want to examine whether a number is exactly zero, we must investigate the class of the number.<br>
I want simple way to examine the number exactness.</p>
</blockquote>
<p>Can you show any actual use case?</p>
<p>-- <br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217032011-10-31T22:21:26Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I misread the proposal, it was actually about adding instance <code>Numeric#exact?</code> method, and not class <code>Numeric.exact?</code> method.</p>
<p>For me, adding class method <code>Numeric.exact?</code> would be good enough.<br>
To examine the exactness of a value, it would be enough to call <code>val.class.exact?</code>.<br>
As Alex pointed out, this is better for performance, and it seems to me easier to assume that Float is always inexact than to inspect its instances.</p>
<p>I think it helps distinguishing classes that are <em>intended</em> to represent numbers exactly from those that are not. Those that are intended for this, should carry out standard arithmetic exactly, or return an exceptional value.</p>
<p>Sorry, i change my mind, i am more in favor of <code>Numeric.exact?</code>. A new subclass of <code>Numeric</code> can implement <code>#exact?</code>, or maybe interval arithmetic would even be better (<a href="http://en.wikipedia.org/wiki/Interval_arithmetic">http://en.wikipedia.org/wiki/Interval_arithmetic</a>).</p>
<p>-Alexey.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217062011-10-31T22:32:18Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>Charles Nutter wrote:</p>
<blockquote>
<p>Why is BigDecimal considered inexact?</p>
</blockquote>
<p>As Alexey pointed out, the reason is from BigDecimal's definition.<br>
A BigDecimal represents an <em>approximation</em> of a real number, not a rational number.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217092011-10-31T22:54:16Ztadf (tadayoshi funaba)
<ul></ul><p>i've proposed this before 1.9.<br>
and i've virtually used those predicates in Complex/Rational internally.</p>
<p>i've taken those from scheme.<br>
scheme's one accept also complex number.<br>
i think those should be instance methods.</p>
<blockquote>
<p>(exact? 1+0i)<br>
#t<br>
(exact? 1+0.0i)<br>
#f</p>
</blockquote>
<p>bye</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217102011-10-31T22:55:00Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>Yusuke Endoh wrote:</p>
<blockquote>
<p>Can you show any actual use case?</p>
</blockquote>
<p>I want to create a mathematical function which behaves like 1/x.<br>
This function is defined over real numbers except for zero,<br>
because lim_{x -> +0} 1/x = +inf while lim_{x -> -0} 1/x = -inf.<br>
So I want to raise Math::DomainError for the function with exact zero.<br>
But I don't want to raise the error for inexact zeros, such as 0.0, -0.0,<br>
BigDecimal("0"), and others which are instances of classes I don't know.</p>
<p>If Numeric#inexact? and Numeric#exact? are given,<br>
I can realize the function more easily.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217112011-10-31T23:13:31Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Kenta Murata wrote:</p>
<blockquote>
<p>So I want to raise Math::DomainError for the function with exact zero.<br>
But I don't want to raise the error for inexact zeros, such as 0.0, -0.0,<br>
BigDecimal("0"), and others which are instances of classes I don't know.</p>
</blockquote>
<p>In my opinion, checking <code>x.class.exact?</code> would be good enough. If you will be checking the instance property #exact? for inputs, you are likely to find most input floats inexact, if they were produced as output of other operations, as operations on floats are not <em>intended</em> to be exact. I think that all exact values should be grouped in separate classes, like Rational, distinguished by their <em>intended</em> use. (Isn't the different intended uses the reason to have more than one class?) It may make sense to me to create a subclass of Numeric where each value is exact or inexact.</p>
<p>As a use case for <code>Numeric.exact?</code> i can only think about using it in irb to not have to look up documentation, or to have a program that needs to carry out computations exactly and that dynamically chooses or tests an appropriate class to work with (so indeed the program will be testing whether the <em>methods</em> of a class are exact before calling them).</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217132011-10-31T23:29:14Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Hello,</p>
<p>2011/10/31 Kenta Murata <a href="mailto:muraken@gmail.com">muraken@gmail.com</a>:</p>
<blockquote>
<p>I want to create a mathematical function which behaves like 1/x.<br>
This function is defined over real numbers except for zero,<br>
because lim_{x -> +0} 1/x = +inf while lim_{x -> -0} 1/x = -inf.<br>
So I want to raise Math::DomainError for the function with exact zero.<br>
But I don't want to raise the error for inexact zeros, such as 0.0, -0.0,<br>
BigDecimal("0"), and others which are instances of classes I don't know.</p>
<p>If Numeric#inexact? and Numeric#exact? are given,<br>
I can realize the function more easily.</p>
</blockquote>
<p>Thank you for the explanation.</p>
<p>You expect that 1/x does never raise any exception if x is an<br>
inexact number, right? I wonder if the expectation is valid.</p>
<p>But anyway, your use case is certainly convincing for me.<br>
Thank you.</p>
<p>-- <br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217152011-10-31T23:53:14Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Hello,</p>
<p>2011/10/31 tadayoshi funaba <a href="mailto:redmine@ruby-lang.org">redmine@ruby-lang.org</a>:</p>
<blockquote>
<p>i've taken those from scheme.<br>
scheme's one accept also complex number.<br>
i think those should be instance methods.</p>
</blockquote>
<p>I guess Scheme's exactness differs from this proposal.<br>
Scheme's exactness is orthogonal to the type. Actually, the spec of<br>
Scheme allows an "inexact integer" and "exact real."<br>
So it makes no sense to quote Scheme.</p>
<p>But I don't mean that I'm against the proposal. I think the proposal<br>
is fine even if it is different from Scheme's.</p>
<p>-- <br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp">mame@tsg.ne.jp</a></p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217172011-10-31T23:54:36Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>Alexey Muranov wrote:</p>
<blockquote>
<p>In my opinion, checking <code>x.class.exact?</code> would be good enough.</p>
</blockquote>
<p>I don't think so for two reason.<br>
First, I think the form of "x.class.exact?' isn't ruby way, and too long than "x.exact?".<br>
Second, it limits the exactness property for class.</p>
<p>Numeric classes including both exact and inexact instances should be allowed, I think.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217182011-10-31T23:59:04Ztadf (tadayoshi funaba)
<ul></ul><blockquote>
<p>But I don't mean that I'm against the proposal. I think the proposal is fine even if it is different from Scheme's. </p>
</blockquote>
<p>i'm ok too, even if you don't understand at all what i mean.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217192011-11-01T00:32:18Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Kenta Murata wrote:</p>
<blockquote>
<p>Alexey Muranov wrote:</p>
<blockquote>
<p>In my opinion, checking <code>x.class.exact?</code> would be good enough.</p>
</blockquote>
<p>I don't think so for two reason.<br>
First, I think the form of "x.class.exact?' isn't ruby way, and too long than "x.exact?".<br>
Second, it limits the exactness property for class.</p>
<p>Numeric classes including both exact and inexact instances should be allowed, I think.</p>
</blockquote>
<p>I am not against Numeric classes including both exact and inexact instances :).</p>
<p>I just doubt it is a good idea to define this in the base Numeric class itself, it would look too heavy to me. (Being able to check whether a given integer is exact? does not look valuable to me.) It sounds like what Yusuke Endoh said about exactness in Scheme (which i do not know). This would probably mean that you need to set the exactness flag for the first time when you read a value from the input, or it should be set to inexact by default (or, even worse, to exact or inexact depending on the default for the class), which will make you think if your output is inexact because an inexact operation was carried out, or because the exact flag was not set correctly on the first input.</p>
<p>I see benefit in being able to mix exact and inexact values, but it can be done even if exactness is defined on the (sub)class level. Keeping track of what influences exactness of your value seems easier if you know that it is a class property.</p>
<p>Update: i just thought about another argument against instance #exact? method. Please correct me if i am wrong: isn't the state of every Numeric object completely defined by its numeric value (in addition to its class)? If so, adding a second attribute (exacteness) to all Numeric instances will seem like a drastic change.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217512011-11-01T12:52:54Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Rejected</i></li></ul> Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=217772011-11-01T21:14:00Ztarui (Masaya Tarui)tarui@prx.jp
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Assigned</i></li></ul><p>maybe miss operation</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=331992012-11-20T20:55:54Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=687102017-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 - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=803182019-08-01T01:02:32Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>File</strong> <a href="/attachments/7926">numeric-exact-5321.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7926/numeric-exact-5321.patch">numeric-exact-5321.patch</a> added</li></ul><p>Attached is a patch to implement <code>Numeric#exact?</code>. This returns true for Integer and Rational, and false for Float and BigDecimal. For Complex, it returns true if the real and imaginary parts are both true, and false otherwise (one reason <code>exact?</code> needs to be an instance method and not a class method).</p>
<p>This feature is necessary to fix <a class="issue tracker-1 status-2 priority-4 priority-default" title="Bug: Complex#rationalize and to_r with approximate zeros (Assigned)" href="https://bugs.ruby-lang.org/issues/5179">#5179</a>, as BigDecimal is not a core class and Complex cannot know whether or not it is exact.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=836222020-01-04T06:27:03Zwishdev (John Higgins)wishdev@gmail.com
<ul></ul><p>I really think this is a very overblown issue that is being handled incorrectly.</p>
<p>First, we have what appears to be a singular issue on our hands</p>
<p><code>Complex(1, BigDecimal("0.0")).to_r</code></p>
<p>Whether or not that qualifies as a valid operation. <code>BigDecimal('0.0')</code> is ABSOLUTELY without any question at all an exact number and therefore valid in the original operation.</p>
<p><code>BigDecimal('0.0')</code> is and ALWAYS is an exact number</p>
<p>An example with a slightly different number (0 does not allow for many calculation examples).</p>
<pre><code class="ruby syntaxhl"><span class="c1"># Request arbitrary precision for future CALCULATIONS</span>
<span class="no">BigDecimal</span><span class="p">.</span><span class="nf">limit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="c1"># => 0</span>
<span class="c1"># Store a number</span>
<span class="n">a</span> <span class="o">=</span> <span class="no">BigDecimal</span><span class="p">(</span><span class="s1">'0.00000001'</span><span class="p">)</span>
<span class="o">=></span> <span class="mf">0.1e-7</span>
<span class="c1"># Calculate</span>
<span class="n">a</span> <span class="o">+</span> <span class="mf">0.1</span>
<span class="c1"># => 0.1e0 # Note the precision dropped</span>
<span class="c1"># Remove the limit</span>
<span class="no">BigDecimal</span><span class="p">.</span><span class="nf">limit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="o">=></span> <span class="mi">3</span>
<span class="c1"># Recalculate</span>
<span class="n">a</span> <span class="o">+</span> <span class="mf">0.1</span>
<span class="c1"># Precision held</span>
<span class="o">=></span> <span class="mf">0.10000001e0</span>
<span class="no">The</span> <span class="n">number</span> <span class="n">is</span> <span class="mi">100</span><span class="o">%</span> <span class="n">accurate</span> <span class="o">-</span> <span class="n">the</span> <span class="n">precision</span> <span class="n">only</span> <span class="n">affects</span> <span class="n">calculation</span> <span class="n">results</span> <span class="n">and</span> <span class="n">they</span> <span class="n">are</span> <span class="n">only</span> <span class="n">affected</span> <span class="n">at</span> <span class="n">my</span> <span class="n">request</span><span class="o">.</span>
<span class="p">(</span><span class="no">BigDecimal</span><span class="p">(</span><span class="s1">'6e-1092837'</span><span class="p">)</span> <span class="o">+</span> <span class="no">BigDecimal</span><span class="p">(</span><span class="s1">'1'</span><span class="p">)).</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">length</span>
<span class="c1"># 1092842</span>
</code></pre>
<p>That is an accurate calculation to whatever required precision exists. You can lock your system up if you increase the length of the number - it will work to accomplish what you ask of it.</p>
<p>One cannot throw out valid concepts because someone offers up convenience methods to developers to limit precision if desired. But the choice is that of the developer and not an issue with the class. The class is fully compliant with exactness and should be afforded that.</p>
<p>Floating point does not offer up a choice - it has intrinsic limits - BigDecimal offers no such limitations.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=838742020-01-15T02:23:52Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>I think associating class with exactness is wrong idea.</p>
<p>Please define the exactness of numeric classes, at first.</p>
<p>I think exactness and inexactness is a property of a method, not class.</p>
<p>For example, Integer#+ is exact because it implements mathematical addition exactly (except memory limitation).<br>
Rational#+ is also exact.<br>
As far as I know, BigDecimal#+ also implements mathematical addition exactly.<br>
But Float#+ is inexact because it implements just approximation of mathematical addition.<br>
(So, mathematically invalid result, such as 1.0 + 1e-20 == 1.0, is possible.)</p>
<p>Subtraction and multiplication is same as addition.<br>
(Integer, Rational and BigDecimal are exact but Float is inexact.)</p>
<p>Division is interesting.<br>
Integer#/ is exact as integer division. (But inexact as normal division.)<br>
<a href="http://mathworld.wolfram.com/IntegerDivision.html">http://mathworld.wolfram.com/IntegerDivision.html</a><br>
Rational#/ is exact as normal division.<br>
But Float#/ and BigDecimal#/ is inexact.</p>
<p>I doubt the concept of "exactness of numeric classes".<br>
So, I against this proposal until someone provide a good definition of<br>
exactness of numeric classes.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=838762020-01-15T02:43:28Zsawa (Tsuyoshi Sawada)
<ul></ul><p>akr (Akira Tanaka) wrote:</p>
<blockquote>
<p>I think associating class with exactness is wrong idea.<br>
...<br>
I think exactness and inexactness is a property of a method, not class.</p>
</blockquote>
<p>But notice that these operations are binary operations, not unary. You cannot talk about just <code>Integer#+</code> or <code>Float#+</code>, etc. You have to think about integer + integer, float + integer, integer + float, float + float, etc. And float + integer and integer + float are the same as float + float with respect to exactness. The reason should be attributed to some notion of hierarchy of exactness between the classes; float is less exact than integer, and the exactness of + would be the minimum exactness of the operands on both sides.</p>
<p>Hence, at least, the classes should also need to carry some kind of notion of exactness.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=838772020-01-15T03:03:42Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>sawa (Tsuyoshi Sawada) wrote:</p>
<blockquote>
<p>akr (Akira Tanaka) wrote:</p>
<blockquote>
<p>I think associating class with exactness is wrong idea.<br>
...<br>
I think exactness and inexactness is a property of a method, not class.</p>
</blockquote>
<p>But notice that these operations are binary operations, not unary. You cannot talk about just <code>Integer#+</code> or <code>Float#+</code>, etc. You have to think about integer + integer, float + integer, integer + float, float + float, etc. And float + integer and integer + float are the same as float + float with respect to exactness. The reason should be attributed to some notion of hierarchy of exactness between the classes; float is less exact than integer, and the exactness of + would be the minimum exactness of the operands on both sides.</p>
</blockquote>
<p>I see.<br>
I wrongly assumed integer + integer for Integer#+.</p>
<blockquote>
<p>Hence, at least, the classes should also need to carry some kind of notion of exactness.</p>
</blockquote>
<p>Do you have good definition of exactness of numeric classes?</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=838812020-01-15T06:19:07Zsawa (Tsuyoshi Sawada)
<ul></ul><p>akr (Akira Tanaka) wrote:</p>
<blockquote>
<p>Do you have good definition of exactness of numeric classes?</p>
</blockquote>
<p>Like alexeymuranov, I misread the proposal. I took it for <code>Integer.exact?</code>, etc. instead of <code>Numeric#exact?</code>, etc.</p>
<p>As for the notion of <strong>instance method</strong> <code>exact?</code> (on numeric instances), I don't think it would be practical. For float, the same number may be exact or inexact depending on its history. The history may include mathematical operations by which it was created, as well as simply as literal interpretation. For example, <code>2.5</code> achieved by the following literal may be naturally considered exact:</p>
<pre><code class="ruby syntaxhl"><span class="mf">2.5</span> <span class="c1"># => 2.5</span>
</code></pre>
<p>but the same <code>2.5</code> achieved in the following way is inexact:</p>
<pre><code class="ruby syntaxhl"><span class="mf">2.5000000000000001</span> <span class="c1"># => 2.5</span>
</code></pre>
<p>So we may expect it to be possible to have "multiple instances" of 2.5, with different exactness, but that is impossible given Ruby's object system.</p>
<p>As for the notion of <strong>class method</strong> <code>exact?</code> (on numeric classes), I think we can define that based on whether the literal interpretation is "transparent". <code>Float</code> is not exact because different literals are interpreted as the same instance, as with <code>2.5</code> above. I am being careful to use the word "transparent" above, which can be paraphrased as "one-to-one in some relevant sense". <code>Rational</code> should be considered exact even though <code>1/2r</code> and <code>2/4r</code> represent the same instance. <code>Integer</code> is exact.</p>
<p>However, the use case that mrkn provided (<a href="https://bugs.ruby-lang.org/issues/5321#note-13">https://bugs.ruby-lang.org/issues/5321#note-13</a>) requires the instance method <code>exact?</code>. I became not so confident with my argument.</p>
Ruby master - Feature #5321: Introducing Numeric#exact? and Numeric#inexact?https://bugs.ruby-lang.org/issues/5321?journal_id=839162020-01-16T08:04:48Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Rejected</i></li></ul><p>Rejected.</p>
<p>Matz.</p>