-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Differences between 0 and -0 #66
Comments
I think this came up because something like
Maybe the implicit context approach in php-decimal (one global shared context) gives way to these small inconsistencies. The idea was to have Should we go with what is practical and intuitive, or what is more correct from a mathematical point of view? My current intuition is:
|
So you would choose the zero is neither positive nor negative interpretation? Do you think adding I have no experience in C but, if you don't mind, I'd like to try contributing those changes and make this a learning opportunity :) |
Yes I think neither positive nor negative, and would those methods be equivalent to eg. NonNegative = isPositive() or isZero()? I think the compound is nice because there is no ambiguity to the reader. Prefer local reasoning 😊 Feel free to look at the 2.0 branch, should be able to navigate that quite easily. The mpdecimal docs are handy too. We can use this issue for questions and comments. |
I wasn't able to get all tests passing except on branch |
The work done here is good but I'm not sure that we should treat -0 and +0 as equal. https://stackoverflow.com/questions/4083401/negative-zero-in-python |
Maybe we could do so for formatting, but not calculation? |
I fail to see a benefit in keeping -0 not equal to 0, even for calculation only. |
If you have a negative number and round it incidentally to zero, it would be incorrect to change the sign. I'm sure the python folks thought it through. I can see the value in always producing a sign-less "0" when formatting, but to have isNegative and arithmetic honor the sign unchanged. |
If someone would like to render the zero sign, they can do so with checks and a prefix. |
As the referenced document mentioned, I would also agree that the best solution for this would be an opt-in approach. As far as I can understand, in 2.x you can already select the default rounding mode, why not this as an opt-in method too? Because, in version 1.x, I already have too many |
Decimal
does not consider 0 and -0 to be exactly the same value:isPositive()
returnstrue
andisNegative()
returnsfalse
) whileisPositive()
returnsfalse
andisNegative()
returnstrue
).Also,
toString()
andtoFixed()
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:
I would suggest choosing one of these two possibilities and implement methods accordingly, i.e.:
isPositive()
:false
isNegative()
:false
isNonNegative()
:true
isNonPositive()
:true
isStrictlyNegative()
:false
isStrictlyPositive()
:false
isPositive()
:true
isNegative()
:true
Also, I think
-0
should be converted to0
internally.WDYT?
The text was updated successfully, but these errors were encountered: