Thread: Convert float to int?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    Convert float to int?

    Can anyone explain how this is done.

    I am importing a couple values from a class and trying to divide them to get a fractional output from two int variables. I currently get a 0 for a and b when I divide. None of the variables are zero.

    Code:
    Rational operator < (Rational c)// compares two fractions <
    		{
    			float a = (numerator / denominator);
    			float b = c.numerator / c.denominator;
    			if (a < b)
    			{
    				truth = 0;
    				return truth;
    			}
    				
    			else 
    				truth = 1;
    				return truth;
    				
    	
    		}

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Use double instead of float to have more breathing room (aka more precision).

    Also, read here: http://www.cygnus-software.com/paper...ringfloats.htm
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    So, numerator and denominator are ints? You would need to cast them before you perform the 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

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Ive never heard of cast. I'm not using constants so I'm not sure how const_cast would help.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Code:
    float a = (static_cast<float>(numerator) / static_cast<float>(denominator));
    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

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Found an easier way to do it.

    float x,y,z;

    x = someIntVariable;
    y = someOtherIntVariable;

    z = x/y;

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Let me try this again...

    If you have the choice avoid float. Use double.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Actually in the real code I did. I just got a little too quick with my keyboard....my bad.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might want to make sure the denominator isn't zero before you go dividing by it.

    And you don't have to use
    Code:
    truth = 0;
    return truth;
    you can use
    Code:
    return 0;
    unless you use truth elsewhere.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by dwks
    And you don't have to use
    Code:
    truth = 0;
    return truth;
    you can use
    Code:
    return 0;
    unless you use truth elsewhere.
    Also, isn't the return value inverted. 1 is normally true is C/C++, init?

    Unless you particularly want truth to defy convention it would be better to have
    Code:
              return a<b;
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by kryonik
    Found an easier way to do it.
    float x,y,z;

    x = someIntVariable;
    y = someOtherIntVariable;

    z = x/y;
    If you're going to do that, you might as well do this:
    Code:
    float a = numerator;
    a = a / demoninator;

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or this:
    Code:
    double double_value(double numerator, double denominator) {
        return numerator / denominator;
    }
    
    int x = 1, y = 3;
    double number = double_value(x, y);
    Personally I don't see anything wrong with a cast. If laserlight's code
    Code:
    float a = (static_cast<float>(numerator) / static_cast<float>(denominator));
    is too long for your taste, you can cast only one variable (as swoopy's code relys on):
    Code:
    float a = (static_cast<float>(numerator) / (denominator));
    If that's still too long you could use a C-style cast (which probably isn't a good idea):
    Code:
    float a = (float)numerator / denominator;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM