implement a fix to floating point precision as proposed by schuellc.
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 7e4ff40..50022f4 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -422,16 +422,19 @@
TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );
}
-
+/*
+ ToStr() of a number is a very tricky topic.
+ https://github.com/leethomason/tinyxml2/issues/106
+*/
void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
{
- TIXML_SNPRINTF( buffer, bufferSize, "%f", v );
+ TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
}
void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
{
- TIXML_SNPRINTF( buffer, bufferSize, "%f", v );
+ TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
}
@@ -497,12 +500,7 @@
}
// What is this thing?
- // - Elements start with a letter or underscore, but xml is reserved.
- // - Comments: <!--
- // - Declaration: <?
- // - Everything else is unknown to tinyxml.
- //
-
+ // These strings define the matching patters:
static const char* xmlHeader = { "<?" };
static const char* commentHeader = { "<!--" };
static const char* dtdHeader = { "<!" };