What is the biggest "no-floating" integer that can be stored in an IEEE 754 double type without losing precision ?
The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX
or approximately 1.8 × 10308 (if your double is an IEEE 754 64-bit double). It's an integer. It's represented exactly. What more do you want?
Go on, ask me what the largest integer is, such that it and all smaller integers can be stored in IEEE 64-bit doubles without losing precision. An IEEE 64-bit double has 52 bits of mantissa, so I think it's 253:
- 253 + 1 cannot be stored, because the 1 at the start and the 1 at the end have too many zeros in between.
- Anything less than 253 can be stored, with 52 bits explicitly stored in the mantissa, and then the exponent in effect giving you another one.
- 253 obviously can be stored, since it's a small power of 2.
Or another way of looking at it: once the bias has been taken off the exponent, and ignoring the sign bit as irrelevant to the question, the value stored by a double is a power of 2, plus a 52-bit integer multiplied by 2exponent − 52. So with exponent 52 you can store all values from 252 through to 253 − 1. Then with exponent 53, the next number you can store after 253 is 253 + 1 × 253 − 52. So loss of precision first occurs with 253 + 1.
-
106+1 Good job noticing that the question did not really mean what the asker probably intended and providing both answers ("technically correct" and "probably expected"). – Pascal Cuoq Dec 4 '09 at 18:32
-
53Or "messing about" and "trying to help" as I tend to call them :-) – Steve Jessop Dec 4 '09 at 18:34
-
8
-
10You don't mean "all smaller integers", you mean all integers of equal or lesser magnitude. Because there are a lot of negative integers below below 2^53 and cannot be represented exactly in a double. – Southern Hospitality Dec 5 '09 at 8:54
-
11I do mean smaller, and that's exactly what I mean when I say smaller :-) -1,000,000 is less than 1, but it is not smaller. – Steve Jessop Dec 5 '09 at 13:23
9007199254740992 (that's 9,007,199,254,740,992) with no guarantees :)
Program
#include <math.h>
#include <stdio.h>
int main(void) {
double dbl = 0; /* I started with 9007199254000000, a little less than 2^53 */
while (dbl + 1 != dbl) dbl++;
printf("%.0f\n", dbl - 1);
printf("%.0f\n", dbl);
printf("%.0f\n", dbl + 1);
return 0;
}
Result
9007199254740991 9007199254740992 9007199254740992
-
7Assuming it will be 'close' but less than a 2^N, then a faster test is
double dbl = 1; while (dbl + 1 != dbl) dbl *= 2; while (dbl == --dbl);
which yields the same result – Seph Mar 6 '12 at 10:21 -
4@Seph what the...? No?
while (dbl == --dbl)
will loop forever or not at all. :) (in this case, not at all, since it is a 2^N). You'll have to approach it from below. It will indeed also result in one less than the expected result (since the one check in the while loop decrements dbl). And it depends on order of execution, if the decrement is done before or after evaluating the left side (which is undefined as far as I know). If it's the former, it'll always be true and loop forever. – falstro Oct 25 '16 at 14:53 -
2
-
1
-
A weakness to using
while (dbl + 1 != dbl) dbl++;
in thatdbl + 1 != dbl
may evaluate usinglong double
math - considerFLT_EVAL_METHOD == 2
. This could end in an infinite loop. – chux Sep 25 '18 at 19:27
Wikipedia has this to say in the same context with a link to IEEE 754:
On a typical computer system, a 'double precision' (64-bit) binary floating-point number has a coefficient of 53 bits (one of which is implied), an exponent of 11 bits, and one sign bit.
2^53 is just over 9 * 10^15.
-
@Steve Jessop more or less, that is indeed what I am saying. I have also encountered hardware systems that don't have a FPU that still need to be IEEE-compliant, so that "typical system" stuff doesn't really help me if I come back to here 8 months later and need the same info for my 68K-based microcontroller (assuming it doesn't have a FPU... I can't remember). – San Jacinto Dec 4 '09 at 18:39
-
-
14@San Jacinto - "This is useless" is unduly harsh. The answer is quite useful, just not as useful as it would have been if it included the comment that typical computer systems do indeed use the IEEE 754 reprensentation. – Stephen C. Steel Dec 4 '09 at 18:47
-
@Stephen C. Steel, actually you are correct. Under my scenario, coming back to this at a later time and looking for the IEEE max, it is impossibly ambiguous as to what a 'typical system' is, but there is still merit in the answer besides this complaint. – San Jacinto Dec 4 '09 at 18:50
The largest integer that can be represented in IEEE 754 double (64-bit) is the same as the largest value that the type can represent, since that value is itself an integer.
This is represented as 0x7FEFFFFFFFFFFFFF
, which is made up of:
- The sign bit 0 (positive) rather than 1 (negative)
- The maximum exponent
0x7FE
(2046 which represents 1023 after the bias is subtracted) rather than0x7FF
(2047 which indicates aNaN
or infinity). - The maximum mantissa
0xFFFFFFFFFFFFF
which is 52 bits all 1.
In binary, the value is the implicit 1 followed by another 52 ones from the mantissa, then 971 zeros (1023 - 52 = 971) from the exponent.
The exact decimal value is:
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
This is approximately 1.8 x 10308.
You need to look at the size of the mantissa. An IEEE 754 64 bit floating point number (which has 52 bits, plus 1 implied) can exactly represent integers with an absolute value of less than or equal to 2^53.
-
8
1.7976931348623157 × 10^308
http://en.wikipedia.org/wiki/Double_precision_floating-point_format
-
2
-
2@Carl well, if the integer has zeros beyond to the left, then it is precisely stored. – Wilhelm Dec 4 '09 at 18:27
-
4@all you downvoters: 1.7976931348623157 × 10^308 is an exact integer. Do you all need to attend remedial math classes or something?? – Dan Moulding Dec 4 '09 at 18:43
-
5We're down to semantics here in the discussion of this hopelessly sunk answer. True, that number can be represented exactly and thereby fulfills the letter of the question. But we all know it's a tiny island of exactitude in an ocean of near misses, and most of us correctly interpolated the question to mean "the largest number beyond which precision goes down the drain." Ah, isn't it wonderful that CompSci is an exact science? :) – Carl Smotricz Dec 4 '09 at 18:59
-
2@DanMoulding 1.7976931348623157 × 10^308 is an exact integer, but I am pretty sure this particular integer cannot be stored exactly in a double. – Pascal Cuoq Sep 26 '14 at 22:15
DECIMAL_DIG
from <float.h>
should give at least a reasonable approximation of that. Since that deals with decimal digits, and it's really stored in binary, you can probably store something a little larger without losing precision, but exactly how much is hard to say. I suppose you should be able to figure it out from FLT_RADIX
and DBL_MANT_DIG
, but I'm not sure I'd completely trust the result.
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – MichaelChirico Jul 31 '15 at 22:57
-
@MichaelChirico: This answers the question he intended to ask, as it existed when the answer was written. To see the question's edit history, click the "edited Jun 19 '14 at 11:40" link at the bottom of the question. – Jerry Coffin Aug 1 '15 at 0:02
-
your answer reads like a comment because it seems to lack the confidence/authoritativeness that an answer should have ("should give at least a reasonable..." "exactly how much... is hard to say" "I suppose...") . I have no expertise on the question asked or the answer, so i may be wrong; just putting in my two cents given I was sent here from the review queue (which I guess means other users flagged your answer). – MichaelChirico Aug 1 '15 at 0:09
-
1@MichaelChirico: They may well have--you're far from the only one who's ignorant of the subject matter; what makes you unusual is that you realize your'e ignorant of it. Most answers that sound authoritative about the precision of a floating point number in C are simply mistaken. For example, many (most) of them above are based on the false assumption that a
double
corresponds directly to a specific IEEE type, but that's not required, and when this answer was written the question didn't mention a particular IEEE type either. – Jerry Coffin Aug 1 '15 at 0:14 -
__STDC_IEC_559__
, meaning approximately "I have IEEE floats", then double is 64 bits. I think it's right that in IEEE-754/ IEC-60559, "double precision" means 64 bit, always. The question implies to me a C double which is some kind of IEEE float. This might not be an IEEE double, but it's certainly urged in that direction. Note the cautious wording of my answer, only mentioning IEEE 64 bit doubles, not C doubles ;-) – Steve Jessop Dec 4 '09 at 19:56