mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-11 08:50:31 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user