Handling floating-point constant folding and rounding
By default, the compiler replaces most operations involving constant operands with their result at compile time. This process is known as constant folding. Additional folding opportunities might occur with optimization or with the -qnostrict option. The result of a floating-point operation folded at compile time normally produces the same result as that obtained at execution time, except in the following cases:
- The compile-time rounding mode is different from the execution-time
rounding mode. By default, both are round-to-nearest; however, if
your program changes the execution-time rounding mode, to avoid differing
results, perform either of the following operations:
- Change the compile-time rounding mode to match the execution-time mode, by compiling with the appropriate -y option. For more information and an example, see Matching compile-time and runtime rounding modes.
- Suppress folding by compiling with the -qfloat=nofold option.
- Expressions like a+b*c are partially or fully
evaluated at compile time. The results might be different from those
produced at execution time, because b*c might be rounded
before being added to a, while the runtime multiply-add instruction
does not use any intermediate rounding. To avoid differing results,
perform either of the following operations:
- Suppress the use of multiply-add instructions by compiling with the -qfloat=nomaf option.
- Suppress folding by compiling with the -qfloat=nofold option.
- An operation produces an infinite, NaN, or underflow to zero result. Compile-time folding prevents execution-time detection of an exception, even if you compile with the -ftrapping-math (-qflttrap) option. To avoid missing these exceptions, suppress folding with the -qfloat=nofold option.