<!DOCTYPE TEI.2 PUBLIC '-//C. M. Sperberg-McQueen//DTD
          TEI Lite 1.0 plus SWeb (XML)//EN'
          'http://www.w3.org/People/cmsmcq/lib/swebxml.dtd' [
<!ATTLIST list type CDATA 'bullets'>
]>
<?xml-stylesheet type="text/xsl" href="../../People/cmsmcq/lib/swebtohtml.xsl"?> 
<?xml-stylesheet type="text/xsl" href="http://www.w3.org/People/cmsmcq/lib/swebtohtml.xsl"?> 
<TEI.2 rend="w3c-public">
<teiHeader>
<fileDesc>
<titleStmt>
<title>Impact of precisionDecimal on XPath and XQuery</title>
</titleStmt>
<publicationStmt>
</publicationStmt>
<sourceDesc>
<bibl>Don Chamberlin,
<title level="u">Impact of precisionDecimal on XPath and XQuery</title>
Email to w3c-xml-query-wg@w3.org, w3c-xsl-query@w3.org.
16 May 2006.
<xref>http://lists.w3.org/Archives/Member/w3c-xml-query-wg/2006May/0023.html</xref> 
</bibl>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<front>
<titlePage>
<docTitle>
<titlePart>Impact of precisionDecimal on XPath and XQuery</titlePart>
</docTitle>
<docAuthor>Don Chamberlin, IBM</docAuthor>
<docDate>16 May 2006</docDate>
</titlePage>
<divGen type="notoc"/>
</front>
<body>

