Skip to content

Commit 77bd65f

Browse files
authored
Rollup merge of rust-lang#129321 - krtab:float_sum, r=workingjubilee
Change neutral element of <fNN as iter::Sum> to neg_zero The neutral element used to be positive zero, but +0 + -0 = +0 so -0 seems better indicated.
2 parents cc21978 + 529e33a commit 77bd65f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Diff for: core/src/iter/traits/accum.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ macro_rules! float_sum_product {
104104
impl Sum for $a {
105105
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
106106
iter.fold(
107-
0.0,
107+
-0.0,
108108
#[rustc_inherit_overflow_checks]
109109
|a, b| a + b,
110110
)
@@ -126,7 +126,7 @@ macro_rules! float_sum_product {
126126
impl<'a> Sum<&'a $a> for $a {
127127
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
128128
iter.fold(
129-
0.0,
129+
-0.0,
130130
#[rustc_inherit_overflow_checks]
131131
|a, b| a + b,
132132
)

Diff for: core/tests/num/float_iter_sum_identity.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[test]
2+
fn f32_ref() {
3+
let x: f32 = -0.0;
4+
let still_x: f32 = [x].iter().sum();
5+
assert_eq!(1. / x, 1. / still_x)
6+
}
7+
8+
#[test]
9+
fn f32_own() {
10+
let x: f32 = -0.0;
11+
let still_x: f32 = [x].into_iter().sum();
12+
assert_eq!(1. / x, 1. / still_x)
13+
}
14+
15+
#[test]
16+
fn f64_ref() {
17+
let x: f64 = -0.0;
18+
let still_x: f64 = [x].iter().sum();
19+
assert_eq!(1. / x, 1. / still_x)
20+
}
21+
22+
#[test]
23+
fn f64_own() {
24+
let x: f64 = -0.0;
25+
let still_x: f64 = [x].into_iter().sum();
26+
assert_eq!(1. / x, 1. / still_x)
27+
}

Diff for: core/tests/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod int_log;
3030
mod ops;
3131
mod wrapping;
3232

33+
mod float_iter_sum_identity;
3334
mod ieee754;
3435
mod nan;
3536

0 commit comments

Comments
 (0)