Skip to content

Commit

Permalink
[共通] Point::length(), lengthSq() を整数オーバーフローしにくい実装に #1217
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Jun 29, 2024
1 parent 9ee8f8b commit b27886b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Siv3D/include/Siv3D/detail/Point.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,17 @@ namespace s3d
template <class Type>
inline Type Point::length() const noexcept
{
return static_cast<Type>(std::sqrt((x * x) + (y * y)));
const Type x_ = static_cast<Type>(x);
const Type y_ = static_cast<Type>(y);
return static_cast<Type>(std::sqrt((x_ * x_) + (y_ * y_)));
}

template <class Type>
inline constexpr Type Point::lengthSq() const noexcept
{
return static_cast<Type>((x * x) + (y * y));
const Type x_ = static_cast<Type>(x);
const Type y_ = static_cast<Type>(y);
return ((x_ * x_) + (y_ * y_));
}

inline constexpr int32 Point::manhattanLength() const noexcept
Expand Down

0 comments on commit b27886b

Please sign in to comment.