-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
math/big: GCD support for negative and zero arguments #28878
Comments
CC @griesemer |
Note, you can avoid allocation by using:
Which switches the sign without allocation. What would be the proposed convention for the signs of the Bezout coefficients?
I don't think there is an established convention here. For zero values, I presume you would set What is the use case for GCD of negative numbers? |
That would mutate the arguments, however. I don't think it is desirable to change If it worked as Normalizing fractions, computing content of polynomials, simplify integer matrices, all these work beyond natural numbers. |
Yes, it's not ideal that you would have to track the signs of For reference, GMP uses the first convention for the Bezout coefficients |
This as well but also accessing |
Disclaimer: No, don't do this ;)
|
Change https://golang.org/cl/164972 mentions this issue: |
The documentation for
That's unfortunate: it gives well-defined non-error behavior for an input that arguably should have been defined to be a user error. It also makes any change to That seems to suggest that the extended |
That is unfortunate. I don't think there should be a different GCD function that accepts zero or negative inputs. So that probably makes this a Go 2 proposal then. |
Even a Go 2 proposal will generally need to follow the guidelines from #28221 in order to be accepted. |
Doesn't
In particular, it would appear to be part of the penumbra-standard-library
Which seems to indicate this change would be possible in a future release. |
Given semantic import versioning, adding a |
IMHO, I don't think this warrants a new method. |
@TuomLarsen A bug is, generally speaking, a deviation of the behavior of a program from its intention. The fact that this behavior is explicitly documented, though, shows that this behavior is the intention - someone made the deliberate decision that the function should behave that way. As such, if you rely on it, you're also not relying on "inexact results" - it is clearly specified how the function behaves in this case and you're relying on that specification. In any case, this is actually documented (even though we're talking stdlib and not language, the same principles apply). This would at best fall under "specification error" and be incompatible. And the compatibility guarantee clearly states that they only happen for security issues. Bugs, to answer your question, can be fixed, even if it breaks code that relies on it. And as to @bcmills AIUI the intention was to keep this around as a "if we ever release (I have no opinion on my own whether this should have a new function. I think doing what's suggested would be the correct way for |
Per the discussion above, I have to agree that the new definition would indeed be problematic in the light of backward-compatibility. I am also not convinced that we should add a new function (GCD2) at this point. At any rate, it is easy for a client needing the extra functionality to just write that code. Leaving this on hold for now. |
It is true that the docs for GCD say:
but even before that, they say:
Kind of self-contradictory, but we currently forbid < 0 args, even if we also specify a behavior for them. |
Fair point; I did miss that. @bcmills Do you still have strong objections in light of this? Personally, I'd go forward with it. |
I still think it's a breaking change, but if you want to call it a specification error instead I'm ok with trying it out and seeing what breaks. |
@griesemer @bcmills Is there a final decision on this? As far as any practical effects, I don't foresee that any code was calling GCD with negative arguments and doing something specific with an all zero result. Likely, any code calling GCD with negative arguments would have been checking the sign of arguments before the call since the all zero result would not be what they wanted. |
Let's try this (and consider the current documentation at "specification error"). For Go 1.14. |
Thank you! |
Change https://golang.org/cl/217104 mentions this issue: |
Per the suggestion https://golang.org/cl/216200/2/doc/go1.14.html#423. Updates #28878. Change-Id: I654d2d114409624219a0041916f0a4030efc7573 Reviewed-on: https://go-review.googlesource.com/c/go/+/217104 Reviewed-by: Ian Lance Taylor <iant@golang.org>
Please consider adding support for zero and negative arguments to
math/big
GCD.As of version 1.11.1, to calculate GCD of two integers one has to write:
In other words, one needs two extra allocations (or copies) as the
nat
field ofbig.Int
is private.If on the other hand negative inputs were allowed the code would read:
For negative inputs the resulting GCD could be treated as GCD(|a|, |b|), the precise sign does not really matter as long as it is documented. (For example, "GCD is non-negative".)
While I'm at it, it would be also nice to support zero arguments as it is well defined (GCD(a, 0) = |a|, GCD(0, 0) = 0) and it would simplify the code because there would be no need to special-case zero inputs. Currently it sets the result to zero.
The text was updated successfully, but these errors were encountered: