liveBook · Manning

Floating-point arithmetic

As explained in Modern C

A floating-point value is a value represented approximately by a sign, a significand, and a power-of-two exponent.

A value and its printed form

#include <stdio.h> int main(void) { double x = -0.15625; printf("%g\n", x); } This prints -0.15625. The decimal output is readable, but it does not show how the value is represented: -0.15625 is also -5/32, a fraction with a power of two in its denominator. section 5.7.7 section 8.3.4

A floating-point value can be understood through a sign, a finite sequence of binary significand bits, and an exponent that scales those bits by a power of two. For the example, sign -1, exponent -2, and significand bits 101 give -(1/2 + 1/8) × 2^-2 = -5/32. The position at which the binary expansion is cut off depends on the number’s magnitude; it “floats.” section 5.7.7

TAKEAWAY: Floating-point arithmetic approximates real-number arithmetic. A type has a restricted range and a limited number of significand bits, so a value or an intermediate result may be cut off. Unlike integer computation without overflow, a floating-point computation need not produce an exact mathematical result. section 5.7.7 section 5.8

Keep three distinctions in view:

  • C has real floating-point types float, double, and long double, as well as corresponding complex floating-point types. A type’s precision is not the accuracy of a particular computation. section 5.2
  • The float.h macros DBL_MANT_DIG, DBL_MIN_EXP, and DBL_MAX_EXP describe properties of double. DBL_MIN and DBL_MAX describe bounds; DBL_MIN is not the most negative value. section 5.7.7
  • Binary representation is a model for determining operations, not necessarily a description of how an object is stored. The C standard does not completely fix the precision of every double operation across platforms. section 5.1.3
  • For decimal output without special requirements, use %g. Match printf format specifiers and modifiers to their arguments: an inappropriate one makes behavior undefined. section 8.3.4

How Modern C covers it

How deep it goes
Works through sign, significand bits, exponent, and range macros while leaving exact operational precision platform-dependent.
Examples
Calculates -0.15625 from binary significand bits and identifies float.h macros and %g output.
What it assumes
Assumes familiarity with C types and arithmetic, then defines the sign, significand bits, and exponent used in floating-point representation.
What you can do after
Evaluate/understand approximate C results and use %g to print floating-point values when no special format is needed.
How it connects
Connects floating-point types to binary representation, precision, integer arithmetic, and formatted output.

Also explained in

Compare how 20 books explain floating-point arithmetic →

Related concepts

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