0

According to this link, the FLOAT data type in C ranges from 1.2E-38 to 3.4E+38 and has 6 decimal places of precision.

However, in my point of view, the value 1.2E-38 requires much more that 6 decimal places of precision for being represented.

What is wrong in my reasoning?

Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • 5
    Have you ever wondered what the “floating” in “floating-point” meant? It means you can represent 1.2e-38 with 6 decimal places of precision. This said, do not try to think in decimal about binary floating-point. – Pascal Cuoq Aug 09 '16 at 21:57
  • 2
    1.2 is two decimal digits. The exponent has nothing to do with it. [See this link](https://en.wikipedia.org/wiki/Significant_figures#Scientific_notation). [This link may also be useful](https://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32). – user3386109 Aug 09 '16 at 22:01
  • 1
    How many digits did it just take you to represent 1.2E-38 when you were typing this up? – user2357112 Aug 09 '16 at 22:03
  • 1
    0.000000000000000000012 has 21 decimal places, and 2 decimal places *of precision*. – user253751 Aug 09 '16 at 23:33
  • stop thinking decimal, it is a binary format not a decimal format. think of how many bits of mantissa not decimal digits. – old_timer Aug 10 '16 at 01:07
  • The words "precision", and "range" all have specific technical meanings. – RBerteig Aug 10 '16 at 04:22

3 Answers3

4

Precision in floating point refers to the number of leading significant digits.

For common float, it can be considered to have at least 6 decimal digits of significance.


Recall that floating point numbers are distributed logarithmically. There are about as many different FP number from 0.1 to 0.2 as between 10 to 20 as between 0.000001 to 0.000002 as between 1,000,000 to 2,000,000.

Notice that the digits for the next FP number changes by a small amount in the right most digits.

printf("FLT_MIN  %.*e\n", FLT_DECIMAL_DIG - 1, FLT_MIN);
printf("FLT_MIN+ %.*e\n", FLT_DECIMAL_DIG - 1, nextafterf(FLT_MIN, 1.0));
printf("one      %.*e\n", FLT_DECIMAL_DIG - 1, 1.0);
printf("one+     %.*e\n", FLT_DECIMAL_DIG - 1, nextafterf(1.0, 2.0));
printf("FLT_MIN- %.*e\n", FLT_DECIMAL_DIG - 1, nextafterf(FLT_MAX, 1.0));
printf("FLT_MAX  %.*e\n", FLT_DECIMAL_DIG - 1, FLT_MAX);

Output

FLT_MIN  1.17549435e-38
FLT_MIN+ 1.17549449e-38

one      1.00000000e+00
one+     1.00000012e+00

FLT_MAX- 3.40282326e+38
FLT_MAX  3.40282347e+38

The difference between two successive FP number is often called the unit-(in-the)-last-place ULP. It is this difference, as compared to the number that is the "precision". This difference for binary32 is constant for every power of 2, then it doubles with increasing power-of-2.

Note the difference of consecutive float numbers just above 1.0 is 2-23 or 1.0/8,388,608 or 0.000000119.... This is the definition of FLT_EPSILON. Some consider that the "precision". C specifies that the most this can be is 1E-5 or 1 part in 100,000.

C does not specify the float behave exactly like the commonly employed binary32, so expect some variations.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • As a more concrete example, the closest representation of the value *100/3* in `float` is `33.33333206...`. As you can see, only the 7 most significant decimal digits are meaningful. Note that depending on your definition of *precision*, `float` can have either 6 or 7 decimal digits of precision. [This page](http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/) has a pretty good write-up on the details. – MooseBoys Aug 09 '16 at 22:37
0

Short answer, floating point numbers use exponents to represent the numbers, not 2's compliment like integers. user3386109's links explain it, but here is a link to an interactive app to let you see how each bit affects the final result. I think you playing around with that will help more than an explanation or link to a Wikipedia page ever will.

Cody
  • 2,643
  • 2
  • 10
  • 24
0

Computer store floating values using scientific notation. If the floating point value is 293.990933 then the scientific notation is 0.293990933 * 10^3

Here 0 sign bit

293990933 is called mantisa

3 is called exponent

Reserved: for sign bit 1 binary digit. For mantisa, it can have 24 bits and the rest of 32 will be for exponent!

When you use %f it return 6 fraction of the number, by default!

Community
  • 1
  • 1
Md. A. Barik
  • 429
  • 5
  • 12