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


jeremyevans0 (Jeremy Evans) wrote:
> I think [that] Complex numbers with an imaginary part of zero should be treated as real numbers, and that `Complex#real?` should return `true` for such numbers.

Changing `Complex#real?`'s meaning has huge influence. You would also have to change `Numeric#integer?` for consistency.

Notice that in Ruby, there is no class `Real`, and `real?` means "not an instance of `Complex`". I believe `Float` is an approximation of the concept of real numbers in mathematics, but unfortunately, `Integer` and `Rational` are not subclasses of `Float` like integers and rational numbers are subsets of real numbers in the mathematical sense. So discussing your proposal in terms of real numbers does not look that helpful.

> `Complex#<=>` should return 1, 0, or -1 if both the receiver and the argument are real numbers (treating Complex instances with an imaginary part of zero as real numbers).

I think this should rather be handled by coercion as with arithmetic operations.

----------------------------------------
Bug #15857: <=> の右辺が <=> を実装していない場合の振る舞い
https://bugs.ruby-lang.org/issues/15857#change-78352

* Author: shuujii (Shuji KOBAYASHI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
`<=>` の右辺が `<=>` を実装していないとき、`nil` が返却される場合と例外が発生する場合があり一貫性がないように思えるのですが、意図的でしょうか。

```ruby
  0 <=> 0i               #=> NoMethodError (undefined method `<=>' for (0+0i):Complex)
  0 <=> BasicObject.new  #=> nil
 :a <=> 0i               #=> nil
"a" <=> 0i               #=> NoMethodError (undefined method `<=>' for (0+0i):Complex)

```

なお、`0 <=> 0i` に関しては、`0 == 0i` は `true` になるのでそれとも一貫性がないように思えるのもやや気になりました。

---Files--------------------------------
complex-real-spaceship.patch (8.75 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>