"Michael" <yl*********@126.com> wrote in message
news:df***********@mail.cn99.com...
Hi.
Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?
[snip]
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value
between 0 and 1;
if(check(x)==0) goto mark;
for(j=1; j<=100; j++) A[i][j]=x[j];
}
where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x[i]<0)||(x[i]>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array[i]*x[i];
if(sum != 0) return 0;
Try this line instead, and see whether or not it dramatically decreases run
time:
if(sum>SUM_EPS) return 0;
where SUM_EPS is #defined as something like 0.0001 (or some other, suitable
small, epsilon).
return 1;
}
The problem may be that for many interations "sum" comes out to something
like 0.0000438764, which for your purposes *equals* 0. However, since
that's not actually equal to 0 (to your processor, anyway), your "check"
function returns 0 and the program loops again.
In fact, as far as I can tell, unless "array[]" is full of 0s your code is
dependent upon floating-point underflow to ever complete. IMHO depending on
underflow for program completion is usually not a Good Thing.
-Charles