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/

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