426,115 Members | 898 Online Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

# Infinite loop

 P: n/a Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was hoping someone could point me in the right direction. Code: #include #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave Mar 18 '07 #1
33 Replies

 P: n/a #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave Mar 18 '07 #2

 P: n/a On Mar 17, 8:39 pm, "osmium" #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the input is entered. Dave Mar 18 '07 #3

 P: n/a dm******@cox.net wrote: Hi all, I am a mathematician Didn't you start out with this exact same phrase in a post some time ago. As I recall, it start a flame thread. and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was hoping someone could point me in the right direction. Code: #include #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; Unless you've good reasons, you might consider using double for these objects. It provides greater default precision. printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { The = operator is the assignment operator. You probably meant <= or < or some other relational or logical relation. Otherwise the test simply assigns end to x and evaluates x. If x is other than zero, the loop will continue. So if the user had entered a positive or negative value for end, the program will enter an infinite loop. y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); Mismatched parenthesis. If you don't cut and paste the exact code that failed to compile or run, then it's guessing all the way. You should split up the above statement into multiple steps, with a temporary or two. By reordering it you can preserve more accuracy and enhance readability. printf("%3.3f %3.3f\n",x,y); } } Mar 18 '07 #4

 P: n/a dm******@cox.net wrote: for(x=start;x=end;x+=step) { y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } Besides the problem with the for loop, the expressions (67/20) and (37/52) are integer expressions that yield 3 and 0, respectively. To get floating point values, insert a decimal point: (67./27.). The other integers in the expression, 260, 130, 2, and 3, are already converted to type double because of 1) use as a parameter of type double and 2) operation with an expression of type double. In the case of (-3*x), the integer 3 is negated, yielding -3 as an integer, then converted to double (probably all at compile time) before multiplying by x. You can add decimal points to all these constants if you don't want to bother remembering the rules for when conversion to floating point is done. -- Thad Mar 18 '07 #5

 P: n/a #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the input is entered. In general, you should never test any floating point type for equality, such numbers are represented as approximations to the real underlying number. Testing for x==end is like trying to balance a dime on the rim. Changing to x<= end yields a short table of numbers for me. Mar 18 '07 #6

 P: n/a On Mar 17, 9:34 pm, "osmium" #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the input is entered. In general, you should never test any floating point type for equality, such numbers are represented as approximations to the real underlying number. Testing for x==end is like trying to balance a dime on the rim. Changing to x<= end yields a short table of numbers for me. Would it be better to use a do loop? I thought that I remember hearing something that you should use a for loop only if you are using integers to count. Dave Mar 18 '07 #7

 P: n/a On 17 Mar 2007 18:45:31 -0700, "dm******@cox.net" wrote: >On Mar 17, 8:39 pm, "osmium" #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the inputis entered. The conditional expression is evaluated before the first iteration. Since x is not equal to end, the expression evaluates to zero and looping is terminated even before the first iteration. Remove del for email Mar 18 '07 #8

 P: n/a dm******@cox.net said: Would it be better to use a do loop? I thought that I remember hearing something that you should use a for loop only if you are using integers to count. No, there is no such rule or guideline, or at least not amongst those who know the language well! If anything, your for(counter = start; counter < end; counter += step) example is actually a rather good example of when a for-loop is appropriate - but of course a while or a do would get the job done just as well. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 18 '07 #9

 P: n/a #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { >Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the input is entered. In general, you should never test any floating point type for equality,suchnumbers are represented as approximations to the real underlying number.Testing for x==end is like trying to balance a dime on the rim. Changingtox<= end yields a short table of numbers for me. Would it be better to use a do loop? I thought that I remember hearing something that you should use a for loop only if you are using integers to count. There are certain difficulties in using floats as the counter within a for loop. One is that, if the numbers are not integers, the test x != end may fail because of numerical precision. This can be fixed with x <= end + epsilon. Another problem is that programmers are so used to seeing for loops used to index arrays that it can be confusing to see one used for another purpose. The final problem in mathematics is that the funny-looking E notation used for sums, conventionally, takes integral indices. However if a for() loop most naturally expresses the logic of your calculation, then use one. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm Mar 18 '07 #10

 P: n/a On Mar 17, 8:45 pm, "dmora...@cox.net" #include int main() { float start = 0; float end = 0; float step = 0; float x = 0; float y = 0; printf("Enter the starting value\n"); scanf("%f", &start); printf("Enter the ending value\n"); scanf("%f", &end); printf("Enter the step\n"); scanf("%f", &step); for(x=start;x=end;x+=step) { Did you mean x==end ? y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + (37/52)*exp(-3*x); printf("%3.3f %3.3f\n",x,y); } } Thanks, Dave That stopped the infinite loop, but now, it's stopping after the input is entered. I think you're expecting the second part of the for loop to be an end condition, i.e. You want to stop when x is equal to end. But the second part is a boolean condition, i.e. the loop will continue until that condition is false. You probably want x < end instead of x == end, so the loop will stop when x >= end. Mar 18 '07 #11

 P: n/a "Malcolm McLean" By "funny-looking E" would you mean "Sigma", perchance? mlp Mar 18 '07 #12

 P: n/a On Mar 18, 1:32 pm, "dmora...@cox.net"

 P: n/a "Thad Smith" for(x=start;x=end;x+=step) {y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +(37/52)*exp(-3*x);printf("%3.3f %3.3f\n",x,y);} Besides the problem with the for loop, the expressions (67/20) and (37/52) are integer expressions that yield 3 and 0, respectively. To get floating point values, insert a decimal point: (67./27.). The other integers in the expression, 260, 130, 2, and 3, are already converted to type double because of 1) use as a parameter of type double and 2) operation with an expression of type double. In the case of (-3*x), the integer 3 is negated, yielding -3 as an integer, then converted to double (probably all at compile time) before multiplying by x. Since (pow(260,.5)/130), (67/20) and (37/52) are all constants, if you precompute them and insert them as actual values, you can save yourself some processing time. -NM Mar 18 '07 #14

 P: n/a Norm Mann wrote: "Thad Smith" >dm******@cox.net wrote: >>>for(x=start;x=end;x+=step) {y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +(37/52)*exp(-3*x);printf("%3.3f %3.3f\n",x,y);} Besides the problem with the for loop, the expressions (67/20) and (37/52)are integer expressions that yield 3 and 0, respectively. To get floatingpoint values, insert a decimal point: (67./27.). The other integers inthe expression, 260, 130, 2, and 3, are already converted to type doublebecause of1) use as a parameter of type double and2) operation with an expression of type double. Since (pow(260,.5)/130), (67/20) and (37/52) are all constants, if you precompute them and insert them as actual values, you can save yourself some processing time. True, but I prefer having the program compute them to show the derivation. If speed is an issue, you can compute the constants once in the code and assign to variables. -- Thad Mar 19 '07 #15

 P: n/a Malcolm McLean said: > Another problem is that programmers are so used to seeing for loops used to index arrays that it can be confusing to see one used for another purpose. I can't imagine any reason why such programmers should be molly-coddled. Let them expand their horizons a little. Would we stop people from measuring volts on a multimeter just because some people have never used it to measure anything but amps? The final problem in mathematics is that the funny-looking E notation used for sums, conventionally, takes integral indices. I've never seen /that/ written down anywhere. But okay - using only integral indices, how would you, for example, sum the area under the curve of y = x*x, between x = 0 and x = 1? However if a for() loop most naturally expresses the logic of your calculation, then use one. Er, quite so. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 19 '07 #16

 P: n/a In article , Richard Heathfield Malcolm McLean said: >The final problem in mathematics is that the funny-looking E notationused for sums, conventionally, takes integral indices. I've never seen /that/ written down anywhere. But okay - using onlyintegral indices, how would you, for example, sum the area under thecurve of y = x*x, between x = 0 and x = 1? Using an integral, not a sigma. It's worth noting, though, that the sigma notation doesn't require that indices are integers, only that they're discrete. I saw a lot of this at the beginning of the math course I'm currently taking: --- \ / sgn(pi) * A_(1,pi(1)) * ... * A_(n,pi(n)) --- pi in S_n Where pi is a permutation, S_n is the symmetric group of order n, and the only integers in sight are the subscripts to the 'A's. See also: -------- for(curr=head;curr;curr=curr->next) { /*For bonus brain explosion points, allow do_stuff to muck about with the list you're walking through. */ do_stuff(); } -------- dave -- Dave Vandervies dj******@csclub.uwaterloo.ca You misspelled "typedef". I had better go to sleep before I misspell "C". --pete and Brendan Sechter in comp.lang.c Mar 19 '07 #17

 P: n/a Richard Heathfield The final problem in mathematics is that the funny-looking E notationused for sums, conventionally, takes integral indices. I've never seen /that/ written down anywhere. But okay - using only integral indices, how would you, for example, sum the area under the curve of y = x*x, between x = 0 and x = 1? [...] That would be an integral, not a summation (which uses the "funny-looking E", also known as Sigma). -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 19 '07 #18

 P: n/a Keith Thompson said: Richard Heathfield Malcolm McLean said: [...] >>The final problem in mathematics is that the funny-looking Enotation used for sums, conventionally, takes integral indices. I've never seen /that/ written down anywhere. But okay - using onlyintegral indices, how would you, for example, sum the area under thecurve of y = x*x, between x = 0 and x = 1? [...] That would be an integral, not a summation (which uses the "funny-looking E", also known as Sigma).

 P: n/a Richard Heathfield Richard Heathfield >Malcolm McLean said: [...] >>>The final problem in mathematics is that the funny-looking Enotation used for sums, conventionally, takes integral indices.I've never seen /that/ written down anywhere. But okay - using onlyintegral indices, how would you, for example, sum the area under thecurve of y = x*x, between x = 0 and x = 1? [...]That would be an integral, not a summation (which uses the"funny-looking E", also known as Sigma). San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 19 '07 #20

 P: n/a In article , Richard Heathfield

 P: n/a "Richard Heathfield" >I've never seen /that/ written down anywhere. But okay - using onlyintegral indices, how would you, for example, sum the area under thecurve of y = x*x, between x = 0 and x = 1? [...]That would be an integral, not a summation (which uses the"funny-looking E", also known as Sigma).

 P: n/a dm******@cox.net wrote: Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was hoping someone could point me in the right direction. .... for(x=start;x=end;x+=step) { make that for (x = start; x <= end; x += step) First, what you meant was x==end (two equals, the most notorious typo-bug for C-programmers, at least for me). But because floating point arithmetic on computers is practically never precise, *never* compare floats or doubles with ==, in your case, you would certainly miss your end criterion by some small epsilon and march on through number space. Also, you should maybe check that (step >= 0 && start < end). Mar 19 '07 #23

 P: n/a "Thad Smith" "Thad Smith" >>dm******@cox.net wrote: for(x=start;x=end;x+=step) {y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) +(37/52)*exp(-3*x);printf("%3.3f %3.3f\n",x,y);}Besides the problem with the for loop, the expressions (67/20) and(37/52) are integer expressions that yield 3 and 0, respectively. To getfloating point values, insert a decimal point: (67./27.). The otherintegers in the expression, 260, 130, 2, and 3, are already converted totype double because of1) use as a parameter of type double and2) operation with an expression of type double. Since (pow(260,.5)/130), (67/20) and (37/52) are all constants, if youprecompute them and insert them as actual values, you can save yourselfsome processing time. True, but I prefer having the program compute them to show the derivation. If speed is an issue, you can compute the constants once in the code and assign to variables. I hail from a time when speed was always an issue and these stick out like sore thumbs. I would probably write out the equation as a comment and put the pre-computed values in anyway. I would also consider having the constants computed and assigned to variables outside the loop, but that won't prevent you from inadvertently doing integer operations instead of floating point operations because you forget to include decimal points. -NM Mar 19 '07 #24

 P: n/a On Mar 19, 4:21 pm, Tobias Rischer = 0 && start < end). There's no reason to suppose he only wants increasing tables. He just wants to avoid infinite loops. So he only needs to check that (step > 0 && start < end) || (step < 0 && start end) || (start == end). Mar 19 '07 #25

 P: n/a On Mar 19, 3:36 pm, Richard Heathfield

 P: n/a In article <11*********************@n76g2000hsh.googlegroups. com>, Old Wolf On Mar 19, 3:36 pm, Richard Heathfield I've never seen /that/ written down anywhere. It's not uncommon to use fractional indices, or summations thatappear to not involve a numeric variable at all (e.g. summingover some property of each item in a set). Perhaps Mr McLean'spoint was that you can usually (always?) re-write the summationto have an integral index. Or perhaps he's just talking crap. Somewhere in between, I would say. I don't _think_ I've ever seen a sigma used to represent a sum over an uncountable set, and anything that's indexing a countable set can be re-written to use integers as the index just by enumerating the set and summing over the enumeration. It's not hard to come up with examples (not necessarily even contrived ones) where it doesn't make sense to do that rewriting, though, and I don't think "indexible by integers" is the useful distinguishing property - discrete would be better. Exercise for the reader: Does a finitely describable uncountable set of discrete elements exist? (V guvax gur cbjre frg bs gur frg bs vagrtref dhnyvsvrf.) >But okay - using only integral indices, how would you, for example,sum the area under the curve of y = x*x, between x = 0 and x = 1? Not sure what you're asking. You would write this as an integral with0 and 1 as the bounds. If you mean, how would you write a C program,then you could use an integral type for the index, as a sort offixed-point calculation. If I weren't a pure mathematician, I'd be saying "Simpson's Rule" here. But, in the general case, you would need to integrate over a continuous interval instead of summing over a discrete set, and you'd use something other than a sigma to represent that. dave  By training, at least; by trade I'm a computer geek. -- Dave Vandervies dj******@csclub.uwaterloo.ca The Simpsons is a sick parody of everything else; at least they're being consistent in their treatment of religion. --Anthony de Boer in the scary devil monastery Mar 20 '07 #27

 P: n/a Richard Heathfield wrote: > .... snip ... > I've never seen /that/ written down anywhere. But okay - using only integral indices, how would you, for example, sum the area under the curve of y = x*x, between x = 0 and x = 1? Offhand, I would normalize x to the range 0..10,000 and use simpsons rule or the trapezoidal rule. Experiment with diddling the interval until the results are consistent. -- "A man who is right every time is not likely to do very much." -- Francis Crick, co-discover of DNA "There is nothing more amazing than stupidity in action." -- Thomas Matthews -- Posted via a free Usenet account from http://www.teranews.com Mar 20 '07 #28

 P: n/a /* Here's my version of your 1/e program: */ #include #include int main() { double start = 0; double end = 0; double step = 0; double x = 0; double y = 0; int converted; you_idiot_one: printf("Enter the non-negative starting value\n"); converted = scanf("%lf", &start); if (converted != 1 || start < 0.0) goto you_idiot_one; you_idiot_two: printf("Enter the ending value\n"); converted = scanf("%lf", &end); if (converted != 1 || end < start) goto you_idiot_two; you_idiot_three: printf("Enter the step\n"); converted = scanf("%lf", &step); if (converted != 1 || step < 0) goto you_idiot_three; for (x = start; x <= end; x += step) { y = (sqrt(260.0) / 130.0) * cos(2.0 * x - 1.463) + (67. / 20.) * exp(x) + (37. / 52.) * exp(-3. * x); printf("%25.18f %25.18f\n", x, y); } return 0; } /* C:\tmp>foo Enter the non-negative starting value 0 Enter the ending value 10 Enter the step 1 0.000000000000000000 4.074883071075573700 1.000000000000000000 9.248246041158287400 2.000000000000000000 24.653054101250959000 3.000000000000000000 67.264993538320653000 4.000000000000000000 183.023867843540100000 5.000000000000000000 497.105800281756560000 6.000000000000000000 1351.431551737800000000 7.000000000000000000 3673.845061976083800000 8.000000000000000000 9986.160974261707500000 9.000000000000000000 27145.247361213536000000 10.000000000000000000 73788.778437947738000000 */ Mar 21 '07 #29

 P: n/a On Mar 19, 6:23 pm, dj3va...@caffeine.csclub.uwaterloo.ca (Dave Vandervies) wrote: [snip] Exercise for the reader: Does a finitely describable uncountable set of discrete elements exist? (V guvax gur cbjre frg bs gur frg bs vagrtref dhnyvsvrf.) Vf gurer n sbezny cebbs gung gur frg bs nyy fhofrgf bs gur vagrtref vf hapbhagnoyr? Mar 21 '07 #30

 P: n/a On Mar 20, 8:32 pm, "user923005"

 P: n/a On Mar 20, 8:47 pm, "user923005"

 P: n/a In article <11**********************@o5g2000hsb.googlegroups. com>, user923005 On Mar 19, 6:23 pm, dj3va...@caffeine.csclub.uwaterloo.ca (DaveVandervies) wrote:[snip] >Exercise for the reader: Does a finitely describable uncountable setof discrete elements exist?(V guvax gur cbjre frg bs gur frg bs vagrtref dhnyvsvrf.) Vf gurer n sbezny cebbs gung gur frg bs nyy fhofrgf bs gur vagrtref vfhapbhagnoyr? Yes. Vg'f npghnyyl fyvtugyl zber trareny; nal frg bs nal pneqvanyvgl unf n fznyyre pneqvanyvgl guna vgf cbjre frg. I can dig the proof out of my algebra notes from a year or two ago if you're interested; this is getting rather far OT, so it's probably better to email me if you want it. (Using dj3vande at eskimo.com will make it rather less likely that I'll delete your email without noticing it while clearing the spam out from the address I post with.) dave  Protected from Google, not from spammers. -- Dave Vandervies dj******@csclub.uwaterloo.ca If only... there were people unburdened by the Hippocratic Oath on ambulances, so they could usefully be fitted with rocket launchers. --Maarten Wiltink in the scary devil monastery Mar 21 '07 #33

 P: n/a you_idiot_one: printf("Enter the non-negative starting value\n"); converted = scanf("%lf", &start); if (converted != 1 || start < 0.0) goto you_idiot_one; Apart that if, I input some non-numerical data, scanf will try to read it as a double over and over again, have you ever heard about "while"? C ain't Commodore BASIC... I am not one of those structured-programming worshippers who consider it immoral to ever write "goto", "break", or "return" anywhere else than as the last statement of a function, but here I just can't see how goto helps... do { printf("Enter the non-negative starting value\n"); /* You don't even need printf here, */ /* puts("Enter the non-negative starting value"); would do the job */ converted = scanf("%lf", &start); } while (converted != 1 || start < 0.0) Mar 21 '07 #34

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes 