Re: Floating point numbers in XPath
Hi Richard, > Is there any standard default rule governing the conversion of a > double to a string when the double is of the form "0.x1" (where x is > any number of 0s). i.e. should string(0.000001) return "0.000001", > "1e-6", "1e-06", "1e-006", etc, or does the spec leave this open as > implementation specific? If there is a specific rule hidden > somewhere, where is the cut-off point for the number of 0s to output > before the format changes to "1e-x"? The data type "double" doesn't technically exist in XPath 1.0 -- there are only strings, numbers, booleans and node sets. However, numbers in XPath 1.0 are double-precision 64-bit format IEEE 754 values. Numbers are converted to strings using the following rules: * NaN is converted to the string NaN * positive zero is converted to the string 0 * negative zero is converted to the string 0 * positive infinity is converted to the string Infinity * negative infinity is converted to the string -Infinity * if the number is an integer, the number is represented in decimal form as a Number with no decimal point and no leading zeros, preceded by a minus sign (-) if the number is negative * otherwise, the number is represented in decimal form as a Number including a decimal point with at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (-) if the number is negative; there must be no leading zeros before the decimal point apart possibly from the one required digit immediately before the decimal point; beyond the one required digit after the decimal point there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values. http://www.w3.org/TR/xpath#function-string As you can see, there's nothing about using scientific notation for the string value of a number in this description. XPath/XSLT 1.0 processors shouldn't use the syntax "1e-6", "1e-06", "1e-006", etc, but should use "0.000001". This means that aside from NaN, Infinity and -Infinity, the string value of a number can be converted back into a number using the number() function. You can change the format a little using the format-number() function top get a different string, but format-number() doesn't provide scientific notation either. A proper double value type will be introduced in XPath 2.0 (xs:double). Currently, it looks likely that you'll be able to create doubles with literals that use scientific notation:  DoubleLiteral ::= (("." [0-9]+) | ([0-9]+ ("." [0-9]*)?)) ([e] | [E]) ([+] | [-])? [0-9]+ http://www.w3.org/TR/xpath20/#doc-DoubleLiteral When you convert xs:double values to a string, they'll use the canonical lexical representation for xs:double as defined in the XML Schema: Datatypes Recommendation: The canonical representation for double is defined by prohibiting certain options from the Lexical representation (§22.214.171.124). Specifically, the exponent must be indicated by "E". Leading zeroes and the preceding optional "+" sign are prohibited in the exponent. For the mantissa, the preceding optional "+" sign is prohibited and the decimal point is required. For the exponent, the preceding optional "+" sign is prohibited. Leading and trailing zeroes are prohibited subject to the following: number representations must be normalized such that there is a single digit to the left of the decimal point and at least a single digit to the right of the decimal point. http://www.w3.org/TR/xmlschema-2/#double So the double 0.000001 should be turned into the string "1.0E-6" as far as I can tell. According to the XSLT 2.0 WD, the XSL WG will be adding support for scientific notation to the format-number() function as well (see http://www.w3.org/TR/xslt20/#issue-scientific-notation) which should give you more control over the format. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format