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.
double price = 19.99; float ratio = 0.75f;
Try it
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.
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
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. scanfneeds%lffordouble, whileprintfaccepts%ffor both. Mixing them up reads garbage.- Integer division happens first in
1 / 3. Make an operand floating:1.0 / 3or(double)a / b. - Large integers lose exactness in
floatbeyond 2^24 and indoublebeyond 2^53. - Rounding of exact halves:
printf("%.0f", 2.5)prints2in 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
floatwith adoublebecomesdouble; anintwith afloatbecomesfloat. floatarguments to variadic functions are promoted todouble, which is why%fworks for both.- Do not use floating-point for money. Store cents in an integer.