2 Star 1 Fork 0

endpoint_rust/Rust

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
square_root.rs 526 Bytes
一键复制 编辑 原始数据 按行查看 历史
fffzlfk 提交于 2022-03-06 15:04 +08:00 . Add square root by Newtons method (#281)
/// squre_root returns the square root
/// of a f64 number using Newtons method
pub fn square_root(num: f64) -> f64 {
if num < 0.0_f64 {
return f64::NAN;
}
let mut root = 1.0_f64;
while (root * root - num).abs() > 1e-10_f64 {
root -= (root * root - num) / (2.0_f64 * root);
}
root
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
assert!((square_root(4.0_f64) - 2.0_f64).abs() <= 1e-10_f64);
assert!(square_root(-4.0_f64).is_nan());
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Rust
1
https://gitee.com/endpoint_rust/Rust.git
git@gitee.com:endpoint_rust/Rust.git
endpoint_rust
Rust
Rust
master

搜索帮助