Thread: double division error (I get -0.0000)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    double division error (I get -0.0000)

    I'm using gcc on Linux.

    Whether I do this,

    Code:
    printf(" x=%5.4f<br>",145348224945.0000/3330389019.0000);
    or this,

    Code:
    printf(" x=%5.4f<br>",145348224945.0000L/3330389019.0000L);
    I get -0.0000 as the result. And I don't understand why.

    In the actual program, I have variables, of course.

    Code:
    int a, d;
    long b, c;
    long double x;
    
    x=(long double) (a*b) / (long double) (c*d);
    Is there any way to get around this error?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you sure you've got an integral value such as 145348224945?

    [edit]Maybe give this a whirl.
    Code:
    x= ((long double)a*b) / ((long double)c*d);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dorakura View Post
    Is there any way to get around this error?
    --edit stuff that is wrong--, and this code
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      printf(" x=&#37;5.4f<br>",145348224945.0000/3330389019.0000);
      return 0;
    }
    prints
    Code:
     x=43.6430<br>
    (also on gcc on Linux). If you want to print a long double, use %Lf.
    Last edited by tabstop; 03-07-2008 at 03:42 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Afterthoughts:
    1. MinGW users may bump into an issue with long doubles when playing along at home.
    2. This code does not necessarily show what it is attempting to show:
      Quote Originally Posted by tabstop View Post
      Code:
      #include <stdio.h>
      
      int main ( void )
      {
        printf(" x=%5.4f<br>",145348224945.0000/3330389019.0000);
        return 0;
      }
      prints
      Code:
       x=43.6430<br>
      Being constant, 145348224945.0000/3330389019.0000 might be calculated at compile time, so this code could effectively compile into
      Code:
      printf(" x=%5.4f<br>",43.6430171117);
    3. Any problem I see is that a*b and c*d result in longs which are then converted to long doubles. Overflowing the intermediate longs before the conversions is what I suspect as the cause of the unexpected output.

      But with that said, I'm trying this using MinGW, so I'm hosed by the above issue. What I tried to show by moving the cast, was that explicitly converting a into a long double should implicitly convert b and the result as well; same in the denominator. By doing the math with long doubles, I would expect this issue to be handled in an expected manner.

      For example:
      Code:
      #include <stdio.h>
      
      int main(void)
      {
         int a = 15 * 11, d = 3;
         long b = 880898333, c = 1110129673;
         double x = ((double)a * b) / ((double)c * d);
         double y = (double)(a * b) / (double)(c * d);
         printf("x = %g\n", x);
         printf("y = %g\n", y);
         return 0;
      }
      
      /* my output
      x = 43.643
      y = 0.705659
      */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dave_Sinkula View Post
    Afterthoughts:
    1. MinGW users may bump into an issue with long doubles when playing along at home.
    2. This code does not necessarily show what it is attempting to show:
      Being constant, 145348224945.0000/3330389019.0000 might be calculated at compile time, so this code could effectively compile into
      Code:
      printf(" x=%5.4f<br>",43.6430171117);
    Not my code, exactly, but true; in fact if I try to initialize a variable to either of these, bad things happen:
    Code:
    temp.c:7: warning: integer constant is too large for ‘long’ type
    temp.c:7: warning: overflow in implicit constant conversion
    temp.c:8: warning: this decimal constant is unsigned only in ISO C90
    So the second number would fit in an unsigned long, but long overflow is definitely happening with the first (assuming our systems are similar enough). -0.00000 is what you get when you printf a long double with %f instead of %Lf, though, is the point I was making.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by tabstop View Post
    -0.00000 is what you get when you printf a long double with %f instead of %Lf, though, is the point I was making.
    Ah, thanks. I couldn't see that with the MinGW/gcc.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    issue resolved

    It was a casting problem as pointed out above. I've made the following modifications and everything works now. Thank you so much for your help!

    Code:
    int a, d;
    long b, c;
    double x; /* long double turned out to be unnecessary */
    
    x=((double) a * (double) b) / ((double) c * (double) d);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM