0

I am a beginner at C, and I don't understand how Is floating point math broken? relates to my question, since this is just printing a float and does not involve any math with floats.

I ran this code and it outputs a number that is different from what I assigned to the variable value. I understand that a float will output 6 decimal places typically, but the actual output from this is 12345.099609 instead of 12345.100000. double gives me the expected output.

#include <stdio.h>

int main()
{
    float value;
    value = 12345.1;

    printf("Value = %f\n", value);

    return 0;
}

Here is how the output looks:

Output

If it matters, I'm using Dev-C++ 5.11 to compile and run this code.

  • 3
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken. You can use `double`s to get more precision, but you'll still run into the same issue. There are always going to be less bits in whatever type you use than real numbers. – yano Feb 23 '18 at 04:09
  • @yano I tried it with double instead of float, and it showed 12345.100000. I'm a complete beginner at C, so I don't understand how programmers cope with this issue. – Crochet Queen Feb 23 '18 at 04:16
  • @yano I've edited the question to clarify my confusion. – Crochet Queen Feb 23 '18 at 04:20
  • the math is irrelevant, the root of the issue is the same. A bit can be 1 or 0, only 2 states. How would you represent 2 or 3 or 834 with one bit? You can't. You only have so many bits in whatever type you use. Those bits can only map to so many values. It just so happens your `float` representation can't map exactly to 12345.100000 exactly, so it maps to the closest approximation instead. – yano Feb 23 '18 at 04:24
  • Even if you had infinite bits, the number base is also an issue. Suppose you have the ratio 1/3 in base 10. You can't exactly represent that in decimal notation since it is 0.33333333 forever, however in base 3 the ratio 1/3 can easily be represented in decimal. The same issues apply to base 2. – yano Feb 23 '18 at 04:28
  • one more. .. programmers deal with this by "never" performing floating point math,, unless you're perfectly willing to accept rounding errors (and sometimes you are, everything doesn't have to be exact). I think I read somewhere the linux kernel does absolutely zero floating point math. integer math is exact. For example, financial applications are probably going to turn $10.55 into 1055 cents and go from there (but I've never dealt with financial software so I don't know for sure). – yano Feb 23 '18 at 04:31
  • @yano Thank you very much for your help! I figured out that I can solve the issue by simply making it a smaller number, like 12.2 or something. (12345.1 was an arbitrary choice.) – Crochet Queen Feb 23 '18 at 04:37
  • sure,, just make sure you understand the issue. If 12.2 maps exactly, great! But if you just start making up decimals, it won't take long to find one that doesn't. The take away is floating point numbers (and consequently math operations with them) are approximations. – yano Feb 23 '18 at 04:40
  • @CrochetQueen To be clear, with code `float value = 12345.1;`, `value` does have a value about `12345.099609....` and not exactly `12345.1`. A `float` can encode exactly about 2^32 different numbers. `12345.1` is not one of them. `12345.099609....` was the closest `float`. `printf()` is not the issue. – chux - Reinstate Monica Feb 23 '18 at 04:41

0 Answers0