Re: [csswg-drafts] [css-values] mod() isn't needed (#6637)

> This area is already confusing due to the mod operator % being named remainder in javascript,

It's named that because that's what the operation it's performing is called, which is why CSS calls the function implementing that behavior `rem()`. ^_^ The modulus operation works the way `mod()` does.

A number of languages have one behavior or the other, and give the operation one name or the other; it's pretty inconsistent across programming languages. We did a survey of languages and mathematical resources, and figured that remainder/modulus was, overall, the most accepted pair of names.

>  Also, the mod function that the spec dictates (drafts.csswg.org/css-values-4/#round-func) is a weird combination of multiple math functions that I don't believe exists in any language.

I'm not sure what you mean - are you referring to the note at the end that explains how one can do `mod()` and `rem()` using only the other math functions? That's not the actual definition (it's an explanatory note), but also all of the operations used there are standard math functions: arithmetic, "round down" (floor), "round to zero" (truncate) and sign. C and JS both have all of these.

> There is no need to be more expressive than javascript, which only has the % operator, so there is no reason to have any additional functions for this beyond rem().

As Oriol said (and the spec explains, in the note titled "Why Should I Choose mod() vs rem()?"), the "modulus" operation is the form of the operation that's taught in math classes when learning about modular arithmetic, and is generally more useful and intuitive to people. JS's use of the remainder operation for `%` (to match C/Java/etc) is a source of *perennial* confusion for script authors, and commonly necessitates the annoying `((val % N) + N) % N` sequence to actually get the "modulus" behavior that people want.

Wikipedia goes into (as usual for math articles) too much detail about this, but at least the ["Common Pitfalls" section](https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls) is short and sweet, illustrating the issue we're trying to avoid by recommending people generally use `mod()`.

-- 
GitHub Notification of comment by tabatkins
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/6637#issuecomment-929705584 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 28 September 2021 23:52:22 UTC