[css-values-4] calc() function with real number halfway between 2 adjacent integers

NEW
Unassigned

Status

()

enhancement
P3
normal
3 months ago
2 months ago

People

(Reporter: bugzilla, Unassigned)

Tracking

(Blocks 2 bugs, {testcase})

Trunk
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(firefox67 affected)

Details

Reporter

Description

3 months ago

When a calc() function's simplified expression is a real number halfway between 2 adjacent numbers and the property accepts only an integer, then such simplified expression should be rounded _ towards positive infinity _.

"
if a math function that resolves to <number> is used somewhere that only accepts <integer>, the computed value and used value are rounded to the nearest integer, in the same manner as clamping, above. The rounding method must be the same as is used for animations of integer values.
"
CSS4 Values, section 9.1.4. Range Checking
https://www.w3.org/TR/css-values-4/#calc-range

"
(...) 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

Test:
z-index: calc(-3 / 2); /* -1.5 should be rounded up to -1 */
http://w3c-test.org/css/css-values/calc-z-index-fractions-001.html

More info in this thread:
http://lists.w3.org/Archives/Public/www-style/2018Dec/0027.html

Right now, no browser passes the
z-index: calc(-3 / 2)
test.

Reporter

Updated

3 months ago
Blocks: 1376206
Keywords: testcase

Are browsers interoperable? If so, maybe it would be better to change the spec.

Yes, this is how I implemented bug 1440251 I guess. We could change that from round() to (n + 0.5).floor().

Though I'd appreciate to know the reasoning for this behavior (I asked in the www-style thread above).

Reporter

Comment 3

3 months ago

Browsers do not interoperate:

https://wpt.fyi/results/css/css-values/calc-z-index-fractions-001.html?label=stable&aligned

https://wpt.fyi/results/css/css-values/calc-z-index-fractions-001.html?label=master&aligned

"Most programming languages round halfway values away from zero,
Javascript's Math.round being the notable exception (...)"
http://lists.w3.org/Archives/Public/www-style/2018Dec/0026.html

So I checked this specific issue with JS:

-----------------------------------------
|      |          |          |          |
|      |Math.floor|Math.round|Math.ceil |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| 3.1  |     3    |     3    |     4    |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| 3.5  |     3    |     4    |     4    |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| 3.9  |     3    |     4    |     4    |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| -1.1 |    -2    |    -1    |    -1    |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| -1.5 |    -2    |    -1    |    -1    |
|      |          |          |          |
-----------------------------------------
|      |          |          |          |
| -1.9 |    -2    |    -2    |    -1    |
|      |          |          |          |
-----------------------------------------

Firefox 60.4.0 ESR and Chromium 71 both report:

Math.floor(-1.1)
-2
Math.floor(-1.5)
-2
Math.floor(-1.9)
-2
Math.round(-1.1)
-1
Math.round(-1.5)
-1
Math.round(-1.9)
-2
Math.ceil(-1.1)
-1
Math.ceil(-1.5)
-1
Math.ceil(-1.9)
-1

and

Math.round(1.5)
2

Anyway... I have no preference either way on this issue and on changing the spec.

Priority: -- → P3
You need to log in before you can comment on or make changes to this bug.