What's the best way to make an expression evaluate as a double?

This is a discussion on What's the best way to make an expression evaluate as a double? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by King Mir That's why the minimal solution is to convert the 2. Alternatively you could cast MARGIN. ...

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by King Mir View Post
    That's why the minimal solution is to convert the 2. Alternatively you could cast MARGIN. Anything else and the result may be different. That's why the safest approach is to cast everything, and write everything in terms of doubles. Then there is less room for error.

    For example, casting WIDTH is not sufficient if MARGIN*2 will overflow an int (or whatever larger size MARGIN is).

    Which has always been the prevailing argument for using consts over #defines for magic numbers.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    I'm not getting what you are trying to say. Certainly if you declare the included constants as doubles there wouldn't be a problem. . .
    (though I would still advise using 2.0 and 1.0, since the other way technically involves a cast.)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have seen a lot of people who prefer for magic numbers to be explicitly assigned to a constant variable. Though you could always define them with an explicit cast too. Or just cast them as you said. But assuming there is no explicit cast defined, then you won't need a cast at all since everything will be promoted to a double until the point where you assign it to a non-double valued variable.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Errors that make no sense...
    By applescruff in forum C++ Programming
    Replies: 22
    Last Post: 03-04-2005, 04:44 PM
  3. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 09:54 AM
  4. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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