Rounding a float and casting to an int

This is a discussion on Rounding a float and casting to an int within the C Programming forums, part of the General Programming Boards category; Is this the right way to do this? Code: int input; float temp = input * (2/3); int output = ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    Rounding a float and casting to an int

    Is this the right way to do this?

    Code:
    int input;
    
    float temp = input * (2/3);
    
    int output = roundf(temp);
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,815
    Assuming that you actually give input a value, the problem would be that (2/3) results in integer division which truncates the result to 0. If you change it to (2.0f/3.0f) it should work. However, I wonder if this would be simpler yet give identical results:
    Code:
    int output = (input * 2) / 3;
    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
    Nov 2008
    Posts
    83
    Ah, yes. thank you.
    So, for
    int output = (input * 2) / 3;
    Is it truncating or rounding?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,815
    Quote Originally Posted by pollypocket4eva
    So, for
    int output = (input * 2) / 3;
    Is it truncating or rounding?
    Ah yes, that would truncate, not round.
    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
    Nov 2008
    Posts
    83
    For my purpose, either is fine. But was curious for future reference.
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. casting int and float pointers
    By mhaak in forum C Programming
    Replies: 3
    Last Post: 05-13-2005, 05:19 PM
  3. Converting a float to an int without truncation
    By SETmajor in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2004, 12:39 AM
  4. casting c++,const in class types,function return values...
    By ABitLazy in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2004, 03:44 PM
  5. Is this the best way to round up a float ?
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2001, 07:39 AM

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