Negative number safety.

This commit is contained in:
Jeff Young 2025-06-08 11:12:55 +01:00
parent 7417365771
commit 265d273f93

View File

@ -60,15 +60,17 @@ static constexpr T sqrt_max_typed = ct_sqrt( std::numeric_limits<T>::max() );
template <typename T>
T isqrt(T x)
{
T r = (T) std::sqrt((double) x);
T sqrt_max = sqrt_max_typed<T>;
if( x < 0 )
return sqrt_max;
while (r < sqrt_max && r * r < x)
T r = (T) std::sqrt( (double) x );
while( r < sqrt_max && r * r < x )
r++;
while (r > sqrt_max || r * r > x)
while( r > sqrt_max || r * r > x )
r--;
return r;