Skip to content

Commit b27886b

Browse files
committed
[共通] Point::length(), lengthSq() を整数オーバーフローしにくい実装に #1217
1 parent 9ee8f8b commit b27886b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Siv3D/include/Siv3D/detail/Point.ipp

+6-2
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,17 @@ namespace s3d
309309
template <class Type>
310310
inline Type Point::length() const noexcept
311311
{
312-
return static_cast<Type>(std::sqrt((x * x) + (y * y)));
312+
const Type x_ = static_cast<Type>(x);
313+
const Type y_ = static_cast<Type>(y);
314+
return static_cast<Type>(std::sqrt((x_ * x_) + (y_ * y_)));
313315
}
314316

315317
template <class Type>
316318
inline constexpr Type Point::lengthSq() const noexcept
317319
{
318-
return static_cast<Type>((x * x) + (y * y));
320+
const Type x_ = static_cast<Type>(x);
321+
const Type y_ = static_cast<Type>(y);
322+
return ((x_ * x_) + (y_ * y_));
319323
}
320324

321325
inline constexpr int32 Point::manhattanLength() const noexcept

0 commit comments

Comments
 (0)