simple question about double

This is a discussion on simple question about double within the C++ Programming forums, part of the General Programming Boards category; Hello, Why wont the following code assign 0.5 to variable (double type): Code: unsigned int num = 1; double d ...

  1. #1
    l2u
    l2u is offline
    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,328
    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
    l2u
    l2u is offline
    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, 02: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, 08: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

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