Rounding modes

2 views
Skip to first unread message

Alan Karp

unread,
Dec 19, 2025, 5:18:59 PM12/19/25
to <friam@googlegroups.com>
At today's friam, Kevin stated that changing floating point rounding modes was dangerous because of compiler optimizations.  I found that hard to believe.  Even such a common optimization as reordering operations can change the answer you get.

I asked Perplexity (the AI I've found to be the least likely to hallucinate).  It said, "Compilers generally avoid changing the value of floating‑point expressions unless explicitly allowed,"  and, "in strict/precise modes compilers are restricted to optimizations that are provably value‑preserving for all inputs."

It pointed out that compilers have options, such as fast-math, that you can use to get the optimizations.

--------------
Alan Karp

Kevin Reid

unread,
Dec 20, 2025, 3:32:01 PM (13 days ago) 12/20/25
to fr...@googlegroups.com
On Fri, Dec 19, 2025 at 2:19 PM Alan Karp <alan...@gmail.com> wrote:
At today's friam, Kevin stated that changing floating point rounding modes was dangerous because of compiler optimizations.  I found that hard to believe.  Even such a common optimization as reordering operations can change the answer you get.

I mean optimizations such as, for example, constant folding, or changing the execution order of an arithmetic operation relative to a function call. Both of those become impossible if changing the rounding mode is permitted.

Changing the structure of an arithmetic expression using algebraic rules, which I'm guessing is what you mean by "reordering", is not a valid optimization for floats. (I'm *not* talking about -ffast-math style unsound optimizations.)

Reply all
Reply to author
Forward
0 new messages