diff options
| author | Arthur Carcano <arthur.carcano@ocamlpro.com> | 2024-08-20 18:41:42 +0200 | 
|---|---|---|
| committer | Arthur Carcano <arthur.carcano@ocamlpro.com> | 2024-08-20 18:45:53 +0200 | 
| commit | 490818851860fb257e23fe7aa0ee32eaffc4ba40 (patch) | |
| tree | 58b3f4ebe4580df1ea8119140a2c9a6374736f7e | |
| parent | a971212545766fdfe0dd68e5d968133f79944a19 (diff) | |
| download | rust-490818851860fb257e23fe7aa0ee32eaffc4ba40.tar.gz rust-490818851860fb257e23fe7aa0ee32eaffc4ba40.zip  | |
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.
| -rw-r--r-- | library/core/src/iter/traits/accum.rs | 4 | ||||
| -rw-r--r-- | library/core/tests/num/float_iter_sum_identity.rs | 27 | ||||
| -rw-r--r-- | library/core/tests/num/mod.rs | 1 | 
3 files changed, 30 insertions, 2 deletions
diff --git a/library/core/src/iter/traits/accum.rs b/library/core/src/iter/traits/accum.rs index c97cd042ab4..5b7d95c2f65 100644 --- a/library/core/src/iter/traits/accum.rs +++ b/library/core/src/iter/traits/accum.rs @@ -104,7 +104,7 @@ macro_rules! float_sum_product { impl Sum for $a { fn sum<I: Iterator<Item=Self>>(iter: I) -> Self { iter.fold( - 0.0, + -0.0, #[rustc_inherit_overflow_checks] |a, b| a + b, ) @@ -126,7 +126,7 @@ macro_rules! float_sum_product { impl<'a> Sum<&'a $a> for $a { fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self { iter.fold( - 0.0, + -0.0, #[rustc_inherit_overflow_checks] |a, b| a + b, ) diff --git a/library/core/tests/num/float_iter_sum_identity.rs b/library/core/tests/num/float_iter_sum_identity.rs new file mode 100644 index 00000000000..6d3224522a8 --- /dev/null +++ b/library/core/tests/num/float_iter_sum_identity.rs @@ -0,0 +1,27 @@ +#[test] +fn f32_ref() { + let x: f32 = -0.0; + let still_x: f32 = [x].iter().sum(); + assert_eq!(1. / x, 1. / still_x) +} + +#[test] +fn f32_own() { + let x: f32 = -0.0; + let still_x: f32 = [x].into_iter().sum(); + assert_eq!(1. / x, 1. / still_x) +} + +#[test] +fn f64_ref() { + let x: f64 = -0.0; + let still_x: f64 = [x].iter().sum(); + assert_eq!(1. / x, 1. / still_x) +} + +#[test] +fn f64_own() { + let x: f64 = -0.0; + let still_x: f64 = [x].into_iter().sum(); + assert_eq!(1. / x, 1. / still_x) +} diff --git a/library/core/tests/num/mod.rs b/library/core/tests/num/mod.rs index 9d2912c4b22..53ff5ab1ced 100644 --- a/library/core/tests/num/mod.rs +++ b/library/core/tests/num/mod.rs @@ -30,6 +30,7 @@ mod int_log; mod ops; mod wrapping; +mod float_iter_sum_identity; mod ieee754; mod nan;  | 
