Thread: Errors in Comparative Tests

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    18

    Errors in Comparative Tests

    I have the function below as part of my program but I'm having some problems with it. To summarize what some of it means, rmaxed is a number produced by multiplying user entered probability by RAND_MAX, rando is just a random number that comes from an earlier generated index, d is another user entered probability, and t is an integer that has to do with a counter. The reversal index is just a way of recording if at any point the else block is activated. Now I have the problem that when rlik=0 and rando=0 the else function is engaged. Also it seems the else block is ONLY triggered when rando is SMALLER than rlik. For instance if rando=7 and rlik=9 the else block will run. Conversely when rando is larger than rlik the if statement runs.

    The rest of the code in my program besides this function seems to be working perfectly but this is leaving me thoroughly confused. In another function of the program a <= test works perfectly fine.

    Code:
    int rev(int t, int index, int i) /*Regain sim*/
    {
        int rlik, rando; /*Probability of reversal, Random number from string*/
        rlik=rmaxed*pow((1-d), (t-1));
        rando=ran[ri];
        ri++;
        if(rando>rlik) return(0);
        else {
            reversal[index]=1;
            return(1);
        }
    }
    To put this more simply, the ">" is acting like a "<=" for no clear reason.
    Last edited by deeisenberg; 02-21-2012 at 01:32 PM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I can't vouch for your logic or intent but I see an error: when the 'if' condition is met, the function 'rev' does not return anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    Oh, sorry about that, I simplified the function when I pasted it here and meant to take out the b[bnum] stuff not the return(0) part. It does return a number either way.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    OK.
    You said "Now I have the problem that when rlik=0 and rando=0 the else function is engaged. Also it seems the else block is ONLY triggered when rando is SMALLER than rlik."
    That is exactly what you're checking with if (rando>rlik). I don't understand where you are having a problem.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    Quote Originally Posted by nonoob View Post
    OK.
    You said "Now I have the problem that when rlik=0 and rando=0 the else function is engaged. Also it seems the else block is ONLY triggered when rando is SMALLER than rlik."
    That is exactly what you're checking with if (rando>rlik). I don't understand where you are having a problem.
    Oh wow, I feel stupid. I think I just had one problem and then somehow thoroughly confused myself. I'm sorry for wasting your time >.<

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No problem. Next I was going to suggest that you may be running into round-off errors, where you are comparing two near-zero values... the solution would be to introduce a tolerance factor and a calculation. But then I noticed they are integers... so forget that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Unit Tests
    By nelson777 in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2011, 07:16 PM
  2. C++ tests.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 06-30-2006, 06:51 AM
  3. C tests
    By GravtyKlz in forum C Programming
    Replies: 2
    Last Post: 02-22-2003, 08:04 PM
  4. C Practice tests
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 07:31 PM
  5. Interview tests
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-20-2002, 05:40 PM