Should xs: double to xs: decimal be used as BigDecimal.valueOf (double) or new BigDecimal (double)?

XQuery, shared with XSLT and XPath 2.0 and later, supports various data types, two of which are xs:doubleand xs:decimal. It can be distinguished xs:doubleto xs:decimal, as defined in http://www.w3.org/TR/xquery-operators/#casting-to-numerics .

Java implementations appear to be implemented xs:doubleusing the Java data type doubleand xs:decimalusing the class java.math.BigDecimal. This class supports two methods of converting doubleto BigDecimal, namely: BigDecimal.valueOf(doubleValue)and new BigDecimal(doubleValue). According to https://stackoverflow.com/a/166269/167/16 , the former gives a more intuitive result, while the latter gives a more correct result, for example, BigDecimal.valueOf(1.1)leads to 1.1, and the new BigDecimal(1.1)result 1.100000000000000088817841970012523233890533447265625.

When I try to apply xs:doubleto xs:decimalusing Saxon and Exist, then

xquery version "1.0";

let $d1 as xs:double := 1.1E0
return xs:decimal($d1)

outputs 1.100000000000000088817841970012523233890533447265625, and in BaseX it outputs 1.1. I believe the difference comes from different implementations, BaseX does BigDecimal.valueOf(1.1), Saxon and Exist do new BigDecimal(1.1).

: http://www.w3.org/TR/xquery-operators/#casting-to-numerics?

+4
2

xs: decimal 18 , , xs: decimal xs: double value 1.1 - 1.100 000 000 000 000 088, 1.1. ( 19 , 18, 18, 1,1 .) , 1.1 . 18 , 18 , .

public DecimalValue(double in) throws ValidationException {
        try {
            BigDecimal d = new BigDecimal(in);
            value = d.stripTrailingZeros();
        } catch (NumberFormatException err) {
            //...
        }
+3

XQuery , . , .

, , BigDecimal.valueOf(d) new BigDecimal(d) , BaseX (Version 8.0) .

+3

All Articles