Thread: (Short) Integer Division into float variable

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    Question (Short) Integer Division into float variable

    I'm tearing my hair out (again). This time I will simplify:

    Code:
    unsigned short foo1 = 31 ;
    unsigned short foo2 = 49 ;
    float bar ;
    bar = foo1/foo2 ; // I get zero. I want the result to be 0.633...
    I've tried putting (float) all over the division, same result, 0.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Recall that integer division is perform when both operands are integers. However, if one of the operands is an integer and the other is a float, double, or long double, the usual arithmetic conversions will convert the other operand into float, double, or long double respectively. Therefore:
    Code:
    bar = (float)foo1 / foo2;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Thank you, I thought I had tried that, but now it works. Weird!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by nigelmercier
    I thought I had tried that, but now it works. Weird!
    Obviously, you made a mistake, which is why instead of saying "I've tried putting (float) all over the division", you should have posted the code that you actually tried. For example, this is "(float) all over the division":
    Code:
    bar = (float)(foo1 / foo2);
    But it is obviously wrong because being "all over the division" doesn't help when the division is still integer division.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Yup, you are right.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Just a follow up, I had to use the code below, must be my compiler...

    Code:
    bar = (float)(foo1) / (float)(foo2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-13-2014, 06:32 PM
  2. [beginner] float and division
    By codezero in forum C Programming
    Replies: 5
    Last Post: 04-27-2009, 09:32 PM
  3. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  4. issues in float division a/b
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 04-24-2008, 06:15 AM
  5. int division to float
    By rimig88 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2008, 08:48 AM