Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ‘ฉโ€๐ŸŒพ Don't use std::pow with integers in Vectors #207

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ namespace ignition
/// \return The length
public: T Length() const
{
return sqrt(this->SquaredLength());
return static_cast<T>(sqrt(this->SquaredLength()));
}

/// \brief Returns the square of the length (magnitude) of the vector
/// \return The squared length
public: T SquaredLength() const
{
return std::pow(this->data[0], 2)
+ std::pow(this->data[1], 2);
return
this->data[0] * this->data[0] +
this->data[1] * this->data[1];
}

/// \brief Normalize the vector length
Expand Down
14 changes: 8 additions & 6 deletions include/ignition/math/Vector3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ namespace ignition
/// \return the distance
public: T Distance(const Vector3<T> &_pt) const
{
return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
return static_cast<T>(sqrt(
(this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
(this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
(this->data[2]-_pt[2])*(this->data[2]-_pt[2]));
(this->data[2]-_pt[2])*(this->data[2]-_pt[2])));
chapulina marked this conversation as resolved.
Show resolved Hide resolved
}

/// \brief Calc distance to the given point
Expand All @@ -116,16 +117,17 @@ namespace ignition
/// \return the length
public: T Length() const
{
return sqrt(this->SquaredLength());
return static_cast<T>(sqrt(this->SquaredLength()));
}

/// \brief Return the square of the length (magnitude) of the vector
/// \return the squared length
public: T SquaredLength() const
{
return std::pow(this->data[0], 2)
+ std::pow(this->data[1], 2)
+ std::pow(this->data[2], 2);
return
this->data[0] * this->data[0] +
this->data[1] * this->data[1] +
this->data[2] * this->data[2];
}

/// \brief Normalize the vector length
Expand Down
16 changes: 9 additions & 7 deletions include/ignition/math/Vector4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ namespace ignition
/// \return the distance
public: T Distance(const Vector4<T> &_pt) const
{
return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
return static_cast<T>(sqrt(
(this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
(this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
(this->data[2]-_pt[2])*(this->data[2]-_pt[2]) +
(this->data[3]-_pt[3])*(this->data[3]-_pt[3]));
(this->data[3]-_pt[3])*(this->data[3]-_pt[3])));
}

/// \brief Calc distance to the given point
Expand All @@ -99,17 +100,18 @@ namespace ignition
/// \return The length
public: T Length() const
{
return sqrt(this->SquaredLength());
return static_cast<T>(sqrt(this->SquaredLength()));
}

/// \brief Return the square of the length (magnitude) of the vector
/// \return the length
public: T SquaredLength() const
{
return std::pow(this->data[0], 2)
+ std::pow(this->data[1], 2)
+ std::pow(this->data[2], 2)
+ std::pow(this->data[3], 2);
return
this->data[0] * this->data[0] +
this->data[1] * this->data[1] +
this->data[2] * this->data[2] +
this->data[3] * this->data[3];
}

/// \brief Round to near whole number.
Expand Down
5 changes: 5 additions & 0 deletions src/Vector2_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,10 @@ TEST(Vector2Test, Length)
math::Vector2d v(0.1, -4.2);
EXPECT_NEAR(v.Length(), 4.20119030752, 1e-10);
EXPECT_DOUBLE_EQ(v.SquaredLength(), 17.65);

// Integer vector
math::Vector2i vi(3, 4);
EXPECT_EQ(vi.Length(), 5);
EXPECT_EQ(vi.SquaredLength(), 25);
}

12 changes: 12 additions & 0 deletions src/Vector3_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ TEST(Vector3dTest, Distance)

double dist2 = vec1.Distance(1, 2, 3);
EXPECT_DOUBLE_EQ(dist, dist2);

math::Vector3i vecInt(0, 0, 0);
int distInt = vec1.Distance({2, 2, 1});
EXPECT_EQ(distInt, 3);
}

/////////////////////////////////////////////////
Expand All @@ -163,12 +167,15 @@ TEST(Vector3dTest, SquaredLength)
{
math::Vector3d vec1(0, 0, 0);
math::Vector3d vec2(1, 2, 3);
math::Vector3i vec3(1, 2, 3);

double sum1 = vec1.SquaredLength();
double sum2 = vec2.SquaredLength();
int sum3 = vec3.SquaredLength();

EXPECT_DOUBLE_EQ(sum1, 0);
EXPECT_DOUBLE_EQ(sum2, 14);
EXPECT_EQ(sum3, 14);
}

/////////////////////////////////////////////////
Expand All @@ -194,6 +201,11 @@ TEST(Vector3dTest, Length)
math::Vector3d v(0.1, -4.2, 2.5);
EXPECT_NEAR(v.Length(), 4.88876262463, 1e-10);
EXPECT_DOUBLE_EQ(v.SquaredLength(), 23.9);

// Integer vector
math::Vector3i vi(1, 2, 2);
EXPECT_EQ(vi.Length(), 3);
EXPECT_EQ(vi.SquaredLength(), 9);
}

/////////////////////////////////////////////////
Expand Down
21 changes: 21 additions & 0 deletions src/Vector4_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ TEST(Vector4dTest, EqualTolerance)
EXPECT_TRUE(math::Vector4d::Zero.Equal(math::Vector4d::One, 1.1));
}

/////////////////////////////////////////////////
TEST(Vector4dTest, Distance)
{
math::Vector4d vec1(0, 0, 0, 0);
math::Vector4d vec2(1, 2, 3, 4);

double dist = vec1.Distance(vec2);
EXPECT_NEAR(dist, 5.477225, 1e-6);

double dist2 = vec1.Distance(1, 2, 3, 4);
EXPECT_DOUBLE_EQ(dist, dist2);

math::Vector4i vecInt(0, 0, 0, 0);
int distInt = vec1.Distance({1, 1, 1, 1});
EXPECT_EQ(distInt, 2);
}

/////////////////////////////////////////////////
TEST(Vector4dTest, Sum)
{
Expand Down Expand Up @@ -420,5 +437,9 @@ TEST(Vector4dTest, Length)
math::Vector4d v(0.1, -4.2, 2.5, 1.0);
EXPECT_NEAR(v.Length(), 4.98998997995, 1e-10);
EXPECT_DOUBLE_EQ(v.SquaredLength(), 24.9);

// Integer vector
EXPECT_EQ(math::Vector4i::One.Length(), 2);
EXPECT_EQ(math::Vector4i::One.SquaredLength(), 4);
}