Re: allocate array
- From: nospam@xxxxxxxxxxxxx (Richard E Maine)
- Date: Thu, 9 Nov 2006 08:54:18 -0800
nakisa <nakisa.nooraee@xxxxxxxxx> wrote:
i have a question about allocate array.
I'm afraid you have bigger problems than that. :-(
do x=-2,2,0.01
The above is generally a bad idea. In fact, it is formally obsolescent.
Because of floatting point rounding, you basically can't tell how many
times this loop will go and you can't tell whether the last value will
be close to 2 or closer to 1.99. It may well vary from one compiler to
another. There are several alternative ways to do this, as discussed in
previous threads here.
if ( abs (function ) .eq. 0 ) then
The above test is an even worse idea. This one probably just won't work
at all unless you are unusually lucky. You can't expect any of your
function results to be exactly zero. In some situations, testing for a
value close to zero is ok, but for root finding, that's just not the
right method at all. You need to look instead for places where the value
changes sign. If the function might have a root at a point where it
doesn't change sign (i.e. that point is a local minimum or maximum),
then things are more complicated.
Also, the abs() here is pointless, although harmless. The standard
guarantees that it makes no difference here. Even if the processor
supports a negative zero (as most today do), the standard guarantees
that negative zero tests as equal to postive zero. I find it odd to see
code that worries about that subtle point, while ignoring the much
larger issue of the result not being exactly zero of either sign.
allocate ( A(? ), stat = ? )
Well, that's the right syntax, but the question here is what you are
trying to do. That isn't entirely clear from the code. I am guessing
that you want an array to store all of the roots that you find, not
knowing in advance how many that will be. If that's not it, then you'll
have to explain what it is.
There are many approaches to that kind of problem. In your case, you
seem to have a reasonable and easily computed upper limit on the number
of possible values. At any rate, the rest of your code implies that
because it only examines approximately 1+(2-(-2)) /.01 values of x. The
"approximately" is because of the floating point rounding issues
mentioned above. It is presumably unlikely that every one of those
points is a root, but then perhaps that is a possible degenerate case if
your function might degenerate to giving a constant zero result.
Thus, *IF* I were to close my eyes to the other issues in the code (and
that's really hard to do), I'd suggest computing the maximum size and
allocating the array before the loop instead of inside of it. Something
like
n_max = 2 + (upper_limit - lower_limit) / step_size
allocate(a(n_max))
where upper_limit is 2.0 for your case, lower_limit is -2.0, step_size
is 0.01, and the 2 really should be 1, but I added a harmless extra
location just for "insurance".
There is no point, in my opinion, in having a stat= here. If the
alocation fails, just let the compiler runtime give its error message
and abort, which is what it will do. That is infinitely better than
specifying stat= and then not doing anything useful with its result,
because then you'll still have the failure, but the syjptoms won't show
up until later in some form much harder to diagnose. Besides, the odds
of having an allocation failure on something this small are pretty low.
If the allocation faile, it is probably just a symptom of other things
already gone wrong elsewhere. The other problems noted above are far
bigger and more fundamental issues than worrying about allocation
failures.
After the loop is done, and you can count how many roots you actually
found, then you could allocate an array to exactly the right size and
copy the found roots to it.
Anyway, that would be my suggested answer to your allocation question,
if I focussed on it alone. But again, the rest of your code has far
bigger problems.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
.
- Follow-Ups:
- Re: allocate array
- From: glen herrmannsfeldt
- Re: allocate array
- References:
- allocate array
- From: nakisa
- allocate array
- Prev by Date: Re: array bounds checking -- comparing compilers
- Next by Date: Re: array bounds checking -- comparing compilers
- Previous by thread: allocate array
- Next by thread: Re: allocate array
- Index(es):
Relevant Pages
|