8

I'm new to C++!

Wiki says this about float: The minimum positive normal value is 2^−126 ≈ 1.18 × 10^−38 and the minimum positive (denormal) value is 2^−149 ≈ 1.4 × 10^−45.

But if a float can have max 7 digit (≈ 7.225), the min is it not just 0,0000001? I'm confused :)

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 4
    That's 7 digits of **precision**, not **range**. – Oliver Charlesworth Aug 24 '16 at 18:37
  • Maybe I'm stupid, but can you give to me an example to get it? What's the min (positive) value? – markzzz Aug 24 '16 at 18:39
  • And 9 is the max, not 7. http://stackoverflow.com/a/13543600/1241334 – Jonny Henly Aug 24 '16 at 18:40
  • This is probably a more useful wiki page to start with https://en.wikipedia.org/wiki/Floating_point – atkins Aug 24 '16 at 18:41
  • 2
    Floating-point types are (almost always) represented using binary, not decimal. The value `0.0000001` typically can't even be represented exactly. The number of "decimal digits" it can represent is just a user-friendly way of talking about how precise they are. Also, the exact values can vary from one implementation to another, though IEEE representation is nearly universal these days. – Keith Thompson Aug 24 '16 at 18:42
  • @JonnyHenly - The linked answer is somewhat misleading. The precision is constant, but the number of decimal digits required to uniquely identify particular `float` values does vary. – Oliver Charlesworth Aug 24 '16 at 18:45
  • 1
    Even though your number might have 38 or 45 digits in it, only 7 of them will be correct - less if it's denormal. – Mark Ransom Aug 24 '16 at 18:53

2 Answers2

17

A floating point number consists of 3 parts: a sign, a fraction, and an exponent. These are all integers, and they're combined to get a real number: (-1)sign × (fraction × 2-23) × 2exponent

The Wikipedia article uses a binary number with a decimal point for the fraction, but I find it clearer to think of it as an integer multiplied by a fixed constant. Mathematically it's the same.

The fraction is 23 bits, but there's an extra hidden bit that makes it a 24-bit value. The largest integer that can be represented in 24 bits is 16777215, which has just over 7 decimal digits. This defines the precision of the format.

The exponent is the magic that expands the range of the numbers beyond what the precision can hold. There are 8 bits to hold the exponent, but a couple of those values are special. The value 255 is reserved for infinities and Not-A-Number (NAN) representations, which aren't real numbers and don't follow the formula given above. The value 0 represents the denormal range, which are called that because the hidden bit of the fraction is 0 rather than 1 - it's not normalized. In this case the exponent is always -126. Note that the precision of denormal numbers declines as the fraction gets smaller, because it has fewer digits. For all the other bit patterns 1-254, the hidden bit of the fraction is 1 and the exponent is bits-127. You can see the details at the Wikipedia section on exponent encoding.

The smallest positive denormal number is (-1)0 × (1 × 2-23) × 2-126, or 1.4e-45.

The smallest positive normalized number is (-1)0 × (0x800000 × 2-23) × 2(1 - 127), or 1.175494e-38.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
-1

It has to be distinguished between the internal representation and the format.

In the internal representation Floating-point numbers are typically packed as sign bit, the exponent field, and the significand or mantissa, from left to right. This representation is defined for the range you mentioned (mathematical limit)

The format defines the "external" representation and is limited to the available space and thereby to the precision of the data type e.g. float about 7 digits (technical limit).