From 2bff8204614fecc2f3eeb371de321b26663c2abf Mon Sep 17 00:00:00 2001 From: Thomas Churchman Date: Tue, 29 Oct 2024 14:24:20 +0100 Subject: [PATCH] Provide rem_euclid in FloatFuncs --- src/common.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 _)*);