ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
@ 2020-08-01  1:05 chris
  2020-08-01  2:18 ` [ruby-core:99427] " merch-redmine
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: chris @ 2020-08-01  1:05 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been reported by chrisseaton (Chris Seaton).

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99427] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
@ 2020-08-01  2:18 ` merch-redmine
  2020-08-01 12:18 ` [ruby-core:99432] " chris
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: merch-redmine @ 2020-08-01  2:18 UTC (permalink / raw)
  To: ruby-core

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


I disagree.  By that logic, positive float zero should be positive (it isn't), and since `-0.0 == 0.0` both should report as negative and positive, which is a contradiction.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86879

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99432] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
  2020-08-01  2:18 ` [ruby-core:99427] " merch-redmine
@ 2020-08-01 12:18 ` chris
  2020-08-01 12:38 ` [ruby-core:99433] " sawadatsuyoshi
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: chris @ 2020-08-01 12:18 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by chrisseaton (Chris Seaton).


It's already the case that `neg_zero == pos_zero` but not all methods on the two returns the same result - for example `neg_zero.inspect != pos_zero.inspect`.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86885

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99433] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
  2020-08-01  2:18 ` [ruby-core:99427] " merch-redmine
  2020-08-01 12:18 ` [ruby-core:99432] " chris
@ 2020-08-01 12:38 ` sawadatsuyoshi
  2020-08-01 12:50 ` [ruby-core:99434] " chris
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: sawadatsuyoshi @ 2020-08-01 12:38 UTC (permalink / raw)
  To: ruby-core

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


chrisseaton (Chris Seaton) wrote in #note-2:
> not all methods on the two [return] the same result - for example `neg_zero.inspect != pos_zero.inspect`.

What is your suggestion? To dispense with `-0.0`?

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86886

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99434] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (2 preceding siblings ...)
  2020-08-01 12:38 ` [ruby-core:99433] " sawadatsuyoshi
@ 2020-08-01 12:50 ` chris
  2020-08-01 16:00 ` [ruby-core:99441] " chris
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: chris @ 2020-08-01 12:50 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by chrisseaton (Chris Seaton).


My suggestion is that `Float#negative?(neg_zero) == true` and `Float#positive?(pos_zero) == false`.

We can't get rid of negative zero - it's a standard part of floating point numbers as implemented by the hardware. It's also useful in some applications.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86887

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99441] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (3 preceding siblings ...)
  2020-08-01 12:50 ` [ruby-core:99434] " chris
@ 2020-08-01 16:00 ` chris
  2020-08-01 18:02 ` [ruby-core:99442] " eregontp
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: chris @ 2020-08-01 16:00 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by chrisseaton (Chris Seaton).


If people aren't keen on changing `Float#negative?` since it has existing semantics, then another option could be to add a new predicate `Float#negative_zero?` - that would allow people to differentiate as needed.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86896

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99442] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (4 preceding siblings ...)
  2020-08-01 16:00 ` [ruby-core:99441] " chris
@ 2020-08-01 18:02 ` eregontp
  2020-08-01 19:41 ` [ruby-core:99443] " marcandre-ruby-core
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: eregontp @ 2020-08-01 18:02 UTC (permalink / raw)
  To: ruby-core

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


chrisseaton (Chris Seaton) wrote in #note-5:
> then another option could be to add a new predicate `Float#negative_zero?` - that would allow people to differentiate as needed.

Interesting, I thought `equal?` would work for that (it does on TruffleRuby) but it does not on CRuby 2.6.6:
```
[19] pry(main)> 0.0.equal?(0.0)
=> true
[20] pry(main)> -0.0.equal?(-0.0)
=> false
```
I guess it's a result of implementing Float as tagged (flonum) but only for a subset of Float:
```
[25] pry(main)> a=-0.0
=> -0.0
[26] pry(main)> a.equal?(a)
=> true
```

`eql?` and `==` do not differentiate 0.0 and -0.0.

One way to test for -0.0 seems `(-f).equal?(0.0)` but that's kind of brittle as it relies on 0.0 being a flonum on CRuby.

I tend to agree that `0.0.positive? == -0.0.negative?` and since `0.positive? => false` then both as `false` seems to make sense (essentially they are the same as `> 0` and `< 0`).

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86897

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99443] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (5 preceding siblings ...)
  2020-08-01 18:02 ` [ruby-core:99442] " eregontp
@ 2020-08-01 19:41 ` marcandre-ruby-core
  2020-08-02 12:21 ` [ruby-core:99444] " sawadatsuyoshi
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: marcandre-ruby-core @ 2020-08-01 19:41 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by marcandre (Marc-Andre Lafortune).


AFAIK, the only way to check for `-0.0` is `1.0 / var == -Float::INFINITY`

I find it difficult to discuss what need to be done about any of this as I do not know of the use cases for `-0.0`; without them it seems very theoretical.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86898

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:99444] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (6 preceding siblings ...)
  2020-08-01 19:41 ` [ruby-core:99443] " marcandre-ruby-core
