Fix /= operator under Python 3

The Python method for /= was set as `__idiv__`, which should be
`__itruediv__` under Python 3.

This wasn't totally broken in that without it defined, Python constructs
a new object by calling __truediv__.  The operator tests, however,
didn't actually test the /= operator: when I added it, I saw an extra
construction, leading to the problem.  This commit also includes tests
for the previously untested *= operator, and adds some element-wise
vector multiplication and division operators.
This commit is contained in:
Jason Rhinelander
2017-05-20 20:19:26 -04:00
parent d2da33a34a
commit acad05cb13
3 changed files with 28 additions and 3 deletions

View File

@@ -39,10 +39,14 @@ public:
Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
@@ -61,10 +65,14 @@ test_initializer operator_overloading([](py::module &m) {
.def(py::self - float())
.def(py::self * float())
.def(py::self / float())
.def(py::self * py::self)
.def(py::self / py::self)
.def(py::self += py::self)
.def(py::self -= py::self)
.def(py::self *= float())
.def(py::self /= float())
.def(py::self *= py::self)
.def(py::self /= py::self)
.def(float() + py::self)
.def(float() - py::self)
.def(float() * py::self)