[xsd-users] xs:double truncation

Boris Kolpackov boris at codesynthesis.com
Fri Jun 20 12:10:03 EDT 2008


Hi Aaron,

Aaron Bray ARA/SED <abray at ara.com> writes:

> I set value to 3.07391836376424E-30 in code.
> 
> When serialized to file I get the value as 0
> 
> This is not acceptable. Something is truncating my double value.

We are trying to balance between writing too much (e.g., some users
expect 9999.9999 and get -9999.999900000000707) and too little (your
case) information while serializing floating-point numbers. It has
been tricky to satisfy everybody. 

In XSD 3.1.0 by default we serialize the number of decimal digits
that the type can represent without change as opposed to just the
number of potentially significant decimal digits (that's what XSD
2.x.x uses by default). See the following paper for some background
information:

http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf

You can achieve the same behavior as in 2.x.x by compiling your code
with XSD_FP_ALL_DIGITS macro defined. It is also possible to customize
the build-in floating point types (xsd:float, xsd:double, xsd:decimal)
as well as their parsing and serialization routines in order to implement
custom precision. The following example shown how to do this:

http://www.codesynthesis.com/~boris/tmp/custom-double.zip

Let me know if none of this helps.

Boris




More information about the xsd-users mailing list