[Bug 2644] [F+O] Conversion from float/double to string

http://www.w3.org/Bugs/Public/show_bug.cgi?id=2644





------- Additional Comments From mike@saxonica.com  2006-01-17 21:10 -------
After doing some further work on this, I'm going to propose three options.

Option A: don't prescribe the rounding rules, except that the value must be a
lexical representation of the original float or double (to allow round-tripping
without loss of precision)

Option B: specify that the value must be an exact decimal representation of the
original binary value: no rounding or truncation of significant digits is
allowed even if the resulting value would round-trip to the original value.
(This appears to be what Java does, as distinct from what it says it does.)

Option C: specify the rounding rules. Here is text that does that:

In 17.1.2, delete the text

"Besides these special values, the general form of the canonical form for
xs:float and xs:double is a mantissa, .......... the value from all other values
for the datatype after rounding the final digit."

replacing it with:

"For other values, the canonical representation consists of a minus sign '-'
(x2D) if the value is negative, followed by the magnitude m (absolute value),
represented as follows. Let n be the unique integer such that 10^n <= m <
10^(n+1); then let a be the mathematically exact quotient of m and 10^n so that
1 <= a < 10. The magnitude is then represented as the integer part of a, as a
single decimal digit, followed by '.' (x2E), followed by decimal digits
representing the fractional part of a, followed by the letter 'E' (x45),
followed by a representation of n as a decimal integer, as produced by the rules
for converting xs:integer to xs:string. Suppose that the string of decimal
digits that exactly represents the fractional part of a is S. So long as the
length of S is at least one, if rounding of the last digit in S results in a
string that is a lexical representation of the original xs:float or xs:double
value, then such rounding is performed; and this process is repeated. The
rounding is done up or down according to the rules of the
fn:round-half-to-even() function.

Note: I haven't reproduced the Java words exactly here, because I don't actually
think the Java words are precise. In any case, Java doesn't seem to do what the
spec says it should. (They actually use the same phrase "as many, but only as
many, more digits as are needed to uniquely distinguish" that appears in XPath
1.0, and then embellish them with a further explanation: but the explanation
doesn't seem to explain the results actually produced). I couldn't find a
description of the rules for C#, but the actual behaviour of C# appears to lose
precision - the result of round-tripping a float to a string and back is not
necessarily the original float, which is one of our objectives (but I don't know
the language well and might have missed something). My aim here is to reflect
the intent of the current words, and simply give a precise interpretation of
what I think they were probably intended to mean - not necessarily the only
interpretation possible.

Received on Tuesday, 17 January 2006 21:10:09 UTC