News for hackers. Stuff that matters.
Re:How to Correctly Sum Up Numbers by eesmith on Thursday October 31, 2024 @10:43AM (#42007420)
You're right! I am not a Rust programmer and mis-read the "i32" as "float32".

The text does imply the correct float solution has a similar structure to the int32 case, with "So, you will have to resort to writing machine code manually if you want to efficiently check overflows of float numbers."

Re:How to Correctly Sum Up Numbers by jepler on Sunday November 03, 2024 @11:04AM (#42033800)
You don't need machine code these days to find overflow in IEEE floating point arithmetic.

If any intermediate sum overflows, you get an infinity. Further arithmetic can never return you to the realm of finite numbers (though you can also get into the world of "not a number" NaNs by calculating -inf + inf for instance), so you can use a standard function like (C99 <math.h>) isfinite() to check if any intermediate step of summing FP numbers overflowed the representation.

Re:How to Correctly Sum Up Numbers by tialaramex on Sunday November 03, 2024 @11:05AM (#42033806)
Rust's 32-bit floating point type is, unsurprisingly, named f32 (stable Rust also has f64, and unstable builds offer f16 and possibly f128 which are both newer IEEE formats)

If you're sure your answer is a 32-bit IEEE floating point number, I think you could store the intermediate sums as a fully expanded fixed point number with enough bits to go from the smallest possible f32 to the largest possible, so you're just doing (big integer) integer summation. 36 bytes ought to be very possible. When you've finished summing, you find the top-most set bit, its position decides the exponent and the subsequent 23 bits are your mantissa.

Re:How to Correctly Sum Up Numbers by lifthrasiir on Sunday November 03, 2024 @07:14PM (#42037229)
Indeed, this approach is known as a superaccumulator [1] and faster overall than the Kahan summation for a large amount of numbers. I don't exactly know the currently accepted threshold but 10,000 numbers should be conservative enough for that.

[1] https://arxiv.org/abs/1505.05571