0

In The C Programming Language book by Dennis Ritchie, it is mentioned that "A float number is typically a 32-bit quantity, with at least six significant digits and magnitude generally between about 10^-38 and 10^+38."

How is that possible since we have only 32-bits? Shouldn't the top limit be 2^32? I tried printing out a float by looping and multiplying a float by 10, 38 times and this was the output 100000006944061730000000000000000000000.000000 . It also has to keep track of the sign , so how is it storing all this in just 32-bits?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Krash
  • 2,085
  • 3
  • 13
  • 36
  • 1
    Did you look at how floating point variables work? That it's not a 1-for-1 with the bits, but a value and exponent? And how big floats are now compared to previously? – AntonH Apr 07 '17 at 17:45
  • 3
    Please [read this](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) which explains the IEEE 754 standard format in common use. Although the *actual* format to be used is not specified in the C standard. – Weather Vane Apr 07 '17 at 17:48
  • Thank you, the info and links were very helpful. I should probably do more research before posting a question. Thank you. – Krash Apr 07 '17 at 17:53
  • 2
    The expanded range is a trade-off against accuracy. Not all of the infinite number of real values in the range can be represented. Only about 2**32 of them. Another useful reference here [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Weather Vane Apr 07 '17 at 17:57

1 Answers1

3

See Single-precision floating-point format for a details about a typical C float.

Range of magnitude for float in c programming language?

#include <float.h>
printf("float magnitude range %e to %e\n", FLT_MIN, FLT_MAX);
// typical result
// float magnitude range 1.175494e-38 to 3.402823e+38

How is that possible since we have only 32-bits?

Typical float does indeed only store about 232 different values. Yet they are not distributed linearly but logarithmically in linear groups.

223 different values in the range [2-126 to 2-127)
...
223 different values in the range [0.5 to 1.0)
223 different values in the range [1.0 to 2.0)
223 different values in the range [2.0 to 4.0)
...
223 different values in the range [2127 to 2128)

And their negative counter parts.
Also +/- zeros, small sub-normal numbers, +/- infinity and Not-a-Number

It also has to keep track of the sign , so how is it storing all this in just 32-bits?

 1 bit for sign
 8 bits for the binary exponent
23 bits for the significant (with a MSBit usually implied as 1)
--
32 bits

When printing a float, only about 6 or so (FLT_DIG, FLT_DECIMAL_DIG) significant digits usually are important.

printf("%.*e\n", FLT_DIG-1, FLT_TRUE_MIN);
printf("%.*e\n", FLT_DIG-1, FLT_MIN);
printf("%.*e\n", FLT_DIG-1, acos(-1));
printf("%.*e\n", FLT_DECIMAL_DIG - 1, nextafterf(FLT_MAX, 0.0));
printf("%.*e\n", FLT_DECIMAL_DIG - 1, FLT_MAX);

Output

1.40130e-45    // min sub-normal
1.17549e-38    // min normal
3.14159e+00    // pi
3.40282326e+38 // number before max
3.40282347e+38 // max
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256