[xsd-users] comparison operation

Boris Kolpackov boris at codesynthesis.com
Thu Aug 28 16:19:44 EDT 2008


Hi Ray,

Rizzuto, Raymond <Raymond.Rizzuto at sig.com> writes:
 
> Is there a way to use the new 3.2.0 feature that address the precision
> of float/double to "fix" the representation issue?
> 
> I.e. if my original double is rounded to XSD_CXX_TREE_DOUBLE_PRECISION
> places.  If I take the rounded value, serialize and deserialize it via
> my XSD-generated library, would the deserialized value be guaranteed to
> compare equal to the rounded version I serialized?

This is a tricky question. First, even if in your program the decimal
representation of a floating point number is rounded to some number of
digits, there is no guarantee that the actual binary representation
will contain the same number of digits. Here is an example (taken from
the paper mentioned below):

float f = 3.1459; // the actual value stored in f is 3.1459001

I believe the way to achieve what you want is to serialize all
potentially significant digits as explained in this paper:

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

You can achieve this in XSD 3.2.0 by defining the following macros
when compiling your program:

XSD_CXX_TREE_FLOAT_PRECISION_MAX
XSD_CXX_TREE_DOUBLE_PRECISION_MAX
XSD_CXX_TREE_DECIMAL_PRECISION_MAX

There is, however, another potential problem: it does not seem to
be guaranteed that the C++ runtime will always be able to preserve
all the digits when reading a floating point number serialized with
such a precision. I remember reading on some mailing list that the
VC++ runtime is unable to round-trip without loss in some cases (I
think it was one of the Boost mailing lists, if you would like, I
can try to find those posts).

One alternative would be to customize the float/double/decimal
types and implement their parsing/serialization using floating
point routines that are known to preserve all potentially
significant digits during serialization and parsing. One such
library seems to be netlib/fp though I haven't used it myself:

http://www.netlib.org/fp/index.html

The 'custom/double' example shows how to customize the double
XML Schema built-in type.

Boris




More information about the xsd-users mailing list