'double precision in C++, 308 digits or 15 digits?

If double can represent value up to 3.4E308 (308 zeros), then why do we say that double stores only 15 digits? What is point of saying "ten power of 308" ?



Solution 1:[1]

We don't say that "double stores only 15 digits". We say that "double has 15 digits of precision". It means that the computed value of double, when printed as a base-10 sequence of digits, is accurate only up to those 15 digits.

double can represent 3.4E308. Yes, to print it you need more than 15 digits of precision. Some particular values have those guarantees thanks to floating-point implementation. But, for example, a number 3.4E308 - 1, which is inside of double's range, cannot be represented accurately by a double.

If you want to be sure, just take the first 15 digits of double. Some values can be correctly represented with more than 15 digits, but some cannot. Every value in double's range will be correctly represented up to the 15th digit of its decimal representation.

Solution 2:[2]

To help perceive this in simple terms, consider a number type that can represent following numbers:

-100000
-100
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
+1
+2
+3
+4
+5
+6
+7
+8
+9
+100
+100000

The type can represent numbers up to 100000. Does this type have 6 digits of precision, or 1?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fureeish
Solution 2 eerorika