Re: Numeric precision in CSS numbers

On Dec 23, 2009, at 4:12 AM, Anne van Kesteren wrote:

> On Tue, 22 Dec 2009 20:29:30 +0100, Simon Fraser <smfr@me.com> wrote:
>> The section on Numbers in CSS3 Values and Units at <http://www.w3.org/TR/css3-values/#numbers> says nothing about numeric precision, other than some wording about values being restricted to a range for some properties.
> 
> Specifications typically do not state such limits as they can grow over time and depend on hardware constraints and such.

That sounds reasonable. There's still the question of what an implementation should do when it encounters a number that is larger than its internal representation can support. The options would seem to be:
a) clamp to the supported range
b) treat as invalid

I think the spec should say which one happens.

> 
>> For integers, it isn't stated how large values can get before they fail to parse, or fail to round-trip; for floating point values, it isn't specified how many significant digits are retained by parsing, and round-tripping. (By round-tripping, I mean setting the value via the CSS OM, and then reading it back.)
> 
> I guess it ought to say something about this, but I'd like it if we can do it without mentioning limits.

My suggested wording below attempts to deal with this.

> 
>> For both large integers and floating point values with many decimal places, implementations behave differently. Both WebKit and Gecko fall into scientific notation in some cases (e.g. set 'left' to '12345678px', and reading it back will give you '1.23457e+7px' which fails to parse if you in turn try to use it to set the value); Opera does not. WebKit and Gecko also retain 6 significant digits for floating point values; Opera, on the other hand, rounds to two decimal places.
> 
> Using scientific notation seems like a bug to me. (Though dino has suggested we adopt it in the past.)

On the WG call, we heard that the SVG folks have requested it in the past. Accepting scientific notion would be a change to the grammar, so that needs investigation.
>> 
>> Also, we need to be able to round-trip floating point numbers with high precision for CSS transforms.
>> 
>> There is a test page here (but note that it doesn't work in Opera because of value limiting):
>> <https://bug-30341-attachments.webkit.org/attachment.cgi?id=45276>
>> 
>> I think there are two things that we need to do:
>> 1. Be more explicit about the behavior of round-tripping large integer values, and floating point values with lots of significant digits.
> 
> I guess we could impose a minimum. I don't think we want to endorse serializing to scientific notation but still require it to fail to parse, but I don't think you're asking for that either :-)

David Baron had a suggestion here, captured in the wording below:

Suggested additional paragraph in CSS3 Values and Units, section 3.2/3.3 "Numbers" and "Non-negative numbers"
<http://www.w3.org/TR/css3-values/#numbers>

I moved some stuff into subsections in order to be able to add a subsection on numeric precision.

3.2. Numeric values

3.2.1. Numbers

Number values can either be integer or real numbers. Integer values are denoted by <integer> and real number values are denoted by <number>. Integers and real numbers are specified in decimal notation only. An <integer> consists of one or more digits "0" to "9". A <number> can either be an <integer>, or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both integers and real numbers may be preceded by a "-" or "+" to indicate the sign, and the sign character is considered part of the <number> value.

3.2.2. Non-negative numbers

Non-negative number values can either be integer or real numbers. Integer values are denoted by <non-negative integer> and real number values are denoted by <non-negative number>. Integers and real numbers are specified in decimal notation only. A <non-negative-integer> consists of one or more digits "0" to "9". A <non-negative-number> can either be an <integer>, or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both non-negative integers and non-negative real numbers may be preceded by a "+" to indicate the sign, but this is not required and conveys no useful information.

3.2.3 Clamping and precision

Many properties that allow an integer or real number as a value actually restrict the value to some range. If the restriction is to a non-negative value, then a non-negative number type is used. A user agent may clamp very large positive or negative values, and may not maintain the full precision of numbers in decimal notation.

Authors may access the value of a number after it has been processed by the user agent (for example, using the interfaces of the CSS Object Model). A value accessed in this way (the "accessed value") may have been clamped to a range, or have lost precision from the original value specified. However, this accessed value must "round-trip" exactly, if set and accessed a second time.


Simon

Received on Wednesday, 13 January 2010 18:58:18 UTC