@ 2020-08-02 12:21 ` sawadatsuyoshi
  2021-06-15  4:58 ` [ruby-core:104267] " muraken
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: sawadatsuyoshi @ 2020-08-02 12:21 UTC (permalink / raw)
  To: ruby-core

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


Ruby or IEEE 754 seem to regard `-0.0` and `0.0` not as 0 in the mathematical sense, bit as (something like) negative and positive infinitesimal. Taking this into account, I started to think `-0.0.negative?` and `0.0.positive?` should both be `true`.

Regarding the fact that `-0.0 == 0.0` is true, I think it should be understood as '`-0.0` approaches `0.0`' rather than '`-0.0` is `0.0`'. Then it would not contradict with the above. Floating point numbers are approximated numbers to begin with, so it does not make much sense to talk about their exact identity. Hence it makes sense to regard `Float#==` to mean 'close enough' rather than 'exactly the same'.

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-86899

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:104267] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (7 preceding siblings ...)
  2020-08-02 12:21 ` [ruby-core:99444] " sawadatsuyoshi
@ 2021-06-15  4:58 ` muraken
  2021-06-15  5:05 ` [ruby-core:104268] " muraken
  2021-06-17  5:47 ` [ruby-core:104332] " matz
  10 siblings, 0 replies; 12+ messages in thread
From: muraken @ 2021-06-15  4:58 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by mrkn (Kenta Murata).


The current behavior is consistent with the behavior of the integer zero:

```
irb(main):001:0> 0.negative?
=> false
irb(main):002:0> 0.positive?
=> false
```

----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-92477

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:104268] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (8 preceding siblings ...)
  2021-06-15  4:58 ` [ruby-core:104267] " muraken
@ 2021-06-15  5:05 ` muraken
  2021-06-17  5:47 ` [ruby-core:104332] " matz
  10 siblings, 0 replies; 12+ messages in thread
From: muraken @ 2021-06-15  5:05 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by mrkn (Kenta Murata).


If you have pragmatic demands that need to distinguish `-0.0` and `+0.0`, what do you think to propose the introduction of `Float#signbit`?


----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-92478

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

* [ruby-core:104332] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative
  2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
                   ` (9 preceding siblings ...)
  2021-06-15  5:05 ` [ruby-core:104268] " muraken
@ 2021-06-17  5:47 ` matz
  10 siblings, 0 replies; 12+ messages in thread
From: matz @ 2021-06-17  5:47 UTC (permalink / raw)
  To: ruby-core

Issue #17098 has been updated by matz (Yukihiro Matsumoto).


Unless there's any particular (real-world) reason to distinguish floating positive zero and negative zero, I'd like to keep the current behavior. 
And as @mrkn suggested, `Float#sign` may be useful to distinguish those two zeros, but it should be discussed as a different proposal.

Matz.


----------------------------------------
Bug #17098: Float#negative? reports negative zero as not negative
https://bugs.ruby-lang.org/issues/17098#change-92552

* Author: chrisseaton (Chris Seaton)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Is this intended behaviour?

```
irb(main):001:0> neg_zero = -0.0
=> -0.0
irb(main):002:0> neg_zero.negative?
=> false
irb(main):003:0> neg_zero < 0
=> false
```

It happens because `Numeric#negative?` uses `< 0`. My understanding of IEEE floating point is that negative zero is not less than zero, but I think it should still report as negative.



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

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

end of thread, other threads:[~2021-06-17  5:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-01  1:05 [ruby-core:99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative chris
2020-08-01  2:18 ` [ruby-core:99427] " merch-redmine
2020-08-01 12:18 ` [ruby-core:99432] " chris
2020-08-01 12:38 ` [ruby-core:99433] " sawadatsuyoshi
2020-08-01 12:50 ` [ruby-core:99434] " chris
2020-08-01 16:00 ` [ruby-core:99441] " chris
2020-08-01 18:02 ` [ruby-core:99442] " eregontp
2020-08-01 19:41 ` [ruby-core:99443] " marcandre-ruby-core
2020-08-02 12:21 ` [ruby-core:99444] " sawadatsuyoshi
2021-06-15  4:58 ` [ruby-core:104267] " muraken
2021-06-15  5:05 ` [ruby-core:104268] " muraken
2021-06-17  5:47 ` [ruby-core:104332] " matz

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