<p><!--* Decimal data fans, *--> XML Schema 1.1 <ref
target="xsdl1.1">[1]</ref> introduces a new primitive datatype called
<name type="typename">xs:precisionDecimal</name>.</p>
<p>The new type is independent of
<name type="typename">xs:decimal</name> in the sense that neither is derived from the
other. A value of type <name type="typename">xs:precisionDecimal</name> includes both
a numeric value and a precision (number of significant digits to the
right of the decimal point.) For example, the precision of 0.12 might
be 2 while the precision of 0.1200 might be 4. The precision of 5000
might be 0 if all its digits are significant, or might be -3 if only
its first digit is significant. The new
<name type="typename">xs:precisionDecimal</name> type is defined to be aligned with
the floating-point decimal type expected to be defined in a
forthcoming revision of IEEE 754 called "754r" <ref
target="ieee754">[2]</ref>. Like <name type="typename">xs:float</name> and
<name type="typename">xs:double</name>, the new <name type="typename">xs:precisionDecimal</name> type
includes the special values negative zero, +INF, -INF, and NaN. XML
Schema 1.1 defines two lexical representations for
<name type="typename">xs:precisionDecimal</name> called
"<ident>decimalPtNumeral</ident>" (example: -0.0054) and
"<ident>scientificNotationNumeral</ident>" (example: -5.4E-3). XML
Schema 1.1 defines a <soCalled>lexical mapping</soCalled> that maps a
lexical representation in either <ident>decimalPtNumeral</ident> or
<ident>scientificNotationNumeral</ident> form into the value space of
<name type="typename">xs:precisionDecimal</name> (inferring the precision from the
number of digits after the decimal point). XML Schema 1.1 also defines
a <soCalled>canonical mapping</soCalled> that maps a precisionDecimal
value into its canonical lexical representation, which may be either a
<ident>decimalPtNumeral</ident> or a
<ident>scientificNotationNumeral</ident> (generally scientific
notation is used for very small or very large values; otherwise the
<ident>decimalPtNumeral</ident> format is used.) 
</p>
<p>
More information about the <name type="typename">precisionDecimal</name> type,
including motivation for why this type was introduced, can be found in
<!--* an attachment to this note. *-->
<ref target="decifaq" type="bibref">[3]</ref>.
</p>
<p>
This memo explores the impact of the new
<name type="typename">xs:precisionDecimal</name> type on XPath and XQuery. It
identifies some issues that will need to be resolved and explores some
alternative solutions. The following conventions are used:
<list>
<item>The <name>xs:</name> prefix is generally omitted from the names
of types (<name type="typename">xs:decimal</name> etc.)</item>
<item>"<name type="typename">precisionDecimal</name>" is abbreviated as
"<name type="typename">pDecimal</name>"</item>
<item>"We" refers to the joint Query and XSLT working groups.</item>
</list>
</p>
<p>
(1) We will need to define where <name type="typename">pDecimal</name> fits into the
numeric promotion hierarchy. Proposal: put <name type="typename">pDecimal</name>
between <name type="typename">decimal</name> and <name type="typename">float</name>. In other words,
<name type="typename">decimal</name> is promotable to <name type="typename">pDecimal</name>, and
<name type="typename">pDecimal</name> is promotable to <name type="typename">float</name>. This has
the following implications:
</p>
<p>
(a) Any function that expects a <name type="typename">pDecimal</name> parameter can be
called with a <name type="typename">decimal</name> or <name type="typename">integer</name> argument.
</p>
<p>
(b) Any function that expects a <name type="typename">decimal</name> parameter cannot
be called with a <name type="typename">pDecimal</name> argument. Currently, there are
no such functions in the XPath/XQuery built-in function library.
</p>
<p>
(2) We will need to define the rules for how <name type="typename">decimal</name> and
<name type="typename">integer</name> values are promoted to <name type="typename">pDecimal</name>
values. One possible way to do this is to convert the input value to
its canonical lexical form and then apply the lexical mapping defined
by XML Schema 1.1 to convert the lexical form into a
<name type="typename">pDecimal</name> value. Another possible way is to assume a
fixed, implementation-defined precision for each of the existing
types, and adopt this as the precision of the resulting
<name type="typename">pDecimal</name>.
</p>
<p>
(3) In F&amp;O, we will need to define the semantics of the following
arithmetic functions on the <name type="typename">pDecimal</name> type:
<list type="simple">
<item>op:numeric-add</item>
<item>op:numeric-subtract</item>
<item>op:numeric-multiply</item>
<item>op:numeric-divide</item>
<item>op:numeric-mod</item>
<item>op:numeric-unary-plus</item>
<item>op:numeric-unary-minus</item>
</list>
</p>
<p>
Part of this definition will involve specifying the precision of the
result, based on the precision of the operands. Presumably this can be
done by reference to the forthcoming IEEE 754r standard. In general,
the precision of the result of a decimal operation is at least as
great as the greater of the precisions of its operands. For example,
if (1000 with precision -3) is added to (.001 with precision 3), the
result is (1000.001 with precision 3). Each operator (addition,
multiplication, etc.) defines its own precision rules. It is also
necessary to deal with many special cases (what is the result of -INF
modulus negative zero, etc.) Again, this can be done by reference to
IEEE 754r.
</p>
<p>
One important case deals with arithmetic between a
<name type="typename">pDecimal</name> value and a value of some other type. For
example, suppose that a <name type="typename">pDecimal</name> value is added to a
<name type="typename">decimal</name> value (whose precision is not explicit). Should
the result get its precision from the <name type="typename">pDecimal</name> operand?
Or should the <name type="typename">decimal</name> operand be promoted to a
<name type="typename">pDecimal</name> value (with somewhat arbitrary precision) and
then the precision of the result computed based on
<name type="typename">pDecimal</name> rules?
</p>
<p>
An expert on XQuery Functions and Operators should read the draft 754r
standard carefully. It contains some surprises, which apply to both
binary and decimal formats, such as the following: 
</p>
<p>
In 754r Section 5.10 we read "totalorder(-NaN, number) is true where
-NaN represents a NaN with negative sign bit". (But there is no
negative NaN value in XML Schema.)
</p>
<p>
In 754r Section 6.2 we read "Two different kinds of NaN, signaling and
quiet, shall be supported in all operations." (This concept also does
not exist in XML Schema. Possibly the distinction between signalling
and quiet NaN's can be ignored in XPath/XQuery.)
</p>
<p>
(4) In F&amp;O, we will need to define the behavior of the following
comparison functions on the <name type="typename">pDecimal</name> type:
<list type="simple">
<item>op:numeric-equal</item>
<item>op:numeric-less-than</item>
<item>op:numeric-greater-than</item></list>
</p>
<p>
These semantics can be based on the ordering rules for
<name type="typename">pDecimal</name> given in Schema 1.1. Basically, two
<name type="typename">pDecimal</name> values are compared based on their numerical
values, disregarding precision (for example, 5 is equal to 5.0000).
Two zeros with different signs are equal. INF is equal to itself and
greater than all other values except NaN. -INF is equal to itself and
less than all other values except NaN. NaN is not comparable to any
other value, including itself.
</p>
<p>
(5) We will need to define how <name type="typename">pDecimal</name> values
participate in "order by" operations. Presumably, when ordering two
values of dissimilar type, the values are promoted to their "greatest
common type." For this purpose, is <name type="typename">pDecimal</name> INF
considered equal to <name type="typename">float</name> and <name type="typename">double</name> INF?
Are <name type="typename">pDecimal</name> NaN values considered equivalent to
<name type="typename">float</name> and <name type="typename">double</name> NaN values? Does a
<name type="typename">pDecimal</name> NaN have a sign for ordering purposes? (I think
754r says Yes and Schema 1.1 says No.) In considering these questions,
we should probably investigate how the same questions are being
handled in the ISO SQL Standard. 
</p>
<p>
(6) We will need to define how to represent a literal
<name type="typename">pDecimal</name> value in a query. In the simplest approach, we
would not define a new literal format, but simply rely on the
constructor function named <code>xs:precisionDecimal()</code> that
accepts any valid lexical representation and applies the lexical
mapping defined by XML Schema 1.1 (example:
<code>xs:precisionDecimal("5.000")</code> maps into a
<name type="typename">pDecimal</name> value with a precision of 3). We could consider
additional measures such as defining a new literal format, similar to
our current <name type="typename">double</name> literals but using a different letter
for the exponent, such as "1.23D-5".
</p>
<p>
(7) We will need to define the semantics of the following numeric
functions for <name type="typename">pDecimal</name> operands, including the precision
of the result:
<list>
<item>fn:ceiling()</item>
<item>fn:floor()</item>
<item>fn:round()</item>
<item>fn:round-half-to-even()</item>
<item>fn:avg()</item>
<item>fn:max()</item>
<item>fn:min()</item>
<item>fn:sum()</item>
</list>
</p>
<p>
Again, the IEEE 754r draft has something to say about these functions,
which should be considered carefully. We might also consider adding a
new function that implements the rounding mode called "Round to
Nearest, Ties Away from Zero" (defined in IEEE 754r). For aggregating
functions such as fn:max and fn:avg, we will need to give special
attention to the case where <name type="typename">pDecimal</name> values are mixed
with values of other types.
</p>
<p>
(8) We will need to define general rules for overflow and underflow in
<name type="typename">pDecimal</name> arithmetic, including intermediate results.
These rules might be specified to be consistent with the current rules
for <name type="typename">decimal</name> or the current rules for <name type="typename">double</name>
(these sets of rules are not the same). The <name type="typename">pDecimal</name> type
probably resembles <name type="typename">double</name> more than <name type="typename">decimal</name>
because of the presence of INF and -INF. The IEEE 754r draft also
talks about overflow and underflow semantics, which are influenced by
a concept called "rounding mode" that does not currently exist in
XPath/XQuery.
</p>
<p>
(9) We will need to define the casting rules for
<name type="typename">pDecimal</name>. What types can be cast into
<name type="typename">pDecimal</name> and into what types can it be cast? I expect
that the rules will be similar to those for the existing
<name type="typename">decimal</name> type. On casting other types into
<name type="typename">pDecimal</name>, we need to specify how the precision of the
result is determined. On casting <name type="typename">pDecimal</name> into other
types, we need to specify what happens to special values like negative
zero, INF, and NaN. Casting <name type="typename">pDecimal</name> into string should
probably be based on the canonical mapping defined by XML Schema 1.1.
This mapping sometimes produces "<ident>decimalPtNumeral</ident>"
notation (example: -0.0054) and sometimes produces
"<ident>scientificNotationNumeral</ident>" notation (example:
-5.4E-3). We may choose to define additional specialized
cast-to-string functions that force one or the other of these
notations (but note that forcing <ident>decimalPtNumeral</ident>
notation may result in very long strings.)
</p>
<p>
(10) Note that the XQuery rules for SequenceType Matching respect
subtypes (for example, an <name type="typename">integer</name> value matches the
<name type="typename">decimal</name> type) but not promotion (for example, a
<name type="typename">decimal</name> value does not match the <name type="typename">double</name>
type). Essentially, <name type="typename">decimal</name> and <name type="typename">pDecimal</name> are
different types in the same sense that <name type="typename">float</name> and
<name type="typename">double</name> are different types. This is not a problem, but we
should be aware of the consequences, including the following:
</p>
<p>
(a) If a variable is declared to have type <name type="typename">pDecimal</name>, it
cannot be assigned a <name type="typename">decimal</name> value, and vice versa.
</p>
<p>
(b) "instance of <name type="typename">pDecimal</name>" returns false for
<name type="typename">decimal</name> values and vice versa.
</p>
<p>
(c) typeswitch expressions need separate branches for
<name type="typename">decimal</name> and <name type="typename">pDecimal</name>.
</p>
<p>
(d) "treat as <name type="typename">pDecimal</name>" will not accept
<name type="typename">decimal</name> values at run-time, and vice versa
</p>
<p>
(e) In a path expression, the node-test <code>element(*,
xs:decimal)</code> will not match an element of type
<name type="typename">pDecimal</name> and vice versa. Similarly, the node-test
schema-element(IQ) will not match an element named IQ of type
<name type="typename">xs:decimal</name> if the schema definition of IQ calls for
<name type="typename">pDecimal</name>, and vice versa.
</p>
<p>
(11) We will need to define rules for the minimum
<name type="typename">pDecimal</name> precision required of conforming implementations
(XML Schema 1.1 has some rules about this, which we could choose to
copy.)
</p>

