| | |
Ascending & descending for loops give different answer in summation - why?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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)
#include <iostream> #include <iomanip> int main(int argc, char *argv[]) { double result=1.; std::cout << std::setprecision(20) << "\n:"; for(double i=1.; i<=10000.; ++i) { //std::cout << "\n" << i; result += 1./i; } std::cout << "\n\nSmall to large: " << result; result = 1.; for(double i=10000.; i>=1.; --i) { //std::cout << "\n" << i; result += 1./i; } std::cout << "\nLarge to small: " << result << "\n\n"; return 0; }
>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.
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.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
![]() |
Other Threads in the C++ Forum
- Previous Thread: understanding a problem
- Next Thread: competition and school problem solving and algorithms
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






