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?
- A. No, because doubles lose every integer digit after 10^6
- B. No, because doubles cannot add integers
- C. Yes, because doubles represent integers exactly up to 2^53, about 9 x 10^15
- 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
- A loop reads n numbers and stores them in int a[n] as a VLA of size up to 10^6 inside main. What is the main risk?
- To sort 10^6 integers with qsort, which comparator is correct for all int values?
- A counting pass over an array of 10^7 values, each in [0, 9], uses 10 counters. Which statement is correct?
- Which output method is fast enough for 10^6 lines of output in C?
- Which 2D DP table declaration is correct for a 1000 x 1000 table of ints in a contest C program?
- A modular sum over 10^6 terms uses sum = (sum + a[i]) % MOD with a[i] < MOD and sum an int. What is wrong?