ProgrammingVery hard

A sum of n = 10^5 terms is stored in a double. Is this a safe choice for exact integer sums up to 10^14?

  1. A. No, because doubles lose every integer digit after 10^6
  2. B. No, because doubles cannot add integers
  3. C. Yes, because doubles represent integers exactly up to 2^53, about 9 x 10^15
  4. D. Yes, because doubles wrap around like unsigned ints

Correct answer: C. Yes, because doubles represent integers exactly up to 2^53, about 9 x 10^15

A double holds integers exactly up to 2^53. The sum 10^14 is below that bound, so no rounding occurs, although a long long is the usual choice.

More C Programming questions

Exams that ask this topic