implement a fix to floating point precision as proposed by schuellc.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b58f9eb..f0ae3dc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,8 +10,8 @@
################################
# set lib version here
-set(GENERIC_LIB_VERSION "1.0.12")
-set(GENERIC_LIB_SOVERSION "1")
+set(GENERIC_LIB_VERSION "1.0.13")
+set(GENERIC_LIB_SOVERSION "1")
################################
diff --git a/dox b/dox
index 7ab99ff..2413b2e 100755
--- a/dox
+++ b/dox
@@ -32,7 +32,7 @@
# This could be handy for archiving the generated documentation or
# if some version control system is used.
-PROJECT_NUMBER = 1.0.12
+PROJECT_NUMBER = 1.0.13
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer
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 = { "<!" };
diff --git a/tinyxml2.h b/tinyxml2.h
index fa4eeb5..086c282 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -116,9 +116,9 @@
#define TIXML_SSCANF sscanf
#endif
-static const int TIXML2_MAJOR_VERSION = 1;
-static const int TIXML2_MINOR_VERSION = 0;
-static const int TIXML2_PATCH_VERSION = 12;
+static const int TIXML2_MAJOR_VERSION = 1;
+static const int TIXML2_MINOR_VERSION = 0;
+static const int TIXML2_PATCH_VERSION = 13;
namespace tinyxml2
{
@@ -1321,6 +1321,11 @@
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
+ /// Sets the named attribute to value.
+ void SetAttribute( const char* name, float value ) {
+ XMLAttribute* a = FindOrCreateAttribute( name );
+ a->SetAttribute( value );
+ }
/**
Delete an attribute.
diff --git a/xmltest.cpp b/xmltest.cpp
index 533bcba..2c1fece 100644
--- a/xmltest.cpp
+++ b/xmltest.cpp
@@ -1139,18 +1139,6 @@
XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
}
-#if 0 // the question being explored is what kind of print to use:
- // https://github.com/leethomason/tinyxml2/issues/63
- {
- const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9'/>";
- XMLDocument doc;
- doc.Parse( xml );
- doc.FirstChildElement()->SetAttribute( "attrA", 123456789.123456789 );
- doc.FirstChildElement()->SetAttribute( "attrB", 1.001e9 );
- doc.Print();
- }
-#endif
-
{
// An assert should not fire.
const char* xml = "<element/>";
@@ -1240,39 +1228,85 @@
"</root>";
XMLDocument doc;
- doc.Parse( xml );
+ doc.Parse(xml);
XMLElement* subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
XMLElement* two = doc.RootElement()->FirstChildElement("two");
two->InsertFirstChild(subtree);
- XMLPrinter printer1( 0, true );
- doc.Accept( &printer1 );
- XMLTest( "Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
+ XMLPrinter printer1(0, true);
+ doc.Accept(&printer1);
+ XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
- doc.Parse( xml );
+ doc.Parse(xml);
subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
two = doc.RootElement()->FirstChildElement("two");
doc.RootElement()->InsertAfterChild(two, subtree);
- XMLPrinter printer2( 0, true );
- doc.Accept( &printer2 );
- XMLTest( "Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false );
+ XMLPrinter printer2(0, true);
+ doc.Accept(&printer2);
+ XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
- doc.Parse( xml );
+ doc.Parse(xml);
XMLNode* one = doc.RootElement()->FirstChildElement("one");
subtree = one->FirstChildElement("subtree");
doc.RootElement()->InsertAfterChild(one, subtree);
- XMLPrinter printer3( 0, true );
- doc.Accept( &printer3 );
- XMLTest( "Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false );
+ XMLPrinter printer3(0, true);
+ doc.Accept(&printer3);
+ XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
- doc.Parse( xml );
+ doc.Parse(xml);
subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
two = doc.RootElement()->FirstChildElement("two");
doc.RootElement()->InsertEndChild(subtree);
- XMLPrinter printer4( 0, true );
- doc.Accept( &printer4 );
- XMLTest( "Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false );
+ XMLPrinter printer4(0, true);
+ doc.Accept(&printer4);
+ XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
}
+ {
+ const char* xml = "<svg width = \"128\" height = \"128\">"
+ " <text> </text>"
+ "</svg>";
+ XMLDocument doc;
+ doc.Parse(xml);
+ doc.Print();
+ }
+
+#if 1
+ // the question being explored is what kind of print to use:
+ // https://github.com/leethomason/tinyxml2/issues/63
+ {
+ //const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9' attrC='1.0e-10' attrD='1001000000.000000' attrE='0.1234567890123456789'/>";
+ const char* xml = "<element/>";
+ XMLDocument doc;
+ doc.Parse( xml );
+ doc.FirstChildElement()->SetAttribute( "attrA-f64", 123456789.123456789 );
+ doc.FirstChildElement()->SetAttribute( "attrB-f64", 1.001e9 );
+ doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e9 );
+ doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e20 );
+ doc.FirstChildElement()->SetAttribute( "attrD-f64", 1.0e-10 );
+ doc.FirstChildElement()->SetAttribute( "attrD-f64", 0.123456789 );
+
+ doc.FirstChildElement()->SetAttribute( "attrA-f32", 123456789.123456789f );
+ doc.FirstChildElement()->SetAttribute( "attrB-f32", 1.001e9f );
+ doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e9f );
+ doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e20f );
+ doc.FirstChildElement()->SetAttribute( "attrD-f32", 1.0e-10f );
+ doc.FirstChildElement()->SetAttribute( "attrD-f32", 0.123456789f );
+
+ doc.Print();
+
+ /* The result of this test is platform, compiler, and library version dependent. :("
+ XMLPrinter printer;
+ doc.Print( &printer );
+ XMLTest( "Float and double formatting.",
+ "<element attrA-f64=\"123456789.12345679\" attrB-f64=\"1001000000\" attrC-f64=\"1e+20\" attrD-f64=\"0.123456789\" attrA-f32=\"1.2345679e+08\" attrB-f32=\"1.001e+09\" attrC-f32=\"1e+20\" attrD-f32=\"0.12345679\"/>\n",
+ printer.CStr(),
+ true );
+ */
+ }
+#endif
+
+
+
// ----------- Performance tracking --------------
{
#if defined( _MSC_VER )