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
[css-values] round(A,B) with B negative #4718
Comments
The current definition of round() is correct. The quote is wrong; I tried to simplify the fuller expression of
No, changing the sign of a mod/rem result is never correct. Use a value other than 2/-2 in your tests and you'll see that immediately. ^_^ |
Sure, that's what happens when I write a long post in a hurry, something is gonna be wrong :) I think the mostly correct one would be
e.g.
the problem is with 0:
|
I'm okay with that; addition isn't commutative with respect to -0 in IEEE semantics, so I'm not surprised it doesn't come out correctly from such an expression. I'll note that explicitly. |
Maybe add a note clarifying that |
I'm not sure what you mean, it seems to me that |
Addition does produce Subtracting is the real weird one, tho: only I think you're right that commutitivity isn't actually the law being broken there; I guess it's additive inverses don't work correctly around zeros (which is why flipping the sign like that isn't guaranteed to work right). |
When rounding a value A with a negative precision B, I think there are two reasonable approaches:
Ignore the sign, so that
We find the two integer multiples of B closest to A, "lower B" and "upper B", with "lower B" < "upper B". And then we choose between by applying the strategy to "lower B" and "upper B":
nearest
, choose whichever of "lower B" and "upper B" that is closer to A. In case of tie, choose "upper B".up
, choose "upper B".down
, choose "lower B".to-zero
, choose whichever of "lower B" and "upper B" that is closer to 0.In JS, for finite B,
Use the sign to 'reverse' the strategy, so that
We find the two integer multiples of B closest to A,
B*n
andB*(n+1)
. And then we choose between them by applying the strategy ton
andn+1
:nearest
, choose whichever ofB*n
andB*(n+1)
that is closer to A. In case of tie, chooseB*(n+1)
.up
, chooseB*(n+1)
.down
, chooseB*n
.to-zero
, chooseB*n
ifn
is closer to 0 thann*1
, elseB*(n+1)
.In JS, for finite B,
Note there is no difference for
to-zero
. And fornearest
it only matters in case of tie.Currently the spec defines
round()
with (1). However, later it seems to assume (2):The former doesn't hold with (1), e.g.
mod(3, -2)
is supposed to be-1
, but3 - round(down, 3, -2)
is3 - 2 = 1
. But it works with (2):3 - 4 = -1
. With (1), I guess the quote should say(A - round(down, A, B)) * sign(B)
So either keep the definition of round() as-is and fix the quote, or change the definition of round() and keep the quote. And add a note.
The text was updated successfully, but these errors were encountered: