YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Correctness & careChapter 62 of 73

Floating-point calculations and mathematical functions

Understand precision, rounding, tolerance policies, math domains, special values, and the difference between C guarantees and observed formats.

6 min readDevelopingC17 baseline
Offline reading
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

Complete C17 program
#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.

Complete C17 program
#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.

Think it through

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

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.
Explain it in your own words

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.

Can portable code assume every math error is reported only through errno?
Read the explanation for every choice
  1. Yes, exceptions and math_errhandling never matter.

    The implementation’s advertised error-handling facilities matter.

  2. 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.

  3. 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.

CHAPTER 62 / 73

In this chapter

Floating-point calculations and mathematical functions

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping