Click to See Complete Forum and Search --> : data structure


Jorgea
January 5th, 2008, 07:36 AM
1. In general programming, if you want to 'scan', or
do a calculation till the calculated value is closest
to some other value, how do you personally approach
it. I mean your ´thought process or flow chart. The
reason why I ask is that, I have always used the
approach where I use a Do loop till the calculated
value is greater than or equal to the test value. The
problem i have encountered is that at times the
closest value is actually JUST BEFORE the test value,
but I am confused as to what test clause to use. For
eg. 7.98 is closer to 8.00 than 8.04 is. So the code
is likely to fish out 8.04 whereas 7.98 was a better
answer.
I normally attempt to test with a range test clause.
i.e the test clause will read something like DO until
calc value is within 0.99Test val and 1.01Test val.
The problem I have encountered at times is that the
program simply 'dodges' the test val and simply hangs
since it just goes on indefinitely. The solution
should be pretty simple w/o any special program
specific command but I just can't figure it out. I
need more exact answers in a number of my programs. I
need fine tuned answers. Can anyone be of help?....with
typical foolproof test clauses I could use in my Do
loop structures?

Thanks for ur assistance!!!

wildfrog
January 5th, 2008, 11:20 AM
I don't know of any foolproof solution without knowing the exact problem, but:

I normally attempt to test with a range test clause. i.e the test clause will read something like DO until calc value is within 0.99Test val and 1.01Test val. The problem I have encountered at times is that the program simply 'dodges' the test val and simply hangs since it just goes on indefinitely.

You could always add some kind of maximum counter limiting the number of iteration your algorithm performs. This should keep it from 'hanging'.

- petter

GremlinSA
January 5th, 2008, 03:05 PM
You should acctually keep record of all inputs to the calculation and then calculate a percentage difference, for the results..

run your loop until you pass the result, after that you can analize the results to find the closest value..

hope this helps

Gremmy....

MikeAThon
January 5th, 2008, 03:20 PM
I typically test the difference between the calculated value and the target value, and continue iterating until the earlier of the difference being smaller than some small value or a mximum number of iterations:
int numIter = 0;
do
{
// calculate value
} while ( (numIter++<maxIter) || (fabs(calculated-target)<epsilon) )
Mike