While loop and tolerance

This is a discussion on While loop and tolerance within the C Programming forums, part of the General Programming Boards category; Im having a problem, trying to set up a loop. I have a value u, and I should check it ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    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
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I want to maintain an accuracy of 10^-4. I want to iterate until the value I obtain is less than 10^-4.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Well, any negative number is less than 10^-4, so that can't be all that your after...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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?
    Last edited by MethodMan; 10-05-2004 at 01:47 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Post some code
    hello, internet!

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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
    Last edited by pablo615; 10-05-2004 at 01:57 PM.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  11. #11
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Code:
    #define _abs(n) ( (n)<0 ? -(n) : (n) )
    //maybe this??
    while(_abs(d)>0.0001){
    //iterate at will
    }

  12. #12
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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...
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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

  14. #14
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Just noticed that, Ill try it out again.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetPixel tolerance
    By JordanCason in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2007, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21