tags:

views:

733

answers:

6

What is the biggest "no-floating" integer that can be stored in a double C type (IEEE) without loosing precision ?

A: 

1.7976931348623157 × 10^308

http://en.wikipedia.org/wiki/Double%5Fprecision%5Ffloating-point%5Fformat

Jay
this answer would be much better with a citation.
San Jacinto
@Carl well, if the integer has zeros beyond to the left, then it is precisely stored.
Wilhelm
@all you downvoters: 1.7976931348623157 × 10^308 **is** an exact integer. Do you all need to attend remedial math classes or something??
Dan Moulding
We'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
This is the correct answer to what was asked. The value DBL_MAX, which is the largest value which the IEEE double can represent IS an exact integer: in binary representation it is 53 ones followed by 971 zeros (and of course its exact expression in decimal notation is 308 digits long). However, the next smaller exact integer in the IEEE representation is 52 ones followed by 972 zeroes (i.e. a gap of 2^971). What the OP probably wanted was the upper limit of integer values that can be represented without gaps, which is 2^53 (as noted in other answers).
Stephen C. Steel
@Carl: But, given the question that was asked, is this answer *wrong*? No, it certainly isn't. And nobody really *knows* what the OP *meant* except, the OP. So why downvote an otherwise correct answer? Because you feel Jamie's talents of mind-reading aren't up to par?
Dan Moulding
Is the number given above the exact value of DBL_MAX? I'm not certain it isn't, but the Wikipedia article linked to indicates that it is approximate, and it certainly would be some coincidence for a number determined by the limits of a base-2 representation, to be divisible by quite that many powers of 10. Not that I downvoted this, just in my answer I indicated that my approximation was indeed an approximation.
Steve Jessop
+7  A: 

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.

Carl Smotricz
This is useless. The OP specifically asked for IEEE representation.
San Jacinto
San Jocinto.. WTF!?.Why The Frustration. This answers exactly what the OP asked..
Miky Dinescu
The OP specified IEEE standard, not "typical computer system." It makes a big difference sometimes. If this is IEEE standard, then state it as so and cite it with something as simple as "IEEE section___ states ____" Note, I didn't downvote.
San Jacinto
I think San Jocinto's complaint is that although it's the right answer, the questions is "what about IEEE?", and the answer is "on a typical system..." All that's missing is to note that a typical system uses IEEE doubles.
Steve Jessop
@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
If you edit your answer to say so, I'll even upvote :)
San Jacinto
@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
@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
A: 

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.

Jerry Coffin
+34  A: 

The biggest 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 x 10^308 (if your double is an IEEE 64 bit double). It's an integer. It's represented precisely. What more do you want?

Go on, ask me what the biggest 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 2^53:

  • 2^53 + 1 can't be stored, because the 1 at the start and the 1 at the end have too many zeros in between.
  • Anything less than 2^53 can be stored, with 52 bits explicitly stored in the mantissa, and then the exponent in effect giving you another one.
  • 2^53 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 2^(exponent - 52). So with exponent 52 you can store all values from 2^52 through to 2^53-1. Then with exponent 53, the next number you can store after 2^53 is 2^53 + 1 * 2^(53 - 52). So loss of precision first occurs with 2^53+1.

Steve Jessop
Well spotted. Otherwise FPUs would have to be much, much bigger.
Steve Jessop
+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
Or "messing about" and "trying to help" as I tend to call them :-)
Steve Jessop
I have juste added more precision in the description of my question. I was talking about the biggest "no-floating" integer.
Soubok
I bow to your superior analysis of the question. +1, nice one!
Carl Smotricz
@Soubok, you have to define what "no-floating" is first, because it sure isn't a term with a standard meaning!
Pavel Minaev
@Carl: *Everybody* bows to Steve "Who the hell is Jon Skeet?" Jessop :)
Dan Moulding
I bow to Tony the Pony, and no other.
Steve Jessop
@Pavel Minaev: Yes I know, but if I ask such a question, unfortunately this is because I don't know all standards :)
Soubok
You 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
I 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
+1  A: 

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.

Dolphin
It can exactly represent 2^53, too :-)
Steve Jessop
+6  A: 

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
pmg
nice test :)how many time for your CPU to count up to 9007199254740992 ?
Soubok
First run started at 0 but I Ctrl+C'd it after a few seconds :)
pmg