Issue #15857 has been updated by jeremyevans0 (Jeremy Evans).

File complex-real-spaceship-v2.patch added

sawa (Tsuyoshi Sawada) wrote:
> 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.

Thank you for providing some background on the purpose of the `real?` method.  I agree that it doesn't make sense to change `real?`.  Attached is a patch that only implements `<=>` and removes the modifications to `real?`.  It also adds specs for `<=>`.

> > `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.

Could you please provide a patch for your approach so we can compare and choose the simpler implementation?

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

* 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)
complex-real-spaceship-v2.patch (8.08 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>