liveBook · Manning

Floating-point state

As explained in Modern C, Third Edition

Floating-point state is execution state that controls or records the behavior of floating-point operations, including rounding and contraction.

Floating-point state controls how floating-point operations behave, including contraction and rounding. Pragmas can change that state locally: a rounding mode takes effect at the pragma and returns to its prior value when the scope ends. section 16.3

A locally controlled computation

A distance calculation can disable contraction and select a rounding mode before calling sqrt: #pragma FP_CONTRACT OFF #pragma FENV_ROUND FE_TONEAREST return sqrt(x[0]*x[0] + x[1]*x[1]); The surrounding function can be marked [[reproducible]] when it temporarily changes global floating-point state but restores it before returning. section 16.3

The state matters for optimization and function contracts:

  • [[unsequenced]] requires the function not to change relevant global state, while [[reproducible]] permits temporary changes that are restored. section 16.3

How Modern C, Third Edition covers it

How deep it goes
Explains scoped state changes, restoration, and attribute constraints, but does not detail floating-point formats or arithmetic algorithms.
Examples
Uses a two-dimensional distance function, scoped pragmas, sqrt, and repeated-call replacement.
What it assumes
Assumes familiarity with C pragmas, function attributes, sqrt, and global state.
What you can do after
Apply it via C pragmas and function attributes to constrain floating-point behavior and assess optimization validity.
How it connects
Connects rounding mode, floating-point environment, FP_CONTRACT, FENV_ROUND, sqrt, and reproducible and unsequenced attributes.

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.