Floating-point calculations and mathematical functions
Understand precision, rounding, tolerance policies, math domains, special values, and the difference between C guarantees and observed formats.
In this chapter8 sections
A measured length can contain a fraction, but a computer cannot store every real number exactly in a finite object. Floating-point calculations work with representable values and rounding rules. Good numeric code starts by deciding which error the application can accept.
1. Think about representable values
A floating-point format uses a sign, a significand, and an exponent under its representation rules. The exponent provides scale; the significand carries a finite amount of detail. Spacing between representable numbers changes with magnitude. The number of useful digits and the largest supported magnitude are different properties.
On a binary format, a decimal fraction such as 0.1 has a repeating expansion and is normally rounded to a nearby representable value. This is similar to writing one third with a finite number of decimal digits. C does not require float and double to be IEEE binary32 and binary64 on every implementation.
2. Observe without inventing a universal output
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double total = 0.1 + 0.2;
if (printf("%.*g\n", DBL_DECIMAL_DIG, total) < 0) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
This complete C17 program prints the result with the implementation’s round-trip-oriented double precision macro. On common binary implementations you may see more digits than the everyday decimal 0.3. Treat the printed string as an observation of that build and library. The test does not impose one exact string for all C implementations.
<float.h> describes properties such as FLT_RADIX, DBL_DIG, and DBL_EPSILON. DBL_EPSILON is the spacing above one in the type’s model, not a tolerance that solves every comparison. Conversions, intermediate evaluation formats, rounding modes, and build options can affect numeric observations.
3. A comparison policy belongs to the problem
Suppose measurements range from zero through 100 and differences up to 0.01 units are acceptable. This is an absolute-tolerance policy in the measurement’s unit. The helper rejects values outside that domain before subtraction, keeping the difference bounded. That policy is useful only because the application chose it.
#include <assert.h>
#include <math.h>
/* This application compares measurements in [0, 100]. */
static int close_measurements(double left, double right)
{
if (!isfinite(left) || !isfinite(right)) return 0;
if (left < 0.0 || left > 100.0 || right < 0.0 || right > 100.0) return 0;
return fabs(left - right) <= 0.01;
}
int main(void)
{
assert(close_measurements(2.0, 2.005));
assert(!close_measurements(2.0, 2.02));
assert(!close_measurements(-1.0, 2.0));
assert(!close_measurements(101.0, 2.0));
assert(close_measurements(0.0, 0.0));
return 0;
}
For a wide range of magnitudes, a relative-error component may be appropriate; values near zero often still need an absolute rule. No single tolerance fits every scientific, graphics, or financial task. Approximate equality is also not necessarily transitive, so do not casually use it as the equivalence rule in a sorting comparator.
4. Mathematical functions have domains and errors
<math.h> provides functions such as sqrt, log, and sin, with float and long double variants. Check the mathematical domain and the application domain. For example, real square roots require a nonnegative input, while a measurement application may also reject nonfinite input. Do not teach division by zero as a portable way to manufacture infinity.
Domain, pole, and range errors have specified reporting rules. Inspect math_errhandling to determine whether the implementation uses errno, floating-point exception flags, or both. If checking errno for such a call, clear it first and follow the documented convention; a stale value is not proof that this call failed. Some toolchains require a math-library link option such as -lm.
5. Rounding can accumulate or cancel useful detail
Adding a tiny value to a much larger one may leave the stored result unchanged. Subtracting nearly equal approximations can lose relative accuracy. Changing the order of additions may change a floating-point result; algebraic identities over real numbers do not all remain exact for rounded operations. Better algorithms can matter more than changing one variable to a wider type.
NaNs and infinities, when the implementation supports them, need explicit policy. A NaN is not equal even to itself under the usual IEC floating comparison rules; ordered comparisons do not behave like ordinary finite-number comparisons. Classify values with the supported math facilities and never assume a numeric type alone validates a measurement.
Is DBL_EPSILON automatically the right acceptance threshold for two measured distances?
Read the explanation
No. It describes a property near one in the double model. Measurement units, scale, uncertainty, and the task’s requirements determine an appropriate comparison policy.
Primary references and further reading
- WG14 N1570 — public C11 draft: 5.2.4.2.2, 7.7, 7.12: floating characteristics and math error contracts.
- David Goldberg — What Every Computer Scientist Should Know About Floating-Point Arithmetic: Original research tutorial, reprinted by Oracle; IEEE-specific discussions remain labelled by their assumptions.
- WG14 N3096 — public C23 draft: Version-specific numeric additions; support must be checked separately.
Before you move on
- Floating-point values have finite range and precision, with rounding under the selected implementation rules.
- Comparison tolerances must come from an application’s units, domain, and acceptable error.
- Math domains, special values, and error reporting require explicit policies.
Keep these ideas separate
- DBL_EPSILON / Application tolerance
- The spacing above one does not supply a universal acceptance threshold for arbitrary measurements.
Why could changing an addition order change a floating-point observation without proving the compiler is wrong?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, exceptions and math_errhandling never matter.
The implementation’s advertised error-handling facilities matter.
- Yes, every finite result proves there was no domain or range issue.
Inspect the relevant function and error contract instead of using that general shortcut.
- No. Follow math_errhandling and the relevant function’s result and error rules.
✓ Correct reasoning. Applicable errno and floating-point exception reporting must be handled according to the implementation’s contract.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.