News for hackers. Stuff that matters.
Re:Beware of Fast-Math by kzrdude on Saturday May 31, 2025 @07:48AM (#44143604)
If you write a loop `for x in array { sum += x }` Then your program is a specification that you want to add the elements in exactly that order, one by one. Vectorization would change the order.
Re:Beware of Fast-Math by stingraycharles on Saturday May 31, 2025 @08:35AM (#44143852)
Yup, because of the imprecision of floating points, cannot just assume that “(a + c) + (b + d)” is the same as “a + b + c + d”.

It would be pretty ironic if at some point fixed point / bignum implementations end up being faster because of this.

Re:Beware of Fast-Math by anthk on Saturday May 31, 2025 @10:26AM (#44144484)
They are, just check anything fixed-point for the 486SX vs anything floating under a 486DX. It's faster scaling and sum and print the desired precision than operating on floats.
Re:Beware of Fast-Math by stingraycharles on Tuesday June 03, 2025 @11:37AM (#44171197)
Is that also the case for modern architectures? Eg is there SIMD fixed precision?
Re:Beware of Fast-Math by einpoklum on Saturday May 31, 2025 @05:31PM (#44147049)
I wonder... couldn't there just be some library type for this, e.g. `associative::float` and `associative::doube` and such (in C++ terms), so that compilers can ignore non-associativity for actions on values of these types? Or attributes one can place on variables to force assumption of associativity?
Re:Beware of Fast-Math by dahart on Saturday May 31, 2025 @10:06AM (#44144364)
The bigger problem there is the language not offering a way to signal the author’s intent. If an author doesn’t care about the order of operations in a sum, they will still write the exact same code as the author who does care. This is a failure of the language to be expressive enough, and doesn’t reflect on the IEEE spec. (The spec even does suggest that languages should offer and define these sorts of semantics.) Whether the program is specifying an order of operations is lost when the language offers no way for a coder to distinguish between caring about order and not caring. This is especially difficult since the vast majority of people don’t care and don’t consider their own code to be a specification on order of operations. Worse, most people would even be surprised and/or annoyed if the compiler didn’t do certain simplifications and constant folding, which change the results. The few cases where people do care about order can be extremely important, but they are rare nonetheless.