Skip to content

Commit 920728b

Browse files
committed
Provide rem_euclid in FloatFuncs
1 parent f54f4ab commit 920728b

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/common.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ macro_rules! define_float_funcs {
3434
/// Special implementation for signum, because libm doesn't have it.
3535
fn signum(self) -> Self;
3636

37+
/// Special implementation for `rem_euclid`, because libm doesn't have it.
38+
fn rem_euclid(self, rhs: Self) -> Self;
39+
3740
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret;)+
3841
}
3942

@@ -51,6 +54,16 @@ macro_rules! define_float_funcs {
5154
}
5255
}
5356

57+
#[inline]
58+
fn rem_euclid(self, rhs: f32) -> f32 {
59+
let r = self % rhs;
60+
if r < 0.0 {
61+
r + rhs.abs()
62+
} else {
63+
r
64+
}
65+
}
66+
5467
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret {
5568
#[cfg(feature = "libm")]
5669
return libm::$lfname(self $(,$arg as _)*);
@@ -73,6 +86,16 @@ macro_rules! define_float_funcs {
7386
}
7487
}
7588

89+
#[inline]
90+
fn rem_euclid(self, rhs: f64) -> f64 {
91+
let r = self % rhs;
92+
if r < 0.0 {
93+
r + rhs.abs()
94+
} else {
95+
r
96+
}
97+
}
98+
7699
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret {
77100
#[cfg(feature = "libm")]
78101
return libm::$lname(self $(,$arg as _)*);

0 commit comments

Comments
 (0)