diff --git a/src/Base/MatrixPyImp.cpp b/src/Base/MatrixPyImp.cpp index 4391128f41..bb3501d3b2 100644 --- a/src/Base/MatrixPyImp.cpp +++ b/src/Base/MatrixPyImp.cpp @@ -182,11 +182,17 @@ PyObject * MatrixPy::number_power_handler (PyObject* self, PyObject* other, PyOb return new MatrixPy(Matrix4D()); if(b < 0) { - b = 1+b; - a.inverse(); + if (fabs(a.determinant()) > DBL_EPSILON) + a.inverseGauss(); + else { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot invert singular matrix"); + return 0; + } + b = -b; } + auto res = a; - for(;b;--b) + for(--b;b;--b) res *= a; return new MatrixPy(res); } diff --git a/src/Base/PlacementPyImp.cpp b/src/Base/PlacementPyImp.cpp index cbdd2a134d..c1f6884925 100644 --- a/src/Base/PlacementPyImp.cpp +++ b/src/Base/PlacementPyImp.cpp @@ -363,11 +363,11 @@ PyObject * PlacementPy::number_power_handler (PyObject* self, PyObject* other, P return new PlacementPy(Placement()); if(b < 0) { - b = 1+b; + b = -b; a.invert(); } auto res = a; - for(;b;--b) + for(--b;b;--b) res *= a; return new PlacementPy(res); } diff --git a/src/Base/RotationPyImp.cpp b/src/Base/RotationPyImp.cpp index d3c17ed21c..9c16034275 100644 --- a/src/Base/RotationPyImp.cpp +++ b/src/Base/RotationPyImp.cpp @@ -440,11 +440,11 @@ PyObject * RotationPy::number_power_handler (PyObject* self, PyObject* other, Py return new RotationPy(Rotation()); if(b < 0) { - b = 1+b; + b = -b; a.invert(); } auto res = a; - for(;b;--b) + for(--b;b;--b) res *= a; return new RotationPy(res); }