</body>
<back>
<div>
<head>References</head>
<listBibl>
<bibl id="xsdl1.1" n="[1]">[1]
World Wide Web Consortium (W3C). 
<date value="2001-05-02">2006</date>.
<title level="m">XML
Schema 1.1 Part 2: Datatypes</title>, ed.
<editor>David Peterson</editor>,
<editor>Paul V. Biron</editor>,
<editor>Ashok Malhotra</editor>,
and 
<editor>C. M. Sperberg-McQueen</editor>.
W3C Working Draft 17 February 2006
[Cambridge, Sophia-Antipolis, and Tokyo]: World Wide Web Consortium.
<xref>http://www.w3.org/TR/xmlschema11-2/</xref>
</bibl>

<bibl n="[2]" id="ieee754">[2]
IEEE (Institute of Electrical and Electronics Engineers).
<date>2001-2007</date>.
<title>Draft Standard for Floating-Point Arithmetic P754</title>. 
Various drafts, 2001-2007.
At the time of publication, the most recent drafts, reflecting the
resolution of ballot comments, are not publicly available.  See
<xref>http://754r.ucbtest.org/drafts/archive/</xref> for an archive of
older committee drafts.
</bibl>

<bibl n="[3]" id="decifaq">[3]
Cowlishaw, Michael.
<date>2007</date>.
<title> Decimal Arithmetic FAQ
(Frequently Asked Questions)</title>. 
21 April 2007.
On the Web at 
<xref>http://www2.hursley.ibm.com/decimal/decifaq.html</xref>.
</bibl>
</listBibl>
</div>
</back>
</text>
</TEI.2>
<!-- Keep this comment at the end of the file
Local variables:
mode: xml
sgml-default-dtd-file:"/Library/SGML/Public/Emacs/sweb.ced"
sgml-omittag:t
sgml-shorttag:t
End:
-->
