Basic and Advance C Question:

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

Tweet Share WhatsApp

Answer:

Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333333... repeats infinitely. It turns out that in base 2, one tenth is also an infinitely-repeating fraction (0.0001100110011...), so exact decimal fractions such as 3.1 cannot be represented exactly in binary. 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 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).

Download C Programming PDF Read All 221 C Programming Questions
Previous QuestionNext Question
I am trying to do some simple trig, and I am #including <math.h>, but the linker keeps complaining that functions like sin and cos are undefinedWhat does it mean when the linker says that _end is undefined?