Infinite loop

D

dmoran21

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
 
O

osmium

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 ?
 
S

santosh

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.
 
O

osmium

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.
 
D

dmoran21

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
 
T

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.

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.
 
B

Barry Schwarz

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
 
R

Richard Heathfield

(e-mail address removed) 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.
 
M

Malcolm McLean

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.
 
C

Chris Johnson

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.
 
M

Mark L Pappin

Malcolm McLean said:
in mathematics ... the funny-looking E notation used for sums

<OT>
By "funny-looking E" would you mean "Sigma", perchance?
</OT>

mlp
 
O

Old Wolf

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!).
 
N

Norm Mann

Thad Smith said:
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
 
T

Thad Smith

Norm said:
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.
 
R

Richard Heathfield

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.
 
D

Dave Vandervies

Malcolm McLean said:

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 (e-mail address removed)
You misspelled "typedef".
I had better go to sleep before I misspell "C".
--pete and Brendan Sechter in comp.lang.c
 
K

Keith Thompson

Richard Heathfield said:
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).
 
C

CBFalconer

Richard said:
.... 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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"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
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top