Issue #5321 has been reported by Kenta Murata. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Open Priority: Normal Assignee: Yukihiro Matsumoto Category: core Target version: 2.0 Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-09-14 06:20

on 2011-10-30 00:10

Issue #5321 has been updated by Alexey Muranov. I like this proposal. -Alexey. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Open Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-30 01:58

Issue #5321 has been updated by Charles Nutter. 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. 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. 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. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Open Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-30 13:30

Issue #5321 has been updated by Alexey Muranov. 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.". Thus the operations on BigDecimal are not exact. (See also BigDecimal::ROUND_MODE.) For me it is easier to think simply whether a class is *intended* 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 *exceptional* value." -Alexey. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Open Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 12:05

On 30/10/11 12:29, Alexey Muranov wrote: > > Issue #5321 has been updated by Alexey Muranov. > > > 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.". > Thus the operations on BigDecimal are not exact. (See also BigDecimal::ROUND_MODE.) > > For me it is easier to think simply whether a class is *intended* 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 *exceptional* value." The problem with this is that the values *are* exact, and *some* of the operations are inexact. Some of the operations are *sometimes* inexact. Flagging a class at a time as exact or inexact just sounds wrong to me. Better would be flagging values that have had inexact operations performed on them (or exact operations with an inexact operand) at any time in their history, but I've no idea how you'd do that while keeping any sort of respectable performance. -- Alex

on 2011-10-31 13:13

Issue #5321 has been updated by Yusuke Endoh. Status changed from Rejected to Assigned Oops, sorry, I've closed this ticket by error. Reopen. -- Yusuke Endoh <mame@tsg.ne.jp> ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 13:56

Issue #5321 has been updated by Yusuke Endoh. Hello, 2011/9/14 Kenta Murata <muraken@gmail.com>: > Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. > So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. > I want simple way to examine the number exactness. Can you show any actual use case? -- Yusuke Endoh <mame@tsg.ne.jp> ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 14:22

Issue #5321 has been updated by Alexey Muranov. I misread the proposal, it was actually about adding instance Numeric#exact method, and not class Numeric.exact method. For me, adding class method Numeric.exact? would be good enough. To examine the exactness of a value, it would be enough to call `val.class.exact?`. As Alex pointed out, this is better for performance, and it seems easier to assume that Float is always inexact than to inspect its instances. I think it helps distinguishing classes that are *intended* 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. Sorry, i change my mind, i am more in favor of `Numeric.exact?`. A new subclass of Numeric can implement `Numeric#exact?`, or maybe interval arithmetic would even be better (http://en.wikipedia.org/wiki/Interval_arithmetic). -Alexey. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 14:32

Issue #5321 has been updated by Kenta Murata. Charles Nutter wrote: > Why is BigDecimal considered inexact? As Alexey pointed out, the reason is from BigDecimal's definition. A BigDecimal represents an *approximation* of a real number, not a rational number. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 14:56

Issue #5321 has been updated by tadayoshi funaba. i've proposed this before 1.9. and i've virtually used those predicates in Complex/Rational internally. i've taken those from scheme. scheme's one accept also complex number. i think those should be instance methods. > (exact? 1+0i) #t > (exact? 1+0.0i) #f bye ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 14:57

Issue #5321 has been updated by Kenta Murata. Yusuke Endoh wrote: > Can you show any actual use case? I want to create a mathematical function which behaves like 1/x. This function is defined over real numbers except for zero, because lim_{x -> +0} 1/x = +inf while lim_{x -> -0} 1/x = -inf. So I want to raise Math::DomainError for the function with exact zero. But I don't want to raise the error for inexact zeros, such as 0.0, -0.0, BigDecimal("0"), and others which are instances of classes I don't know. If Numeric#inexact? and Numeric#exact? are given, I can realize the function more easily. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 15:14

Issue #5321 has been updated by Alexey Muranov. Kenta Murata wrote: > So I want to raise Math::DomainError for the function with exact zero. > But I don't want to raise the error for inexact zeros, such as 0.0, -0.0, > BigDecimal("0"), and others which are instances of classes I don't know. In my opinion, checking `x.class.exact?` 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 *intended* to be exact. I think that all exact values should be grouped in separate classes, like Rational, distinguished by their *intended* 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. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 15:24

Hello, 2011/10/31 Kenta Murata <muraken@gmail.com>: > I want to create a mathematical function which behaves like 1/x. > This function is defined over real numbers except for zero, > because lim_{x -> +0} 1/x = +inf while lim_{x -> -0} 1/x = -inf. > So I want to raise Math::DomainError for the function with exact zero. > But I don't want to raise the error for inexact zeros, such as 0.0, -0.0, > BigDecimal("0"), and others which are instances of classes I don't know. > > If Numeric#inexact? and Numeric#exact? are given, > I can realize the function more easily. Thank you for the explanation. You expect that 1/x does never raise any exception if x is an inexact number, right? I wonder if the expectation is valid. But anyway, your use case is certainly convincing for me. Thank you.

on 2011-10-31 15:35

Hello, 2011/10/31 tadayoshi funaba <redmine@ruby-lang.org>: > i've taken those from scheme. > scheme's one accept also complex number. > i think those should be instance methods. I guess Scheme's exactness differs from this proposal. Scheme's exactness is orthogonal to the type. Actually, the spec of Scheme allows an "inexact integer" and "exact real." So it makes no sense to quote Scheme. 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.

on 2011-10-31 15:55

Issue #5321 has been updated by Kenta Murata. Alexey Muranov wrote: > In my opinion, checking `x.class.exact?` would be good enough. I don't think so for two reason. First, I think the form of "x.class.exact?' isn't ruby way, and too long than "x.exact?". Second, it limits the exactness property for class. Numeric classes including both exact and inexact instances should be allowed, I think. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 15:59

Issue #5321 has been updated by tadayoshi funaba. >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. i'm ok too, even if you don't understand at all what i mean. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-10-31 16:32

Issue #5321 has been updated by Alexey Muranov. Kenta Murata wrote: > Alexey Muranov wrote: > > In my opinion, checking `x.class.exact?` would be good enough. > > I don't think so for two reason. > First, I think the form of "x.class.exact?' isn't ruby way, and too long than "x.exact?". > Second, it limits the exactness property for class. > > Numeric classes including both exact and inexact instances should be allowed, I think. I am not against Numeric classes including both exact and inexact instances :). I just doubt it is a good idea to define this in the Numeric class itself, it would looks too heavy for 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. 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 kno w that it is a class property. I think this is all i had in mind. ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2011-11-01 13:14

Issue #5321 has been updated by Masaya Tarui. Status changed from Rejected to Assigned maybe miss operation ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? http://redmine.ruby-lang.org/issues/5321 Author: Kenta Murata Status: Assigned Priority: Normal Assignee: Yukihiro Matsumoto Category: Target version: Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.

on 2012-11-20 13:00

Issue #5321 has been updated by mame (Yusuke Endoh). Target version set to next minor ---------------------------------------- Feature #5321: Introducing Numeric#exact? and Numeric#inexact? https://bugs.ruby-lang.org/issues/5321#change-33199 Author: mrkn (Kenta Murata) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: Target version: next minor Ruby has several numeric classes such as Integer. These classes are classified whether their instances represent numbers exactly. According to this, Integer and Rational are exact, and Float and BigDecimal are inexact because they're always including error. The exactness of a Complex depends on its real and imaginary parts. Now, Ruby's numeric classes doesn't provide predicators to investigate the exactness of their instances. So, if we want to examine whether a number is exactly zero, we must investigate the class of the number. I want simple way to examine the number exactness. I propose to introduce Numeric#exact? and/or Numeric#inexact? for resolving this inconvenience.