Added an atof overload to convert from string to float, hopefully without any precision error

Mon, 01 Jul 2013 17:27:37 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 01 Jul 2013 17:27:37 +0300
changeset 308
4e2425bd4dc7
parent 307
c731a22899a3
child 309
11ec6aa1f1fb

Added an atof overload to convert from string to float, hopefully without any precision error

src/file.cpp file | annotate | diff | comparison | revisions
src/misc.cpp file | annotate | diff | comparison | revisions
src/misc.h file | annotate | diff | comparison | revisions
--- a/src/file.cpp	Thu Jun 27 14:55:32 2013 +0300
+++ b/src/file.cpp	Mon Jul 01 17:27:37 2013 +0300
@@ -570,15 +570,11 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-static vertex parseVertex (QStringList& s, const ushort n) {
-	// Disable the locale while parsing the line or atof's behavior changes
-	// between locales (i.e. fails to read decimals properly). That is
-	// quite undesired...
-	setlocale (LC_NUMERIC, "C");
-	
+static vertex parseVertex( QStringList& s, const ushort n )
+{
 	vertex v;
-	for (const Axis ax : g_Axes)
-		v[ax] = s[n + ax].toFloat ();
+	for( const Axis ax : g_Axes )
+		v[ax] = atof( s[n + ax] );
 	
 	return v;
 }
--- a/src/misc.cpp	Thu Jun 27 14:55:32 2013 +0300
+++ b/src/misc.cpp	Mon Jul 01 17:27:37 2013 +0300
@@ -249,6 +249,23 @@
 	return list.join (delim);
 }
 
+double atof( str val )
+{
+	// Disable the locale while parsing the line or atof's behavior changes
+	// between locales (i.e. fails to read decimals properly). That is
+	// quite undesired...
+	setlocale( LC_NUMERIC, "C" );
+	char* buf = new char[val.length()];
+	char* bufptr = &buf[0];
+	
+	for( qchar& c : val )
+		*bufptr++ = c.toAscii();
+	*bufptr = '\0';
+	
+	double fval = atof( buf );
+	delete[] buf;
+	return fval;
+}
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--- a/src/misc.h	Thu Jun 27 14:55:32 2013 +0300
+++ b/src/misc.h	Mon Jul 01 17:27:37 2013 +0300
@@ -37,6 +37,8 @@
 // Converts a float value to a string value.
 str ftoa (double num);
 
+double atof( str val );
+
 // Simplifies the given fraction.
 void simplify (short& numer, short& denom);
 

mercurial