Embercode
✕
C++ LoopsQuick quiz

Float drift and output

6 questions. Covers Buffered output.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``cpp #include <iostream> int main() { double sum = 0.0; for (int i = 0; i < 10; ++i) { sum += 0.1; } std::cout << sum << ' ' << (sum == 1.0) << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <iostream> int main() { int count = 0; for (double t = 20.0; t <= 21.0; t += 0.1) { ++count; } std::cout << "count: " << count << '\n'; } ``
  3. 3. True or false, with a reason: for (double x = 0.0; x <= 1.0; x += 0.25) prints exactly five values, 0.0, 0.25, 0.5, 0.75 and 1.0.
  4. 4. True or false, with a reason: printing a double with the default precision shows the drift, so ten additions of 0.1 print as 0.99999999999999989.
  5. 5. Why must the counter loop divide by 10.0 in double t = 20.0 + i / 10.0; and not by 10?

5 question(s) left on this page.