Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

C float and double floating-point types

Reference for the C float and double types: IEEE 754 precision, ranges, literal suffixes, printf and scanf specifiers, rounding errors, NaN and infinity.

float and double store numbers with a fractional part using a binary floating-point format, almost always IEEE 754. A float has 32 bits and about 7 decimal digits of precision; a double has 64 bits and about 15 to 16 digits. Both can also represent infinity and "not a number" (NaN).

Floating-point numbers are approximations. Most decimal fractions, such as 0.1, have no exact binary representation, so results carry tiny errors. Use double as your default; use float when you must save memory (large arrays, graphics, GPUs) and can accept less precision.

C
double price = 19.99;
float ratio = 0.75f;

Try it

C Precision and rounding error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Special values and literal forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
float x;
double y;
Item float double
Size (typical) 4 bytes 8 bytes
Significant digits FLT_DIG = 6 (about 7) DBL_DIG = 15 (about 16)
Largest finite value about 3.4 ร— 10^38 (FLT_MAX) about 1.8 ร— 10^308 (DBL_MAX)
Machine epsilon about 1.2 ร— 10^-7 (FLT_EPSILON) about 2.2 ร— 10^-16 (DBL_EPSILON)
Literal 3.14f 3.14
printf %f %e %g (a float is promoted to double) %f %e %g
scanf %f %lf

Constants live in <float.h>. Literals without a suffix are double; f makes a float, l or L a long double. Hexadecimal floating literals (0x1.8p1) and INFINITY / NAN from <math.h> are standard since C99.

Errors / undefined behavior: floating-point division by zero is not undefined on IEEE platforms (it gives infinity or NaN), but integer division by zero is. Converting a floating value too large for the target integer type is undefined behavior. Comparisons with NaN are always false, except !=.

Usage notes

  • Never compare with == after arithmetic. Compare the absolute difference with a tolerance; see Comparing floating-point numbers.
  • scanf needs %lf for double, while printf accepts %f for both. Mixing them up reads garbage.
  • Integer division happens first in 1 / 3. Make an operand floating: 1.0 / 3 or (double)a / b.
  • Large integers lose exactness in float beyond 2^24 and in double beyond 2^53.
  • Rounding of exact halves: printf("%.0f", 2.5) prints 2 in the default round-to-nearest-even mode.
  • Order matters. Floating addition is not associative, so summing a long list from smallest to largest is more accurate.
  • Mixed arithmetic: a float with a double becomes double; an int with a float becomes float.
  • float arguments to variadic functions are promoted to double, which is why %f works for both.
  • Do not use floating-point for money. Store cents in an integer.