diff --git a/src/common.rs b/src/common.rs index 30036869..b8d8a2b1 100644 --- a/src/common.rs +++ b/src/common.rs @@ -34,6 +34,9 @@ macro_rules! define_float_funcs { /// Special implementation for signum, because libm doesn't have it. fn signum(self) -> Self; + /// Special implementation for rem_euclid, because libm doesn't have it. + pub fn rem_euclid(self, rhs: Self) -> Self; + $(fn $name(self $(,$arg: $arg_ty)*) -> $ret;)+ } @@ -51,6 +54,16 @@ macro_rules! define_float_funcs { } } + #[inline] + pub fn rem_euclid(self, rhs: f32) -> f32 { + let r = self % rhs; + if r < 0.0 { + r + rhs.abs() + } else { + r + } + } + $(fn $name(self $(,$arg: $arg_ty)*) -> $ret { #[cfg(feature = "libm")] return libm::$lfname(self $(,$arg as _)*); @@ -73,6 +86,16 @@ macro_rules! define_float_funcs { } } + #[inline] + pub fn rem_euclid(self, rhs: f64) -> f64 { + let r = self % rhs; + if r < 0.0 { + r + rhs.abs() + } else { + r + } + } + $(fn $name(self $(,$arg: $arg_ty)*) -> $ret { #[cfg(feature = "libm")] return libm::$lname(self $(,$arg as _)*);