-
Notifications
You must be signed in to change notification settings - Fork 997
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
get_custody_atoms: avoid application of modulo operation (%) to a negative value #1923
Comments
This is a very useful shorthand to get the number of missing elements to pad to a certain length. It's certainly unfortunate if we can't use it :( |
I have mixed feelings about it. An alternative is definitely bulky and this padding approach is short and elegant. However, we encounter more and more cases when concise is not clear (e.g. #1746 or #1924). In the particular case, the modulo operation is clearly defined in Python, which is great. However, people are people and make mistakes, the particular case is not obvious to humans. On the other side, bugs here are easy to reveal with appropriate tests. As a part of overall efforts to make the spec more formal and less ambiguous, my goal is at least to inform EF team about such ambiguities (and suggest a solution, when possible). |
We used to have this problem with Java and JavaScript is affected as well. Both, Java and JavaScript by default does it in the following way:
While Python follows the canonical math approach:
EDT: Thus Java spec also have this operation description: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3 -- in Java |
Many languages, including C, Rust, Java Nim, have the Either we define modulo like @mkalinin sugested, or we use explicit |
A couple of things:
I suggest we avoid this all together |
get_custody_atoms applies modulo operation to a negative dividend:
(-len(bytez) % BYTES_PER_CUSTODY_ATOM)
.Modulo operation implementations differ from language to language, when dividend (or divisor) is negative. Therefore that may lead to a bug in a language, which implements modulo operation in a different way than python does (i.e. returning a negative result when a dividend is negative).
@protolambda in a personal communication has suggested that such an ambiguity is better to be avoided in the spec.
The text was updated successfully, but these errors were encountered: