simplified RoundToDecimals

Thu, 04 Jan 2018 22:42:01 +0200

author
Santeri Piippo
date
Thu, 04 Jan 2018 22:42:01 +0200
changeset 1223
4f8fa42aed07
parent 1222
34def2630300
child 1224
5a31b6d4bf81

simplified RoundToDecimals

src/basics.h file | annotate | diff | comparison | revisions
src/glRenderer.cpp file | annotate | diff | comparison | revisions
src/miscallenous.cpp file | annotate | diff | comparison | revisions
src/miscallenous.h file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/basics.h	Thu Jan 04 21:41:17 2018 +0200
+++ b/src/basics.h	Thu Jan 04 22:42:01 2018 +0200
@@ -17,6 +17,7 @@
  */
 
 #pragma once
+#include <cmath>
 #include <QString>
 #include <QObject>
 #include <QStringList>
--- a/src/glRenderer.cpp	Thu Jan 04 21:41:17 2018 +0200
+++ b/src/glRenderer.cpp	Thu Jan 04 22:42:01 2018 +0200
@@ -559,8 +559,8 @@
 	cx *= negXFac;
 	cy *= negYFac;
 
-	RoundToDecimals(cx, 4);
-	RoundToDecimals(cy, 4);
+	cx = RoundToDecimals(cx, 4);
+	cy = RoundToDecimals(cy, 4);
 
 	// Create the vertex from the coordinates
 	pos3d.setCoordinate(axisX, cx);
--- a/src/miscallenous.cpp	Thu Jan 04 21:41:17 2018 +0200
+++ b/src/miscallenous.cpp	Thu Jan 04 22:42:01 2018 +0200
@@ -262,24 +262,9 @@
 
 // =============================================================================
 //
-void RoundToDecimals(double& a, int decimals)
+double RoundToDecimals(double value, int decimals)
 {
-	static const long e10[] =
-	{
-		1l,
-		10l,
-		100l,
-		1000l,
-		10000l,
-		100000l,
-		1000000l,
-		10000000l,
-		100000000l,
-		1000000000l,
-	};
-
-	if (decimals >= 0 and decimals < countof(e10))
-		a = round(a * e10[decimals]) / e10[decimals];
+	return std::round(value * std::pow(10, decimals)) / std::pow(10, decimals);
 }
 
 // =============================================================================
--- a/src/miscallenous.h	Thu Jan 04 21:41:17 2018 +0200
+++ b/src/miscallenous.h	Thu Jan 04 22:42:01 2018 +0200
@@ -32,7 +32,7 @@
 using ApplyToMatrixFunction = std::function<void(int, double&)>;
 using ApplyToMatrixConstFunction = std::function<void(int, double)>;
 
-void RoundToDecimals(double& a, int decimals);
+double RoundToDecimals(double a, int decimals);
 void ApplyToMatrix(Matrix& a, ApplyToMatrixFunction func);
 void ApplyToMatrix(const Matrix& a, ApplyToMatrixConstFunction func);
 
--- a/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 21:41:17 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 22:42:01 2018 +0200
@@ -161,12 +161,12 @@
 
 			v.apply([&](Axis, double& a)
 			{
-				RoundToDecimals(a, config.roundPositionPrecision());
+				a = RoundToDecimals(a, config.roundPositionPrecision());
 			});
 
 			ApplyToMatrix(t, [&](int, double& a)
 			{
-				RoundToDecimals(a, config.roundMatrixPrecision());
+				a = RoundToDecimals(a, config.roundMatrixPrecision());
 			});
 
 			mo->setPosition(v);
@@ -180,7 +180,7 @@
 				Vertex v = obj->vertex(i);
 				v.apply([&](Axis, double& a)
 				{
-					RoundToDecimals(a, config.roundPositionPrecision());
+					a = RoundToDecimals(a, config.roundPositionPrecision());
 				});
 				obj->setVertex(i, v);
 				num += 3;

mercurial