-
Notifications
You must be signed in to change notification settings - Fork 192
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
Integer division behavior with negative result is not unified #746
Comments
Related: The behavior of
No matter which integer division behavior we choose, we should also at the same time define the behavior of To visualize this, here's a table with the integers in the range Table to compare division and modulo behaviors
|
Personally I prefer the floor division and "wraparound" modulo behavior. When you divide a range of integers by a constant divisor, the results fall nicely into groups that are all the same size (unlike with truncating division, where the group with result 0 is larger than the other groups). I also think it makes more sense to always have modulo return a number in the range Though maybe I'm biased here, because I'm primarily a Python user and this is Python's behavior for integer division and modulo. :) |
@dgelessus Actually, @generalmimon Totally agree that We can probably start with |
I don't think that's a good idea if we've already decided that |
Looking at the table created by @dgelessus, maybe I'm more inclined to the
We're using the modulo behavior 2, so it doesn't really make sense to have truncated integer division ( @GreyCat Have you some specific reason why prefer the truncated division, other than that it is used by the majority of languages? Again, we don't have to reflect the majority behavior if it doesn't suit for us (this sentence is becoming my mantra 😄). The languages that truncate the integer division have their own reasons for doing so, and they mostly use the modulo behavior 1, so it makes sense for them, but it doesn't for us. |
By the way @generalmimon I'm curious where you got the division behavior for Lua, JavaScript, Perl and PHP from. In all of these languages, dividing two integers performs a float division instead, so you cannot perform an integer division without explicitly truncating or rounding the result. If you go by the behavior of their modulo operators, JavaScript and PHP say |
@dgelessus I'm talking about the actual behavior of the KSC-generated code (i.e. the result that users get when they e.g. create a value instance with |
@dgelessus @generalmimon Thanks for explanation! Totally agree that it's better to choose most consistent behavior. " |
The integer division operation currently doesn't behave the same way in all languages, if the quotient (division result) is a negative number. Here's a summary table (
-20 / 12 = -1.666...
;69 / -20 = -3.45
):-20 / 12
69 / -20
The User Guide says this:
But that says nothing about how the division on integers should behave on negative results. There are two options: true integer division always rounding to the closest lower number (
math.floor(a / b)
) and a float division, whose result will be converted (truncated) to integer (which means trucating towards 0). The target languages now use both these options, so it would be nice to unify it to ensure same results for different KS targets.But I'm actually 100% not sure which approach should be used. Probably the one with integer conversion makes more sense, because it expresses the intention better. The KS expression language doesn't want to yield a float when the operands are integers, so it's natural to calculate the result as float and then cast it to integer. That would imply that the behavior of JavaScript, Lua, Python and Ruby is incorrect.
Is that right? Strictly speaking, it's a BC break with 0.8, but as @GreyCat said here that we've already done that, so I guess it's OK?
The text was updated successfully, but these errors were encountered: