Floating point numbers provide a precise, but inexact approximation to real numbers. (How precise depends on their size: how many bits of precision are available in the floating point type you're using.)
IEEE 754 floating point (a very popular representation used on many computers) uses binary. This means that binary fractions such as 1/2, 1/4, 1/8 and combinations thereof: 3/8, 5/16, etc are represented exactly (within the limits of how many bits of precision are available). Fractions not based on powers of two are not representable exactly.
The number 1/10 or 0.1 has no exact representation. When you input 0.1 into the machine, it is converted to a value with many binary digits that is close to 0.1. When you print this back with printf
or what have you, you get 0.1
again because the printf
function rounds it off, and so it looks like 0.1
is being exactly represented: 0.1
went into the machine, and by golly, 0.1
came out. Suppose that it takes 10 decimal digits of precision to see the difference between 0.1
and the actual value, but you are only printing to 8 digits. Well, of course it will show as 0.1
. Your floating point printing routine has chopped off the error!
The %f
conversion specifier in the C printf will use more digits for larger numbers because it uses a fixed number of places past the decimal point, with the default being six. So for instance 0.002 will print as 0.002000. But the number 123456 will print as 123456.000000. The larger the number in terms of magnitude, the more significant figures are printed. When you print 786.4 with %f, you're actually asking for 9 decimal digits of precision. Three for the 786 integer part and then six more.
You're using float
which is most likely a 32 bit IEEE 754 float. This has only 24 bits of precision. (1 bit is used for the sign, and 7 for the binary exponent, leaving 24.) 24 bits is equivalent to only about 7 decimal digits of precision!
So, you're asking the machine to print 786.4 (for which it has an inexact representation, remember!) to nine significant figures, from a floating-point representation that is only good for about 7 decimal significant figures. You're asking for two more digits of precision which are not there, and therefore getting two error digits.
So what you can do is use a wider type like double
and/or change the way you're printing the result. Do not ask for so many significant figures. For instance try %.3f
(three digits past decimal point).
The float
type in C should rarely be used by the way. You usually want to be working with the type double
. float
has its uses, such as saving space in large arrays.