Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

float, double and long double

float 12 minFloating-point types store binary approximations of real numbers; double is the everyday choice, float trades precision for space, long double is at least as precise as double, and equality tests should use a tolerance.

float, double and long double

The logger stores each reading in tenths, but the daily average rarely lands on a neat tenth: 644 tenths over 3 readings is 21.4666… degrees. For values with fractions, C has three floating-point types. They are fast and useful, and they are all approximations.

The rule

Use double by default; floats approximate, so never test them with ==.

Type Typical size Typical precision Literal
float 32 bits about 6–7 significant digits 21.5f
double 64 bits about 15–16 significant digits 21.5
long double 64 to 128 bits at least as precise as double 21.5L

On almost every machine today, float and double follow the IEEE 754 standard. long double varies most: it is an 80-bit extended format with GCC on x86 Linux, but the same as double with Microsoft's compiler.

Key facts:

  • An unsuffixed literal like 21.5 is a double. Add f to get a float.
  • printf uses %f for both float and double, because a float argument is promoted to double. scanf does not promote: it needs %f for a float and %lf for a double. For long double, both use %Lf.
  • Most decimal fractions, such as 0.1 and 21.3, have no exact binary form. The stored value is the nearest one available, so small errors creep in and add up.
double avg = 644 / 3.0;          /* 214.666..., in tenths */
printf("%.2f\n", avg / 10.0);    /* prints 21.47 */

float saves memory, which matters when you store thousands of values on a small board, but it is not "simpler" or more accurate. Reach for it deliberately.

The shortcut

Three habits: default to double, write f only on purpose, compare with a tolerance. Instead of a == b, ask "are they close enough?" using fabs from <math.h>:

#include <math.h>

if (fabs(avg - 21.3) < 0.001) { /* close enough to 21.3 */ }
Adding 0.1 ten timesStep 1 of 4
  1. The code

    double t = 0.0; then t += 0.1; ten times in a loop.

Drills

Answer fast — trust the rule.

quick drill

What is the type of the literal 18.5?

quick drill

Which scanf specifier reads a value into a double?

quick drill

On a typical IEEE 754 machine, what is the value of 0.1 + 0.2 == 0.3?

quick drill

What does the standard guarantee about long double?

Remember

  • Use double by default; floating-point values are approximations.
  • Shortcut: f suffix only on purpose, and compare with fabs(a - b) < tolerance, never ==.
  • Trap: printf uses %f for both, but scanf needs %lf for a double.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainStoring the average

The logger computes the day's average temperature and must decide whether it has reached exactly 21.3 degrees. Explain which floating type you would use, why == is a poor test here, and what you would write instead.