Browse Source

Math: function to compute product of all values in the vector.

Is this mathematic operation at all?
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
c1374155b0
  1. 6
      src/Math/Test/VectorTest.cpp
  2. 1
      src/Math/Test/VectorTest.h
  3. 10
      src/Math/Vector.h

6
src/Math/Test/VectorTest.cpp

@ -121,6 +121,12 @@ void VectorTest::normalized() {
QVERIFY(Vector4(vec).normalized() == Vector4(normalized));
}
void VectorTest::product() {
float vec[] = { 1.0f, 2.0f, 3.0f };
QCOMPARE(Vector3(vec).product(), 6.0f);
}
void VectorTest::angle() {
float a[] = { 2.0f, 3.0f, 4.0f };
float b[] = { 1.0f, -2.0f, 3.0f };

1
src/Math/Test/VectorTest.h

@ -32,6 +32,7 @@ class VectorTest: public QObject {
void addSubstract();
void length();
void normalized();
void product();
void angle();
void negative();

10
src/Math/Vector.h

@ -172,6 +172,16 @@ template<class T, size_t size> class Vector {
return *this/length();
}
/** @brief Product of values in the vector */
T product() const {
T out = 1;
for(size_t i = 0; i != size; ++i)
out *= at(i);
return out;
}
private:
T _data[size];
};

Loading…
Cancel
Save