Thread: simple question about double

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    simple question about double

    Hello,

    Why wont the following code assign 0.5 to variable (double type):

    Code:
    unsigned int num = 1;
    double d = num/2;
    Thanks for help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're dividing integers, so the result will be an integer.

    1 and 2 are integer literals. 1.0 and 2.0 are double literals.

    As long as one of the two operands is a double, the result will be a double, so the solution is to use 2.0 instead of 2.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 03:17 PM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  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. Simple double sqrt, pow
    By Tarento in forum C Programming
    Replies: 1
    Last Post: 06-01-2006, 12:42 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM