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.
This commit is contained in:
Arthur Carcano 2024-08-20 18:41:42 +02:00
parent a971212545
commit 4908188518
3 changed files with 30 additions and 2 deletions

View File

@ -104,7 +104,7 @@ macro_rules! float_sum_product {
impl Sum for $a { impl Sum for $a {
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self { fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold( iter.fold(
0.0, -0.0,
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
|a, b| a + b, |a, b| a + b,
) )
@ -126,7 +126,7 @@ fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
impl<'a> Sum<&'a $a> for $a { impl<'a> Sum<&'a $a> for $a {
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self { fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold( iter.fold(
0.0, -0.0,
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
|a, b| a + b, |a, b| a + b,
) )

View File

@ -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)
}

View File

@ -30,6 +30,7 @@
mod ops; mod ops;
mod wrapping; mod wrapping;
mod float_iter_sum_identity;
mod ieee754; mod ieee754;
mod nan; mod nan;