Comment

Comment by eesmith

This essay only covers underflow and overflow, and gives an example of "correct" summation for Rust.

  fn sum_up_numbers_correctly(numbers: &[i32]) -> Option<i32> {
      let mut sum: i32 = 0;
      for i in numbers {
          sum = sum.checked_add(*i)?;
      }
      Some(sum)
  }

I expected it to also mention the Kahan summation algorithm (https://en.wikipedia.org/wiki/Kahan_summation_algorithm), which gives more accurate results than this checked_add solution, but it did not. I wonder if that will be in one of the subsequent blog posts.
parent eesmith 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton