Skip to main content

concision_math/utils/
arith.rs

1/*
2    Appellation: arith <utils>
3    Contrib: @FL03
4*/
5use num::traits::{Float, Num};
6
7///
8pub fn floor_div<T>(numerator: T, denom: T) -> T
9where
10    T: Copy + Num,
11{
12    (numerator - (numerator % denom)) / denom
13}
14
15/// Round the given value to the given number of decimal places.
16pub fn round_to<T>(val: T, decimals: usize) -> T
17where
18    T: Float,
19{
20    let factor = T::from(10).expect("").powi(decimals as i32);
21    (val * factor).round() / factor
22}