Leif looses accuracy when marshalling an xsd:double

Article ID: 1198
Last updated: 07 Feb, 2008
Article ID: 1198
Last updated: 07 Feb, 2008
Revision: 1
Views: 1716
Posted: 01 Jan, 2000
by Dean J.
Updated: 07 Feb, 2008
by Dean J.
Problem


 

Leif looses accuracy for xsd:double during marshalling. For example if I marshal a double with a value of 1103722885.2345 the marshaled output is 1.103723e+09.

 




Cause


 

By default LEIF relies on the format provided by atof, which offers precision to only 6 decimal places

 




Action


 

You can use a configuration file to control the way in which a binding formats primitive types by following the instructions in section 9.5 of the XMLOL User's Guide.

For example, the configuration file below illustrates a custom mapping for the XML schema type xsd:double.  

<config xmlns="http://www.roguewave.com/ratchet">

 <mappings>

     <type xsdType="xsd:double" cppType="double"/>

        <typeinfo cppType="double" isPrimitive="true" include="doubleFormatterUtils.h">

           <fromString>atof($value$.data())</fromString>

           <toString>doubleFormatterUtils::Formatdouble($value$)</toString>

           <defaultValue>$value$</defaultValue>

            <initialValue>0.0</initialValue>

         </typeinfo>

  </mappings>

</config>

Next you need to implement the .h file specified in the include statement of the mappings so that the conversion function is available for the generated marshal code.  In our example the data binding invokes the static function Formatdouble on class doubleFormatterUtils when marshaling a value of type xsd:double.

#ifndef doubleFormatterUtils_h_

#define doubleFormatterUtils_h_

 

#include <stdlib.h>

class doubleFormatterUtils {

public:

      static LEIF::CString Formatdouble(double adouble)

            {

                  char buf[40];

                  sprintf(buf, "%.14g", adouble);

                  return buf;

            }

};

#endif


You can download sample source code for this example (windows environment) by clicking on the zip file, doubleexample.zip

 

This article was:   Helpful | Not helpful
Report an issue
Article ID: 1198
Last updated: 07 Feb, 2008
Revision: 1
Views: 1716
Posted: 01 Jan, 2000 by Dean J.
Updated: 07 Feb, 2008 by Dean J.

Others in this category