Skip to content

Commit

Permalink
[css-values-4] Define how negative zero works in math functions. Fina…
Browse files Browse the repository at this point in the history
…lly closes #545.
  • Loading branch information
tabatkins committed Jun 14, 2018
1 parent 7935fa4 commit efc3452
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions css-values-4/Overview.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ Type Checking</h4>

Division by zero is possible,
which introduces certain complications.
[=Math functions=] follow simplified versions of IEEE-754 semantics for these operations:
[=Math functions=] follow IEEE-754 semantics for these operations:

* Dividing a positive value by zero produces +∞.
* Dividing a negative value by zero produces −∞.
Expand All @@ -1849,14 +1849,70 @@ Type Checking</h4>
produces NaN.
* Any operation with at least one NaN argument produces NaN.

If a <dfn export>top-level calculations</dfn>
(an argument to ''min()'', ''max()'', or ''clamp()'',
or a ''calc()'' that's not nested directly inside of another [=math function=])
Additionally,
IEEE-754 introduces the concept of "negative zero",
which must be tracked within a calculation
and between nested calculations:

* Negative zero
(0<sup>-</sup>)
can be produced literally by negating a zero
(''-0''),
or by a multiplication or division that produces zero
with exactly one negative argument
(such as ''-5 * 0'' or ''1 / (-1 / 0)'').

Note: Note that,
outside of [=math functions=],
''-0'' just produces a "standard" zero,
identical to ''0''--
CSS as a whole doesn't recognize the concept of signed zeros.
Negative zeros also don't escape a [=math function=];
as detailed below,
they're "censored" away into an "unsigned" zero.
* ''-0 + -0''
or ''-0 - 0''
produces 0<sup>-</sup>.
All other additions or subtractions that would produce a zero
produce 0<sup>+</sup>.
* Multiplying or dividing 0<sup>-</sup> with a positive number
(including 0<sup>+</sup>)
produces a negative result
(either 0<sup>-</sup> or −∞),
while multiplying or dividing 0<sup>-</sup> with a negative number
produces a positive result.

(In other words,
multiplying or dividing with 0<sup>-</sup>
follows standard sign rules.)
* When comparing 0<sup>+</sup> and 0<sup>-</sup>,
0<sup>-</sup> is less than 0<sup>+</sup>.
For example, ''min(0, -0)'' must produce 0<sup>-</sup>,
''max(0, -0)'' must produce 0<sup>+</sup>,
and ''clamp(0, -0, 1)'' must produce 0<sup>+</sup>.

If a <dfn export>top-level calculation</dfn>
(a [=math function=] not nested inside of another [=math function=])
would produce a NaN,
it instead produces +∞.
Other calculations
(such as a ''calc()'' nested directly within a [=math function=])
can produce NaN as normal.
If a [=top-level calculation=] would produce 0<sup>-</sup>,
it instead produces the standard "unsigned" zero.

<div class=example>
For example, ''calc(-5 * 0)'' produces an unsigned zero--
the calculation resolves to 0<sup>-</sup>,
but as it's a [=top-level calculation=],
it's then censored to an unsigned zero.

On the other hand, ''calc(1 / calc(-5 * 0))'' produces −∞,
same as ''calc(1 / (-5 * 0))''--
the inner calc resolves to 0<sup>-</sup>,
and as it's not a [=top-level calculation=],
it passes it up unchanged to the outer calc to produce −∞.
If it was censored into an unsigned zero,
it would instead produce +∞.
</div>


Note: Algebraic simplifications do not affect the validity of a [=math function=] or its resolved type.
For example, ''calc(5px - 5px + 10s)'' and ''calc(0 * 5px + 10s)'' are both invalid
Expand Down

0 comments on commit efc3452

Please sign in to comment.