1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use super::float::Float;
/// Calculates percentage.
///
/// # Arguments
///
/// * `numerator` - The numerator number.
///
/// * `denominator` - The denominator number.
///
/// # Returns
///
/// percentage(numerator / denominator * 100).
///
/// # Examples
///
/// ```
/// use rufl::math;
///
/// assert_eq!(50.0 as f32, math::percent(1.0 as f32, 2.0 as f32));
///
/// assert_eq!(50.0, math::percent(2.0, 4.0));
///
/// ```
pub fn percent<T: Float>(numerator: T, denominator: T) -> T {
numerator.div(&denominator).mul(&Float::cast(100 as f64))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_percent() {
assert_eq!(50.0 as f32, percent(1.0 as f32, 2.0 as f32));
assert_eq!(50.0, percent(2.0, 4.0));
}
}