Convert float to int?

This is a discussion on Convert float to int? within the C++ Programming forums, part of the General Programming Boards category; Can anyone explain how this is done. I am importing a couple values from a class and trying to divide ...

  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
    Portugal
    Posts
    7,532
    Use double instead of float to have more breathing room (aka more precision).

    Also, read here: http://www.cygnus-software.com/paper...ringfloats.htm
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    22,112
    So, numerator and denominator are ints? You would need to cast them before you perform the division.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,112
    Code:
    float a = (static_cast<float>(numerator) / static_cast<float>(denominator));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,532
    Let me try this again...

    If you have the choice avoid float. Use double.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,048
    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,048
    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, 06:26 PM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

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