Thread: Weird problem -- negative doubles not being processed by my code

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    67

    Weird problem -- negative doubles not being processed by my code

    I'm trying to write a very basic if() conditional statement, which in its ideal/simplest form, should look like this:

    Code:
     
    
    double Lq[5] = {0.0}; 
    double Lq_sign = 0.0; 
    
    /* call function to assign values to Lq[ ] array */ 
    
    for(j = 0; j < 5; j++){ 
         Lq_sign = 0.0; 
    
         if(Lq[j] >= 0.005) Lq_sign = 1.0;
         else if(fabs(Lq[j]) < 0.005) Lq_sign = 0.0;    
         else if(Lq[j] <= -0.005) Lq_sign = -1.0; 
    
    }
    When I print the resulting Lq_sign values for each iteration, the values of 1.0 and 0.0 are correctly assigned with no problems. However, the assignment of -1.0 never happens (the Lq values that *should* be -1.0 show up as 0.0).

    I've done some troubleshooting, to determine why the negative Lq values aren't being recognized. Below are the various troubleshooting statements that I've performed:

    Code:
     
    
    ...
    else if(fabs(Lq[j]) > 0.005) printf("abs val is > 0.005.\n"); /* A */
    else if(Lq[j] < 0.0) printf("Lq[%d] is < 0.\n", j);   /* B */ 
    else if((fabs(Lq[j]) > 0.005) && (Lq[j] < 0)) Lq_sign = -1.0; /* C */
    Statement A above always executes when it should, whether the Lq value in question is + or --. However, statement B *never* executes, although there are plenty of negative Lq values that should trigger this statement. And, of course, statement C never executes.

    Does anyone have a suggestion about why my negative Lq[ ] array values aren't being recognized as negative?? Thanks in advance for your input.
    Last edited by CodeKate; 10-11-2011 at 05:37 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know what abs does? (Other than make me look ripped?) It makes a number not negative! Why would you ever reach B?
    Code:
    if number > 0.0005 then
        ... 
    else
    if( FABS < 0.0005 )
    ...
    The only thing you have not covered here is an exact match for 0.005. I think the only way you could possibly reach B is if you enter -0.0005. Have you tried that?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Aha! I hadn't been aware that calling fabs( ) was actually *changing* the value!

    Here is my revised code, which now does work:

    Code:
    if(Lq[j] >= 0.005) Lq_sign = 1.0;
    else if((Lq[j] > -0.005) && (Lq[j] < 0.005)) Lq_sign = 0.0;
    else if(Lq[j] < 0) Lq_sign = -1.0;
    Thanks for pointing that out, quzah.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I trust you also realize Lq_sign being a single variable will only ever reflect the sign of the last element in your Lq[] array?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    I trust you also realize Lq_sign being a single variable will only ever reflect the sign of the last element in your Lq[] array?
    Yes, that is intentional, Tater, but thanks for pointing it out. I clear and reuse that variable each time through the loop.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CodeKate View Post
    Aha! I hadn't been aware that calling fabs( ) was actually *changing* the value!
    What is it you think it actually does? If you give it -1.3, it returns 1.3. But it doesn't actually change what you pass it into something else:
    Code:
    #include<math.h>
    #include<stdio.h>
    int main( void )
    {
        float f = -1.3;
        if( fabs( f ) > 1.0 )
            printf( "f is still %f\n", f );
    
        return 0;
    }
    It doesn't actually change what f is.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    It doesn't actually change what f is.
    Ok, that was my initial belief about the action of calling fabs(). However, I still don't understand why my third condition in my OP wasn't executing:

    Code:
    if(Lq[j] >= 0.005) Lq_sign = 1.0;      
    else if(fabs(Lq[j]) < 0.005) Lq_sign = 0.0;          
    else if(Lq[j] <= -0.005) Lq_sign = -1.0;
    The negative values had absolute values greater than 0.005, and yet the 3rd condition never executed...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CodeKate View Post
    Yes, that is intentional, Tater, but thanks for pointing it out. I clear and reuse that variable each time through the loop.
    You're original code sample did not depict any other processing than a rather tight loop scanning the array. With what you showed us, the only reasonable conclusion is that on exiting the loop Lq_sign is going to be set to whatever the last value in the array is...

    I know I'm nit picking... but for clarity, it would help if you would indicate other processing, even as a comment, when posting code.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    I know I'm nit picking... but for clarity, it would help if you would indicate other processing, even as a comment, when posting code.
    Sorry about that. I'll try to avoid such omissions in future posts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubles Problem
    By Jony in forum C Programming
    Replies: 1
    Last Post: 08-21-2011, 11:48 PM
  2. Weird code execution order problem
    By Syko in forum C Programming
    Replies: 2
    Last Post: 05-12-2005, 02:29 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. Problem with doubles
    By Asbestos in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2005, 05:47 PM
  5. weird problem with code, not displaying what it should
    By chris285 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 11:04 AM