pub trait ApproxSqrt {
    // Required method
    fn approx_sqrt<const NEWTON: usize>(self) -> Self;
}

Required Methods§

source

fn approx_sqrt<const NEWTON: usize>(self) -> Self

Calculates an approximation of a square root.

The result gets iterated through the Newton-Raphson method with a given amount of iterations.

Even at 0 iterations, this algorithm is typically a bit slower than the built-in sqrt function in the standard library, however this method can be run at compile-time.

Example
#![feature(const_trait_impl)]
 
use float_approx_math::ApproxSqrt;
 
const X: f32 = 2.0;
let y: f32 = X.approx_sqrt::<3>(); // Three iterations

assert_eq!(y, X.sqrt());
Error

Calculating the square-root of 2:

use float_approx_math::ApproxSqrt;
 
const TWO: f64 = 2.0;
 
assert_eq!(TWO.sqrt(), 1.4142135623730951);
 
assert_eq!(TWO.approx_sqrt::<0>(), 1.5); // 6.07% error
assert_eq!(TWO.approx_sqrt::<1>(), 1.4166666666666665); // 0.173% error
assert_eq!(TWO.approx_sqrt::<2>(), 1.4142156862745097); // 0.000150% error
assert_eq!(TWO.approx_sqrt::<3>(), 1.4142135623746899); // 0.000000000113% error

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ApproxSqrt for f32

source§

fn approx_sqrt<const NEWTON: usize>(self) -> Self

source§

impl ApproxSqrt for f64

source§

fn approx_sqrt<const NEWTON: usize>(self) -> Self

Implementors§