pub mod error;
use error::SimulationError;
pub fn evaluate_polynomial(coefficients: &[f64], x: f64) -> Result<f64, SimulationError> {
let highest_order_polynomial_coeff = coefficients
.first()
.ok_or_else(|| SimulationError::EmptyPolynomial)?;
Ok(coefficients[0..coefficients.len() - 1]
.iter()
.fold(*highest_order_polynomial_coeff, |acc, coefficient| {
coefficient + x * acc
}))
}
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
pub fn usize_sqrt(n: usize) -> usize {
let mut x = n;
let mut y = 1;
while x > y {
x = (x + y) / 2;
y = n / x;
}
x
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verify_usize_sqrt() {
assert![1 == usize_sqrt(1)];
assert![1 == usize_sqrt(3)];
assert![2 == usize_sqrt(4)];
assert![2 == usize_sqrt(8)];
assert![3 == usize_sqrt(9)];
assert![3 == usize_sqrt(15)];
}
}