3

I was reading the way, how integers are stored in variables in c, that the last bit is used for the sign of the integer and the remaining bits are used to store the number.

But if we take a double variable and the long int variable in c, both has a size of 4 bytes but the float can store the very huge numbers up to a range of 1038 but long int of same size cant store such huge value.


I want to understand the mechanism which is used in storage in float.

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
Nimit Bhardwaj
  • 827
  • 9
  • 19
  • 3
    The representation of any data type is always *implementation-defined*. – Kerrek SB Dec 09 '15 at 09:47
  • Here is one [popular representation](https://en.wikipedia.org/wiki/IEEE_754-1985) for floating point numbers. – Kerrek SB Dec 09 '15 at 09:48
  • Use Google to find explanation about floating point encoding. Or simply open Wikipedia. – i486 Dec 09 '15 at 09:49
  • It does it the same way as your 10-digit calculator displays large numbers: In scientific notation 1.23456789E42 = 1.23456789*10^42. The precision may still not be more than 10 digits although the numer is much bigger. Check what happens when you calculate (1e50+1)-1e50, you *should* be getting 1 as result, but you'll probably end up with 0. – JimmyB Dec 09 '15 at 09:55
  • 1
    There is no such thing as *the last bit*. The most significant bit is almost universally used as a sign bit for signed integer representations. – chqrlie Dec 09 '15 at 09:56

2 Answers2

5

The C language does not require any specific representation for floating point numbers.

Today, most C implementations are using IEEE floating numbers (exceptions are unusual, perhaps some Series Z mainframes from IBM).

Read http://floating-point-gui.de/

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Besides theory, these articles would provide the OP with details on an actual rather common implementation: https://en.wikipedia.org/wiki/Single-precision_floating-point_format and https://en.wikipedia.org/wiki/Double-precision_floating-point_format – chqrlie Dec 09 '15 at 09:54
5

The complete explanation can be found here . Basically, the number is not fully stored, only approximately. The 32 bits are used to store as much precision as possible.

Ward
  • 2,802
  • 1
  • 23
  • 38
  • 1
    Also document double precision: https://en.wikipedia.org/wiki/Double-precision_floating-point_format The OP seems somewhat confused: he mentions `double` where he means `float`. Also document that this is only an implementation, the most common one, but not the only one. – chqrlie Dec 09 '15 at 09:51