← Back to team overview

dolfin team mailing list archive

[noreply@xxxxxxxxxxxxx: [Branch ~dolfin-core/dolfin/main] Rev 6178: Fix problem with pugixml reducing precision of doubles.]

 

Why 20? Any perhaps this should be made into a global parameter value?

--
Anders
--- Begin Message ---
------------------------------------------------------------
revno: 6178
committer: Garth N. Wells <gnw20@xxxxxxxxx>
branch nick: dolfin-all
timestamp: Mon 2011-09-12 12:52:42 +0100
message:
  Fix problem with pugixml reducing precision of doubles.
  
  Solution is to cast double to a string before passing to pugixml, thereby avoiding a cast inside pugixml.
modified:
  dolfin/io/XMLArray.h


--
lp:dolfin
https://code.launchpad.net/~dolfin-core/dolfin/main

Your team DOLFIN Core Team is subscribed to branch lp:dolfin.
To unsubscribe from this branch go to https://code.launchpad.net/~dolfin-core/dolfin/main/+edit-subscription
=== modified file 'dolfin/io/XMLArray.h'
--- dolfin/io/XMLArray.h	2011-07-02 14:50:50 +0000
+++ dolfin/io/XMLArray.h	2011-09-12 11:52:42 +0000
@@ -23,6 +23,7 @@
 
 #include <ostream>
 #include <string>
+#include <boost/lexical_cast.hpp>
 #include "dolfin/common/Array.h"
 #include "dolfin/log/log.h"
 #include "pugixml.hpp"
@@ -90,12 +91,16 @@
     array_node.append_attribute("type") = type.c_str();
     array_node.append_attribute("size") = size;
 
+    std::cout << std::setprecision(20);
+
     // Add data
     for (uint i = 0; i < size; ++i)
     {
       pugi::xml_node element_node = array_node.append_child("element");
       element_node.append_attribute("index") = i;
-      element_node.append_attribute("value") = x[i];
+      // NOTE: Casting to a string to avoid loss of precision when
+      //       pugixml performs double-to-char conversion
+      element_node.append_attribute("value") = boost::lexical_cast<std::string>(x[i]).c_str();
     }
   }
   //-----------------------------------------------------------------------------


--- End Message ---

Follow ups