Decimal does not consider 0 and -0 to be exactly the same value:
- 0 is considered positive (
isPositive() returns true and isNegative() returns false) while
- -0 is considered negative (
isPositive() returns false and isNegative() returns true).
Also, toString() and toFixed() will include the - sign for -0.
I found two ways to create a Decimal instance with -0:
new Decimal('-0')
(new Decimal(0))->negate()
While the first is unlikely, the second may occur in case one negates a value without checking whether it's zero first. Given the differences listed above, this might induce unexpected behavior in the code doing this.
According to Wikipedia, 0 can either be considered:
- neither positive nor negative;
- both positive and negative.
I would suggest choosing one of these two possibilities and implement methods accordingly, i.e.:
- if neither positive nor negative:
isPositive(): false
isNegative(): false
isNonNegative(): true
isNonPositive(): true
- if both positive and negative:
isStrictlyNegative(): false
isStrictlyPositive(): false
isPositive(): true
isNegative(): true
Also, I think -0 should be converted to 0 internally.
WDYT?
Decimaldoes not consider 0 and -0 to be exactly the same value:isPositive()returnstrueandisNegative()returnsfalse) whileisPositive()returnsfalseandisNegative()returnstrue).Also,
toString()andtoFixed()will include the - sign for -0.I found two ways to create a
Decimalinstance with-0:new Decimal('-0')(new Decimal(0))->negate()While the first is unlikely, the second may occur in case one negates a value without checking whether it's zero first. Given the differences listed above, this might induce unexpected behavior in the code doing this.
According to Wikipedia, 0 can either be considered:
I would suggest choosing one of these two possibilities and implement methods accordingly, i.e.:
isPositive():falseisNegative():falseisNonNegative():trueisNonPositive():trueisStrictlyNegative():falseisStrictlyPositive():falseisPositive():trueisNegative():trueAlso, I think
-0should be converted to0internally.WDYT?