Skip to content

Commit 99f7fb0

Browse files
committed
Fix issue of binary_float_op when result of rem is zero
1 parent c90eb48 commit 99f7fb0

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

compiler/rustc_apfloat/src/ieee.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ impl<S: Semantics> Float for IeeeFloat<S> {
10011001
(Category::Infinity, _) | (_, Category::Zero) => Status::INVALID_OP.and(Self::NAN),
10021002

10031003
(Category::Normal, Category::Normal) => {
1004+
let sign = self.sign;
10041005
while self.is_finite_non_zero()
10051006
&& rhs.is_finite_non_zero()
10061007
&& self.cmp_abs_normal(rhs) != Ordering::Less
@@ -1015,6 +1016,10 @@ impl<S: Semantics> Float for IeeeFloat<S> {
10151016
self = unpack!(status=, self - v);
10161017
assert_eq!(status, Status::OK);
10171018
}
1019+
// IEEE754 requires this
1020+
if self.is_zero() {
1021+
self.sign = sign;
1022+
}
10181023
Status::OK.and(self)
10191024
}
10201025
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-pass
2+
// check-run-results
3+
4+
pub fn f() -> f64 {
5+
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
6+
}
7+
8+
pub fn g() -> f64 {
9+
-1.0 % -1.0
10+
}
11+
12+
pub fn main() {
13+
assert_eq!(-1, g().signum() as i32);
14+
assert_eq!((-0.0_f64).to_bits(), f().to_bits());
15+
assert_eq!(f().signum(), g().signum());
16+
}

0 commit comments

Comments
 (0)