MCPcopy Create free account
hub / github.com/douchuan/algorithm / sqrt_binary_search

Function sqrt_binary_search

src/math/mysqrt.rs:1–20  ·  view source on GitHub ↗
(x: f64)

Source from the content-addressed store, hash-verified

1pub fn sqrt_binary_search(x: f64) -> f64 {
2 assert!(x >= 0.0);
3 let mut low = 0.0;
4 let mut up = x;
5 let mut last = 0.0;
6 loop {
7 let mid = (low + up) / 2.0;
8 if (mid - last).abs() <= f64::EPSILON {
9 return mid;
10 }
11
12 if mid * mid > x {
13 up = mid;
14 } else {
15 low = mid;
16 }
17
18 last = mid;
19 }
20}
21
22pub fn sqrt_newton(x: f64) -> f64 {
23 assert!(x >= 0.0);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected