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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 4
Reputation: falloutphil is an unknown quantity at this point 
Solved Threads: 0
falloutphil falloutphil is offline Offline
Newbie Poster

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

 
0
  #1
Jun 23rd, 2008
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,701
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 730
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #2
Jun 24th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

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

 
0
  #3
Jun 24th, 2008
Here's another article that might be of help.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 4
Reputation: falloutphil is an unknown quantity at this point 
Solved Threads: 0
falloutphil falloutphil is offline Offline
Newbie Poster

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

 
0
  #4
Jun 24th, 2008
Thanks for the links all! I'll have a read through them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC