IT lexicon › Programming › Fast-math

Fast-math

Programming På svenska → Updated: 2026-11-05

The -ffast-math compiler flag and its relatives, which let the optimiser ignore IEEE 754's rules in exchange for faster floating-point code.

Floating-point arithmetic is not associative: (a + b) + c may round differently from a + (b + c). A standards-compliant compiler therefore may not split a sum into four parallel partial sums, may not replace division with multiplication by the reciprocal, and may not assume x - x is zero. -ffast-math in GCC and Clang, and /fp:fast in MSVC, lift all of those restrictions at once.

The fine print is what comes along for the ride. The flag assumes NaNs and infinities never occur, so an isnan() check can be optimised away entirely. GCC also long linked in a startup file that switched on flush-to-zero for the whole process — even if only one shared library had been built with the flag. Slightly wrong maths in everyone's code, then, so a colleague could win a few percent in theirs.

← Back to the lexicon