float

This is a discussion on float within the C++ Programming forums, part of the General Programming Boards category; How can i see if a float number doesnt have any ",", in other words, if it is an integer....

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    float

    How can i see if a float number doesnt have any ",", in other words, if it is an integer.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    if(myFloatNumber > (int) myFloatNumber)
        // you have an integer

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Desolation
    Code:
    if(myFloatNumber > (int) myFloatNumber)
        // you have an integer
    That won't work, since the cast truncates toward 0 even if myFloatNumber is negative. You should use "!=" instead of ">", which is more efficient anyway.

    Edit: I think there's a fairly small limit on the size of integer which can be represented exactly as a 32-bit IEEE 754 float, since there are only 23 bits for the mantissa.

    Edit: 23 bits should translate to being able to represent integers between roughly plus and minus 4 million. A 64-bit IEEE 754 double has 52 bits in the mantissa which provides a much broader range, though to take advantage of it one should cast to a larger than 32-bit integral type - such as long, if a long is 64 bits. Alternatively, instead of the cast you could use floor() or ceil(), defined in <cmath>, to return a floating-point number rounded to the next lower or higher integer, resp., and compare with that.
    Last edited by robatino; 01-17-2007 at 08:26 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Thank you!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Actually, my suggestions won't work either - see

    How can I test if something is an integer or not?

    Edit: I may still be right, upon further thought. Follow the above thread for a resolution.
    Last edited by robatino; 01-20-2007 at 10:24 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    just do the
    Code:
    if (x == floor(x))
    thing.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by iMalc
    just do the
    Code:
    if (x == floor(x))
    thing.
    Although as pointed out in the other thread:
    Quote Originally Posted by coder8137
    Often, a calculation will not result in the actual correct value, even though that correct value can be represented.
    So if x was derived from an arbitrary calculation, it may no longer be an integer, even if the exact value is.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Oh, so you mean that if you divided 1.0 by 3, and then multiplied that by 3, you would want this to return true? (given the answer would be very very close to 1)

    That was not my assumption. I considered this to be an exactness question, such that you could tell if the number stored was a result of having directly assigned an integer previously. In that case this should (and does) return false.

    In the non-exact case, you're better of comparing with an epsilon, and looking at the relative error. What you don't do is convert it to an integer type. Doubles can store numbers far above even the largest value a 64-bit number can represent. Of course you can just add more code to just return true if the number falls above that range, since the double can't possibly have any floating point part once it is that large, and you have to check the same for negatives also. But it is probably better to stay within floating point anyway for doing this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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