Issue #5321 has been updated by sawa (Tsuyoshi Sawada).


akr (Akira Tanaka) wrote:
> Do you have good definition of exactness of numeric classes?

Like  alexeymuranov, I misread the proposal. I took it for `Integer.exact?`, etc. instead of `Numeric#exact?`, etc.

As for the notion of **instance method** `exact?` (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, `2.5` achieved by the following literal may be naturally considered exact:

```ruby
2.5 # => 2.5
```

but the same `2.5` achieved in the following way is inexact:

```ruby
2.5000000000000001 # => 2.5
```

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.

As for the notion of **class method** `exact?` (on numeric classes), I think we can define that based on whether the literal interpretation is "transparent". `Float` is not exact because different literals are interpreted as the same instance, as with `2.5` above. I am being careful to use the word "transparent" above, which can be paraphrased as "one-to-one in some relevant sense". `Rational` should be considered exact even though `1/2r` and `2/4r` represent the same instance. `Integer` is exact.

However, the use case that mrkn provided (https://bugs.ruby-lang.org/issues/5321#note-13) requires the instance method `exact?`. I became not so confident with my argument.

----------------------------------------
Feature #5321: Introducing Numeric#exact? and Numeric#inexact?
https://bugs.ruby-lang.org/issues/5321#change-83881

* Author: mrkn (Kenta Murata)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* 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.


---Files--------------------------------
numeric-exact-5321.patch (7.33 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>