u128 的平方根

Nyx*_*nyx 3 int128 square-root rust

a的平方根如何u128计算?经过舍入后得到的数字可以是 a u128

f64有一个f64::sqrt函数,但我认为我们不应该转换u128f64.

kmd*_*eko 5

您可以使用numRoots crate中的特征(或直接使用num-integer crate 中的特征):

\n
\n

pub fn sqrt(&self) -> Self

\n

返回整数 \xe2\x80\x93 的截断主平方根\xe2\x8c\x8a\xe2\x88\x9ax\xe2\x8c\x8b

\n

这是求解rin r\xc2\xb2 = x,向零舍入。结果将\n满足r\xc2\xb2 \xe2\x89\xa4 x < (r+1)\xc2\xb2

\n
\n
use num::integer::Roots; // 0.4.0\n\nfn main() {\n    let a: u128 = 42;\n    let b = a.sqrt();\n    \n    assert!(b == 6);\n}\n
Run Code Online (Sandbox Code Playgroud)\n