W3C home > Mailing lists > Public > www-style@w3.org > December 2018

Re: [css-values] Rounding a real number to an integer

From: Emilio Cobos Álvarez <emilio@crisal.io>
Date: Wed, 19 Dec 2018 02:33:01 +0100
To: www-style@w3.org
Message-ID: <f7709bbe-4077-f50f-8293-c235831744a2@crisal.io>
On 12/19/18 1:58 AM, fantasai wrote:
> On 12/15/18 6:46 PM, Gérard Talbot wrote:
>> Le 2018-12-15 19:46, Gérard Talbot a écrit :
>>> Hello,
>>>
>>> "
>>> (...) interpolation happens in the real number space as for <number>s,
>>> and the result is converted to an <integer> by rounding to the nearest
>>> integer, with *_values halfway between adjacent integers rounded
>>> towards positive infinity_*.
>>> "
>>> CSS4 Values, section 5.1.1. Combination of <integer>
>>> https://www.w3.org/TR/css-values-4/#combine-integers
>>>
>>> So
>>>
>>> 'z-index: -1.5'
>>> should be converted to 'z-index: -1' and not to 'z-index: -2'
>>> while
>>> 'z-index: 1.5'
>>> should be converted to 'z-index: 2'.
>>
>> Oops.. I meant
>>
>> 'z-index: calc(-1.5)'
>> should compute to 'z-index: -1' and not to 'z-index: -2'
>> while
>> 'z-index: calc(1.5)'
>> should compute to 'z-index: 2'.
>>
>>>
>>> Is my interpretation of the spec correct?
> 
> Afaict it is correct. If that's not what implementations do,
> though, let us know as we may need to adjust the spec. :)

Most programming languages round halfway values away from zero, 
Javascript's Math.round being the notable exception (JS rounds the usual 
way when you use (-1.5).toFixed(0), which is somewhat inconsistent...).

That's also what at least Gecko does (not sure if Blink or WebKit 
already support <number>s where <integers> are required in calc), just 
as a side effect of using the standard round() function in Rust[1].

Was this spec text intentional just to match JS, or for other reason?

I'd prefer not to match Javascript here, if possible... I just learnt 
today about JS doing this and it has been pretty much a surprise (plus 
it's somewhat inconsistent, see the toFixed() example above).

And (selfishly, arguably) I'd be sad if I had to stop using standard 
Rust / C++ rounding functions and re-implement our own just for the 
style engine because of this requirement.

That being said, we didn't implement this that long ago[2], and AFAIK we 
were the only engine doing so at the time, so there may be some leeway 
to change Gecko here if we decide it's the right thing to do.

  -- Emilio

[1]: 
https://searchfox.org/mozilla-central/rev/13788edbabb04d004e4a1ceff41d4de68a8320a2/servo/components/style/values/specified/calc.rs#532
[2]: https://bugzilla.mozilla.org/show_bug.cgi?id=1440251

> ~fantasai
> 
> 
Received on Wednesday, 19 December 2018 01:33:25 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 19 December 2018 01:33:26 UTC