diff options
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/mod.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/math/mod.rs b/src/math/mod.rs index c9c1c6e..4035de2 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -14,7 +14,8 @@ pub use self::surface::*; pub use self::triangle::*; pub use self::vec2::*; -use num_traits::Float; +use nalgebra::RealField; +use num_traits::Pow; use std::cmp::Ordering; /// Round a floating point number to the nearest step given by the step argument. For instance, if @@ -22,7 +23,7 @@ use std::cmp::Ordering; /// 0.25 to 0.74999... will be 0.5 and so on. pub fn round<T>(num: T, step: T) -> T where - T: Float, + T: RealField, { // Only positive steps will be accepted. assert!(step > T::zero()); @@ -38,6 +39,15 @@ where } } +/// Like round, but instead of rounding to a certain fraction, rounds to the nth decimal place instead +/// of taking a granularity. +pub fn round_nth_decimal<T>(num: T, decimal_place: u16) -> T +where + T: RealField + Pow<u16, Output = T>, +{ + round(num, nalgebra::convert::<f64, T>(0.1).pow(decimal_place)) +} + /// Works like `std::cmp::max`, however also allows partial comparisons. It is specifically /// designed so functions that should be able to use f32 and f64 work, eventhough these do not /// implement Ord. The downside of this function however is, that its behaviour is undefined when |
