0

I'm trying to figure out how floats and doubles work in C but I'm confused on many levels. Here are my questions:

  1. My book tells me that the max value of a float is 1038. Yet, it is only accurate up to 1010. At 1011, the computer gives me an approximate value, not the exact value. Where does this limit of precision come from, and why is it not mentioned?

  2. What is the point of calling the max range 1038 when it's not accurate?

  3. int and float both take up 4 bytes of memory, yet they have widely different ranges. Why is that?

  4. The range of a float is 10-38 to 1038, yet negative values are possible. Why?

I greatly appreciate any help I can get here. I'm relatively new to C, so please keep that in mind when explaining.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Lobs001
  • 365
  • 4
  • 14
  • 3
    This is not so much C/C++ question as it is general floating point question. – SergeyA Apr 20 '16 at 19:21
  • Recommend looking at [Is floating pint broken](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) answers, especially the answer given from a hardware perspective. – Jonathon Ogden Apr 20 '16 at 19:24
  • 1
    I'd suggest reading https://en.m.wikipedia.org/wiki/IEEE_floating_point – Jesper Juhl Apr 20 '16 at 19:24
  • May I suggest playing with imaginary 8 bit floating point instead trying to wrap your head around with 32 bits. –  Apr 20 '16 at 20:08

3 Answers3

3

I will try to answer generically, without specific and gory details and without being bound by particular floating point standard. If you are interested in those, you should familiarize yourself with IEEE 754 floating point standard - a daunting task. While C/C++ implementations are not required to follow it, they usually do and it is an authoritive source worth understanding for someone deeply interested in the topic.

First of all, generically floating point numbers are represented as two distinctive parts - significant number and exponent. This is easy to understand for someone familiar with scientific notation. In scientific notation, 42.42 can be represented as 4242 * 10 ^ -2. (Where ^ -2 means 10 to the power of -2.) Here 4242 is the so-called significant number, -2 is an exponent and 10 is an exponent base.

The same idea can be encoded in binary representation. You just deisgnate some bits to express significant part, some bits to express exponent and some bits to express base (or default your base).

In effect, binary representation of the floating point number could look something like this:

[5 bits to indicate how many bits for significant] [2 bits to encode base] [significant bits] [exponent bits]

And this scheme allows one to encode much bigger numbers than integer encoding in the same amount of bits. Potentially, with 32 bits and above scheme one can encode numbers up to 10 ^ (2 ^ 25)! Much-much-much bigger number than one represented with simple 32-bit integer!

However, it has it's costs. The bigger (in modulus) or closer to zero the number becomes, the more bits are used for exponent (to indicate large power!) and the less number of bits is dedicated to significant. But with this you invariable lose precision - simply because there is a (very) finite number of numbers which can be represented with, say, eight bits.

That pretty much sums it up. The rest is the rules for producing the numbers, selecting base and exponents, rounding the representation and so forth.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0
  1. It gives you approximate value because of "how they are represented internally". See this Is floating point math broken?

  2. Well you see this approximation occurs only for few cases, so it will be wrong to say that range is not accurate.

  3. Different range because they have different representation. See this Size of int and float.

  4. See this.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • for 4th I couldn't get better explanation. If anyone knows better please help. Thanks in advance. –  Apr 20 '16 at 20:20
  • Hardware has nothing to do woth floating point representation. Floating poitns existed way before floating point units became a standard part of any hardware. – SergeyA Apr 20 '16 at 20:22
  • thanks for pointing out I actually meant machine or system we use . I'll correct it –  Apr 20 '16 at 20:25
  • @SergeyA I think now its ok. Isn't it –  Apr 20 '16 at 20:35
-2

Just read the wikipedia page. It answers your questions perfectly here

DanielHsH
  • 4,287
  • 3
  • 30
  • 36
  • Thanks, I guess that first paragraph tells me the most important thing for me right now; that it's a tradeoff between accuracy and range. – Lobs001 Apr 20 '16 at 19:39
  • Wikipedia article on this matter is, in fact, terrible. – SergeyA Apr 20 '16 at 19:46