[xsd-users] Double trouble

Esben Skovenborg EsbenS at TCElectronic.com
Tue Jan 15 12:37:28 EST 2008


	Hi,

I have a question regarding XSD 3.0 and its serialization of
double-precision numbers. My application (the XML requirements of which
seem to be elegantly handled by the XSD:) needs to serialize and parse
doubles in the full range and precision of 64-bit doubles. 

A recent thread on xsd-users ("losing double precision in output xml")
refers to a patch (xsd-2.3.0-fp-serialization-patch) which indicate that
XSD 2.3 mapped the xsd:double into double, but xsd:decimal into long
double. In version 3, both xsd types are mapped into double, which
apparently forces the serializer to not use exponential notation at all.

I note that "We map both xsd:double and xsd:decimal to double and
decimal cannot be in scientific notation" (from
xsd/cxx/tree/serialization.hxx), however the present solution seems to
severely limit the range of the numbers that can be represented. 

For my application, the printf %g format is essentially what's needed
for serializing xsd:double -- hence my quick'n'dirty fix:

  operator<< (xercesc::DOMElement& e, double d)
  {
    char strbuf[32];
    sprintf(strbuf, "%.16g", d);

    std::string s (strbuf);
    e << s;
  }


Now, I'm not sure if there's a better way of achieving this, or how to
have different << operators for the xsd:double and xsd:decimal. Perhaps
the long double solution in XSD 2.3 was a pretty good idea after all?


Kind regards,

	Esben




More information about the xsd-users mailing list