Skip to content

Commit 529e33a

Browse files
committed
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.
1 parent 91439ce commit 529e33a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

core/src/iter/traits/accum.rs

Lines changed: 2 additions & 2 deletions
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
)
Lines changed: 27 additions & 0 deletions
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+
}

core/tests/num/mod.rs

Lines changed: 1 addition & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy