-
While loop and tolerance
Im having a problem, trying to set up a loop.
I have a value u, and I should check it for a tolerance of 10^-4, and depending on its value I use u for different computations.
My problem is writing a while loop to check for the tolerance.
I have while (u < tolerance), but if u is negative, it jumps out of the loop, although the accuracy(tolerance) still isnt correct. I am aiming for an accuracy of 10^-4.
How can I structure it to correctly check for the accuracy?
Thanks
-
I'm confused. When you say tolerence, do you mean that u must be within 10^-4 of some other value, or must simply contain a value that is less than 10^-4?
-
I also am not sure what you're testing for. 10^-4 is 0.0001. So you're testing to see that d is less than that? Is there any way you could post a bit of code, or better explain what it is you're trying to do?
Quzah.
-
I want to maintain an accuracy of 10^-4. I want to iterate until the value I obtain is less than 10^-4.
-
Well, any negative number is less than 10^-4, so that can't be all that your after...
-
Define "maintain an accuracy of". I think I understand what you're wanting, but you're doing it wrong. If you have a variable d and you want to make sure that as you fiddle with it it that it's within n "tollerance", then you need to do:
Code:
while( d > minimumvalue && d < maximumvalue )
It doesn't matter what "tollerance" you want. It could be within a million, 10, .0001, whatever. The point is, you have to check two values, not just one. Otherwise you're not checking tollerance, you're checking to see that it is less than, or greater than one specific value. You need to check both an upper and lower boundary.
Quzah.
-
That is where my problem lies.
The number can be -2220.0, but it doesnt have an accuracy of 10^-4.
The numbers will get so small that they are within an accuracy of 0.0000XXX
Does that make it clear?
-
-
Code:
while ((u < 0) || (u > 0.00001f))
This way negative numbers will re-enter the loop and any pos # greater than tolerence will enter the loop.
Too many edits...this is what i get for listening to music, surfing, compiling and debugging all at the same time :)
-
Ok I think I understand what he means. He has a loops that is running some sort of test of some sort. Its suppose to calculate something of some sort and each time it iterates through the loops the calculation yields a more percise value. His problem is he doesn't know how exacty to have the loop stop when the loop calculates a value to the ten-thousandths place.
-
Code:
#define _abs(n) ( (n)<0 ? -(n) : (n) )
//maybe this??
while(_abs(d)>0.0001){
//iterate at will
}
-
What master5001 said is exactly what I am trying to do.
while((tempu > TOLERANCE) && (tempv > TOLERANCE))
That is currently my while loop, I have 2 values to check the tolerance of, however, it still does not enter the loop, even after I have taken the absolute value of them...
-
You want to enter the loop if the values are GREATER than the tolerence so that you can operate on them and attempt to reduce them. Change < to > and you should be set.
PK
-
Just noticed that, Ill try it out again.
-
Also don't forget to check and make sure the values are greater than 0, otherwise negative numbers will cause the loop to terminate. See my post above...
PK