Rounding error when casting double to short

This is a discussion on Rounding error when casting double to short within the C Programming forums, part of the General Programming Boards category; I have a mathematical problem with C. For some reason the variables x and y in the code below are ...

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

    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 07: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?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    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 07: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,006
    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, 09:38 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 03:54 PM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 01:04 PM
  4. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 10:20 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 10:17 AM

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