### Question 14.1

When I set a `float` variable to, say, 3.1, why is `printf` printing it as 3.0999999?

Most computers use base 2 for floating-point numbers as well as for integers. In base 2, 1/1010 (that is, 1/10 decimal) is an infinitely-repeating fraction: its binary representation is 0.0001100110011... . Depending on how carefully your compiler's binary/decimal conversion routines (such as those used by `printf`) have been written, you may see discrepancies when numbers (especially low-precision `float`s) not exactly representable in base 2 are assigned or read in and then printed (i.e. converted from base 10 to base 2 and back again). See also question 14.6.

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback