Skip to main content

sqrt

Function sqrt 

Source
pub fn sqrt(bits: u64, f: Format, mode: Rounding) -> Rounded
Expand description

SQRT.fmt — correctly rounded, with exact flags.

§How

value = m x 2^e. Force e even (shifting m left compensates), then scale m up by 2^62 so the integer square root has ~64 significant bits — comfortably more than the p + 2 correct rounding needs:

sqrt(m x 2^e) = sqrt(m x 2^62) x 2^(e/2 - 31)

u128::isqrt gives the floor of that root, and the root is exact precisely when q * q == n — so that comparison is the sticky bit, with no tolerance and no second rounding. q < 2^64, so the square cannot overflow u128.

§Signs

sqrt(-0) is -0, not a NaN: the sign is preserved and nothing is raised. Any other negative operand is Invalid. Collapsing the two is a common error and IEEE is explicit about the exception.