Skip to content

Exact sums are not correctly rounded to even #5

@orlp

Description

@orlp

Consider exactly representable f32 numbers 4194304.0 and 4194304.5. Their sum is 8388608.5, which is not exactly representable as an f32, so it needs to be rounded to the nearest even mantissa, which is 8388608. However accurate doesn't do this correctly:

let a: f32 = 4194304.0;
let b: f32 = 4194304.5;
println!("a = {:.1}", a);
println!("b = {:.1}", b);
println!("a + b = {:.1}", a + b);

use accurate::traits::SumAccumulator;
let s1 = accurate::sum::OnlineExactSum::zero().absorb([a, b]).sum();
let s2 = accurate::sum::i_fast_sum_in_place(&mut [a, b]);
println!("OnlineExactSum: {:.1}", s1);
println!("i_fast_sum: {:.1}", s2);

The above prints:

a = 4194304.0
b = 4194304.5
a + b = 8388608.0
OnlineExactSum: 8388609.0
i_fast_sum: 8388609.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions