2

I have a program in C like this:

#include <stdio.h>

int main() {
    float number = 0;
    scanf("%f", &number);
    printf("%f\n", number);
}

And the output is:

ururay@note:~/workspace/c/float-precision$ gcc float-precision.c -o float-precision
ururay@note:~/workspace/c/float-precision$ ./float-precision 
5456.367
5456.367188

I have executed this program several times and the output is the same. Is the '188' "appended" to the end of the number because float precision? If so, how could I identify this in binary representation?

Thiago Ururay
  • 482
  • 3
  • 15
  • 3
    Short answer : Yes. Long answer : Yes. – Cid Dec 11 '21 at 17:12
  • Please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) Also note that nothing was "appended" nor is the `5456.367` "present" in the `float`'s *binary* value. The default number of places by `printf` is 6, so you can do: `printf("%.3f\n", number);` to get 3 places. But here, you have 7 significant digits, around the limit of what can be done with `float` type anyway. – Weather Vane Dec 11 '21 at 17:14
  • 1
    Your question as asked is answered "yes", anything else you meant to ask is almost guaranteed to be a duplicate of Weathers proposals. Please [edit] to ask something else or confirm the duplicate. – Yunnosch Dec 11 '21 at 17:22
  • Does this answer your question? [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Yunnosch Dec 11 '21 at 17:23

2 Answers2

1

Yes if you want a better precision try using double or long double (but long double is way slower than other type or refer to the same type as double).

Also printf only takes double and long double go look at the spec of printf

LTBS46
  • 205
  • 1
  • 10
  • In fact, putting double makes 5456.367 -> 5456.367000. – Thiago Ururay Dec 11 '21 at 17:29
  • 1
    @ThiagoUruray: Even a `double` cannot represent the number exactly. It is represented as `5456.367000000000189174897968769073486328125`. You must print more digits in order to see the inaccuracy, by using a different `printf` format specifier. – Andreas Wenzel Dec 11 '21 at 17:34
  • 1
    @ThiagoUruray: Using `double` goes not give you a value of 5456.367000 for `5456.367`. “5456.367000” is merely what you get when you print the number rounded to six digits after the decimal point. The actual value, in the format commonly used for `double`, is 5456.367000000000189174897968769073486328125. – Eric Postpischil Dec 11 '21 at 17:35
  • You two are right! `printf("%.20lf\n", number)` => 5456.36700000000018917490 – Thiago Ururay Dec 11 '21 at 17:50
1

The number 5456.367 cannot be represented exactly in the IEEE-754 single-precision floating point format.

The closest number is 5456.3671875, which has the binary representation 01000101101010101000001011110000. The next smaller representable number is 5456.3666992, which has the binary representation 01000101101010101000001011101111.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Indeed, it can't be exactly represented in any binary floating point format, no matter how many bits of precision you have. `367/1000` is in lowest terms, and its denominator is not a power of 2. – Nate Eldredge Dec 11 '21 at 17:36