Skip to content

Commit

Permalink
Provide rem_euclid in FloatFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed Oct 29, 2024
1 parent f54f4ab commit 5e722d3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
fn rem_euclid(self, rhs: Self) -> Self;

$(fn $name(self $(,$arg: $arg_ty)*) -> $ret;)+
}

Expand All @@ -51,6 +54,16 @@ macro_rules! define_float_funcs {
}
}

#[inline]
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 _)*);
Expand All @@ -73,6 +86,16 @@ macro_rules! define_float_funcs {
}
}

#[inline]
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 _)*);
Expand Down

0 comments on commit 5e722d3

Please sign in to comment.