|
|
 |
 |
 |
 |
Infinite loop
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 <stdio.h> #include <math.h> 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
<dmora @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. > Code: > #include <stdio.h> > #include <math.h> > 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
On Mar 17, 8:39 pm, "osmium" <r124c4u@comcast.net> wrote:
> <dmora @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. > > Code: > > #include <stdio.h> > > #include <math.h> > > 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
dmora @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 <stdio.h> > #include <math.h> > 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); > } > }
dmora @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
<dmora @cox.net> wrote: > On Mar 17, 8:39 pm, "osmium" <r124c4u @comcast.net> wrote: >> <dmora @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. >> > Code: >> > #include <stdio.h> >> > #include <math.h> >> > 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.
On Mar 17, 9:34 pm, "osmium" <r124c4u@comcast.net> wrote:
> <dmora @cox.net> wrote: > > On Mar 17, 8:39 pm, "osmium" <r124c4u @comcast.net> wrote: > >> <dmora @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. > >> > Code: > >> > #include <stdio.h> > >> > #include <math.h> > >> > 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
On 17 Mar 2007 18:45:31 -0700, "dmora@cox.net" <dmora@cox.net> wrote:
>On Mar 17, 8:39 pm, "osmium" <r124c4u @comcast.net> wrote: >> <dmora @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. >> > Code: >> > #include <stdio.h> >> > #include <math.h> >> > 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.
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
dmora@cox.net said: <snip> > 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.
<dmora @cox.net> wrote in message >> >> > #include <stdio.h> >> >> > #include <math.h> >> >> > 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.
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
On Mar 17, 8:45 pm, "dmora@cox.net" <dmora@cox.net> wrote:
> On Mar 17, 8:39 pm, "osmium" <r124c4u @comcast.net> wrote: > > <dmora@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. > > > Code: > > > #include <stdio.h> > > > #include <math.h> > > > 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.
"Malcolm McLean" <regniz @btinternet.com> writes: > in mathematics ... the funny-looking E notation used for sums <OT> By "funny-looking E" would you mean "Sigma", perchance? </OT> mlp
On Mar 18, 1:32 pm, "dmora@cox.net" <dmora@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. > int main() > { > 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);
What happens if the person types "x", or just presses Enter? > for(x=start;x=end;x+=step) {
'=' is the assignment operator. The equality test is '=='. Also, as others have pointed out, testing for equality with floating point types is unreliable. > y = (pow(260,.5)/130)*cos(2*x-1.463) + (67/20)*exp(x) + > (37/52)*exp(-3*x);
int divided by int gives int, in C, so 67/20 gives 3 and 37/52 gives 0. You can fix it by writing 67./20 and 37./52 > printf("%3.3f %3.3f\n",x,y); > }
Consider using 'double' instead of 'float'. You will also have to change your scanf modifiers to '%lf' (or preferably use fgets and strtod instead of scanf!).
"Thad Smith" <ThadSm @acm.org> wrote in message news:45fca28d$0$27529$892e0abb@auth.newsreader.octanews.com...
> dmora @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.
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
Norm Mann wrote: > "Thad Smith" <ThadSm @acm.org> wrote in message > news:45fca28d$0$27529$892e0abb@auth.newsreader.octanews.com... >>dmora@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. > 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
Malcolm McLean said: <snip> > 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.
In article <CbadnQx0r_GcmGPYRVny@bt.com>, Richard Heathfield <r@see.sig.invalid> wrote: >Malcolm McLean said: >> 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?
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 dj3va@csclub.uwaterloo.ca > You misspelled "typedef".
I had better go to sleep before I misspell "C". --pete and Brendan Sechter in comp.lang.c
Richard Heathfield <r @see.sig.invalid> writes: > Malcolm McLean said: [...] >> 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?
[...] That would be an integral, not a summation (which uses the "funny-looking E", also known as Sigma). -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson said: > Richard Heathfield <r @see.sig.invalid> writes: >> Malcolm McLean said: > [...] >>> 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? > [...] > That would be an integral, not a summation (which uses the > "funny-looking E", also known as Sigma).
<shrug> I certainly don't claim to be a mathematician but, if I remember my schooldays correctly, you can approximate the area under a curve by summing the areas of a series of narrow strips. I can understand that you might have some special meaning for "summation" which doesn't include this particular technique, but nobody mentioned "summation" until you did, I think. Just "notation used for sums". Why would such summing as I have described not constitute a sum? -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
Richard Heathfield <r @see.sig.invalid> writes: > Keith Thompson said: >> Richard Heathfield <r @see.sig.invalid> writes: >>> Malcolm McLean said: >> [...] >>>> 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? >> [...] >> That would be an integral, not a summation (which uses the >> "funny-looking E", also known as Sigma). > <shrug> I certainly don't claim to be a mathematician but, if I remember > my schooldays correctly, you can approximate the area under a curve by > summing the areas of a series of narrow strips. I can understand that > you might have some special meaning for "summation" which doesn't > include this particular technique, but nobody mentioned "summation" > until you did, I think. Just "notation used for sums". Why would such > summing as I have described not constitute a sum?
This has strayed off topic, but ... You can *approximate* the area by summing the areas of a series of narrow strips. To get the actual area, you need to take the limit as the width of the strips approaches zero, and the number of strips approaches infinity. With a finite number of strips, you have a summation, represented by an upper case Sigma (<http://en.wikipedia.org/wiki/Sum> looks reasonably accurate). In the limit, you have a definite integral, represented by an integral sign; that's integral calculs. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
In article <ufGdneCHEfWEhWPYnZ2dnUVZ8sbin@bt.com>, Richard Heathfield <r@see.sig.invalid> wrote: ><shrug> I certainly don't claim to be a mathematician but, if I remember >my schooldays correctly, you can approximate the area under a curve by >summing the areas of a series of narrow strips. I can understand that >you might have some special meaning for "summation" which doesn't >include this particular technique, but nobody mentioned "summation" >until you did, I think. Just "notation used for sums". Why would such >summing as I have described not constitute a sum?
The integral is the limit of the sum as the interval width tends to zero[1]. It's not itself a sum. This may seem like nitpicking, but details of this kind can lead to endless confusion - look at the innumerable articles in sci.math about whether 0.999... = 1 for example. [1] For Riemann integrals, there are others whose difference is not important here. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
"Richard Heathfield" <r @see.sig.invalid> wrote in message >>> 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). > <shrug> I certainly don't claim to be a mathematician but, if I remember > my schooldays correctly, you can approximate the area under a curve by > summing the areas of a series of narrow strips. I can understand that > you might have some special meaning for "summation" which doesn't > include this particular technique, but nobody mentioned "summation" > until you did, I think. Just "notation used for sums". Why would such > summing as I have described not constitute a sum?
One possible notation is --- 1.0 \ / i * i ---0 i+= 0.01 Another is --- 100 \ / i/100 * i/100 ----i = 0 either would work, but the second form is seen more often. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
dmora @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).
"Thad Smith" <ThadSm @acm.org> wrote in message news:45fddc88$0$27556$892e0abb@auth.newsreader.octanews.com...
> Norm Mann wrote: >> "Thad Smith" <ThadSm @acm.org> wrote in message >> news:45fca28d$0$27529$892e0abb@auth.newsreader.octanews.com... >>>dmora@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. >> 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.
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
On Mar 19, 4:21 pm, Tobias Rischer <tob@rischer.com> wrote:
> dmora @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).
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).
|
 |
 |
 |
 |
|