Conclusions XPath.07 * 100 * 100 as 700.00000000000011 why?

I noticed some strange precision behavior that I cannot understand, I have XML:

<CLD> <UCRV> <UCR1>.07</UCR1> </UCRV> </CLD> 

And in the XSLT file, I select the value as a penny (or 100 pence, as it seems, I don’t know why, but this is what the client wants!) As:

 <xsl:value-of select="./s0:CLD/s0:UCRV/s0:UCR1/text() * 100 * 100"/> 

But this is output as 700.00000000000011. The data type is xsd: Decimal. Is there a default accuracy limit here? I can just round the number, but I just wanted to understand it a little better.

thanks

+7
source share
3 answers

Floating point numbers cannot represent everything exactly. Since numbers are stored in binary form, numbers that seem easy to write in decimal can sometimes only be found in binary format. This applies to 0.07, it is stored internally as 0.070000000000000011, as it seems in your case. Generally, you should never trust floating point values ​​to compare them directly without rounding.

+5
source

As people have already explained, the only numeric type used in XPath is xs: double .

The number() function converts the value to double. Therefore, operations with numbers in XPath 1.0 can and sometimes lead to a loss of precision.

There are various solutions :

Note that XPath 2.0 supports all XSD numeric types , including xs:decimal . An XPath 2.0 expression that does not cause loss of precision:

 xs:decimal(0.07)*100*100 
+4
source

This is standard floating point math. This has the maximum number of bits in order to match the number, and the number is stored in scientific form in binary format. Here 0.07 cannot be represented exactly and is held as 0.070000000000000011.

For more information see many questions about SO (albeit in other languages) using the tag, wikipedia and What Every computer scientist should know about floating-point arithmetic

+3
source

All Articles