ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:92694] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
@ 2019-05-17 10:40 ` shuujii
  2019-06-05  4:55 ` [ruby-core:92970] " merch-redmine
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: shuujii @ 2019-05-17 10:40 UTC (permalink / raw)
  To: ruby-core

Issue #15857 has been reported by shuujii (Shuji KOBAYASHI).

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

* 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` になるのでそれとも一貫性がないように思えるのもやや気になりました。



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92970] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
  2019-05-17 10:40 ` [ruby-core:92694] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い shuujii
@ 2019-06-05  4:55 ` merch-redmine
  2019-06-05  8:33 ` [ruby-core:92975] " sawadatsuyoshi
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: merch-redmine @ 2019-06-05  4:55 UTC (permalink / raw)
  To: ruby-core

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

File complex-real-spaceship.patch added

I agree that this is a bug.  `Complex#<=>` should be implemented, even if it did return `nil` for all arguments.

However, I think Complex numbers with an imaginary part of zero should be treated as real numbers, and that `Complex#real?` should return `true` for such numbers.  Likewise, `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).  However, if the receiver or the argument is not a real number, then it should return `nil`.

Attached is a patch that implements the behavior described above.

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

* 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92975] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
  2019-05-17 10:40 ` [ruby-core:92694] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い shuujii
  2019-06-05  4:55 ` [ruby-core:92970] " merch-redmine
@ 2019-06-05  8:33 ` sawadatsuyoshi
  2019-06-06  2:33 ` [ruby-core:92993] " merch-redmine
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: sawadatsuyoshi @ 2019-06-05  8:33 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:92993] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-06-05  8:33 ` [ruby-core:92975] " sawadatsuyoshi
@ 2019-06-06  2:33 ` merch-redmine
  2019-06-08 12:17 ` [ruby-core:93023] " eregontp
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: merch-redmine @ 2019-06-06  2:33 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:93023] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-06-06  2:33 ` [ruby-core:92993] " merch-redmine
@ 2019-06-08 12:17 ` eregontp
  2019-06-11 14:09 ` [ruby-core:93044] " shuujii
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: eregontp @ 2019-06-08 12:17 UTC (permalink / raw)
  To: ruby-core

Issue #15857 has been updated by Eregon (Benoit Daloze).


The patch looks good to me.

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

* 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:93044] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-06-08 12:17 ` [ruby-core:93023] " eregontp
@ 2019-06-11 14:09 ` shuujii
  2019-06-11 17:18 ` [ruby-core:93046] " merch-redmine
  2019-06-14  1:02 ` [ruby-core:93119] " shuujii
  7 siblings, 0 replies; 8+ messages in thread
From: shuujii @ 2019-06-11 14:09 UTC (permalink / raw)
  To: ruby-core

Issue #15857 has been updated by shuujii (Shuji KOBAYASHI).


@jeremyevans0 Thank you for your comment. About `Complex`, I agree with most of v2 patch, but I have some comments.

- Should methods from `Comparable` that are currently disabled be enabled?
- `rb_undef_method(rb_cComplex, "<=>")` is unneeded.
- `idCmp` can be used instead of `id_spaceship`.

About other than `Complex`, `<=>` of some classes, such as `String` and `Time`, will fail if LHS does not implement `<=>`. It's better to return `nil` in this case too, because whether it's comparable or not and whether LHS implements `<=>` or not is essentially irrelevant, I think.

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

* 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:93046] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-06-11 14:09 ` [ruby-core:93044] " shuujii
@ 2019-06-11 17:18 ` merch-redmine
  2019-06-14  1:02 ` [ruby-core:93119] " shuujii
  7 siblings, 0 replies; 8+ messages in thread
From: merch-redmine @ 2019-06-11 17:18 UTC (permalink / raw)
  To: ruby-core

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

File complex-real-spaceship-v3.patch added

shuujii (Shuji KOBAYASHI) wrote:
> @jeremyevans0 Thank you for your comment. About `Complex`, I agree with most of v2 patch, but I have some comments.
> 
> - Should methods from `Comparable` that are currently disabled be enabled?

I don't think so, but I don't have a strong opinion.  If you would like those added, please submit a separate feature request for that.

> - `rb_undef_method(rb_cComplex, "<=>")` is unneeded.
> - `idCmp` can be used instead of `id_spaceship`.

Thank you, I made both of these changes in the attached v3 patch.

> About other than `Complex`, `<=>` of some classes, such as `String` and `Time`, will fail if LHS does not implement `<=>`. It's better to return `nil` in this case too, because whether it's comparable or not and whether LHS implements `<=>` or not is essentially irrelevant, I think.

I agree, but please submit a separate bug report for that as it is unrelated to `Complex#<=>`.

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

* 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)
complex-real-spaceship-v3.patch (7.79 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:93119] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い
       [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-06-11 17:18 ` [ruby-core:93046] " merch-redmine
@ 2019-06-14  1:02 ` shuujii
  7 siblings, 0 replies; 8+ messages in thread
From: shuujii @ 2019-06-14  1:02 UTC (permalink / raw)
  To: ruby-core

Issue #15857 has been updated by shuujii (Shuji KOBAYASHI).


> > @jeremyevans0 Thank you for your comment. About Complex, I agree with most of v2 patch, but I have some comments.
> >
> > - Should methods from Comparable that are currently disabled be enabled?
>
> I don't think so, but I don't have a strong opinion. If you would like those added, please submit a separate feature request for that.

I don't have a strong opinion, too.

> > About other than Complex, <=> of some classes, such as String and Time, will fail if ~~LHS~~RHS does not implement <=>. It's better to return nil in this case too, because whether it's comparable or not and whether ~~LHS~~RHS implements <=> or not is essentially irrelevant, I think.
>
> I agree, but please submit a separate bug report for that as it is unrelated to Complex#<=>.

Sure. I will create another ticket.

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

* 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)
complex-real-spaceship-v3.patch (7.79 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-06-14  1:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15857.20190517104016@ruby-lang.org>
2019-05-17 10:40 ` [ruby-core:92694] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い shuujii
2019-06-05  4:55 ` [ruby-core:92970] " merch-redmine
2019-06-05  8:33 ` [ruby-core:92975] " sawadatsuyoshi
2019-06-06  2:33 ` [ruby-core:92993] " merch-redmine
2019-06-08 12:17 ` [ruby-core:93023] " eregontp
2019-06-11 14:09 ` [ruby-core:93044] " shuujii
2019-06-11 17:18 ` [ruby-core:93046] " merch-redmine
2019-06-14  1:02 ` [ruby-core:93119] " shuujii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).