943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2163
  • C++ RSS
Jun 23rd, 2008
0

Ascending & descending for loops give different answer in summation - why?

Expand Post »
Hi,

I'm at a loss to explain an issue I'm seeing below. It's a simple enough peice of C++, just sum all the values of 1/n from n=1 to n=10000. Easy, eh? The problem I have is explaining why when looping for the large to small bound i.e. 10000 down to 1 do I *always* get a slightly larger result than if I loop from 1 to 10000?

My gut instinct and some further testing tells me this is a rounding problem - for example if I use 'long double' then no rounding error is seen in the first 20dp, but given the same range in both loops, I can't explain why the decrementing loop comes out larger for any large-ish n?

I'd be eternally grateful if someone could explain this in detail or point me to a good link?

Cheers,

Phil.

Program output - GCC 4.0.1:
Small to large: 10.78760603604437307
Large to small: 10.787606036044381952

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. double result=1.;
  7. std::cout << std::setprecision(20) << "\n:";
  8. for(double i=1.; i<=10000.; ++i)
  9. {
  10. //std::cout << "\n" << i;
  11. result += 1./i;
  12. }
  13. std::cout << "\n\nSmall to large: " << result;
  14. result = 1.;
  15. for(double i=10000.; i>=1.; --i)
  16. {
  17. //std::cout << "\n" << i;
  18. result += 1./i;
  19. }
  20. std::cout << "\nLarge to small: " << result << "\n\n";
  21. return 0;
  22. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
falloutphil is offline Offline
4 posts
since Jun 2008
Jun 24th, 2008
1

Re: Ascending & descending for loops give different answer in summation - why?

>I'd be eternally grateful if someone could explain this in detail or point me to a good link?
Here's a good link, but the short answer is that due to floating-point imprecision, simply changing the order of arithmetic operations can affect the accuracy of the result.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 24th, 2008
0

Re: Ascending & descending for loops give different answer in summation - why?

Here's another article that might be of help.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Jun 24th, 2008
0

Re: Ascending & descending for loops give different answer in summation - why?

Thanks for the links all! I'll have a read through them.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
falloutphil is offline Offline
4 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: understanding a problem
Next Thread in C++ Forum Timeline: competition and school problem solving and algorithms





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC