Thread: Rounding error when casting double to short

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Rounding error when casting double to short

    I have a mathematical problem with C. For some reason the variables x and y in the code below are not exactly equal all of the time. I believe that the problem has to do with casting. How, exactly do I cast the equations for x and y so that they are always equal?

    Code:
    long x, y, w;
    double a, b;
    
    x = a*w/b+0.5;
    Last edited by thetinman; 10-23-2006 at 06:25 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What conditions make these not equal? Does it have to do with overflow?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I edited the first post. I misunderstood my problem. I still suspect though that I have a problem with casting.

    regarding your post. My variables are b = 350, 3 < a < 500, and 10 < w < 200,000.
    Last edited by thetinman; 10-23-2006 at 06:29 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Sorry, but there's really nothing you can do.

    double and float are just approximations, so most mathematical truisms don't apply.

    Case in point:
    Code:
    #include <iostream>
    
    int main (void) {
    	double a = 9.95;
    	double b = 1;
    	long x;
    
    	x = a/b + .05;
    	std::cout << x << '\n'; // 9
    	a *= 10;
    	b *= 10;
    
    	x = a/b + .05;
    	std::cout << x << '\n'; // 10
    
    	return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Both are 10 with your code compiled with gcc on a 32-bit machine.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by SKeane
    Both are 10 with your code compiled with gcc on a 32-bit machine.
    Except where they aren't both 10 on a 32-bit machine compiled with gcc in either C (with necessary modifications) or C++.
    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
    Sep 2001
    Posts
    752
    Quote Originally Posted by SKeane
    Both are 10 with your code compiled with gcc on a 32-bit machine.
    Your mileage may vary. Optomization, for example, will change the results. Typically, (int) (9.95 + 0.05) == 9 is a really safe bet.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by QuestionC
    Your mileage may vary. Optomization, for example, will change the results. Typically, (int) (9.95 + 0.05) == 9 is a really safe bet.
    Funny, I get 10.000000.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM