Thread: floating point value

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    floating point value

    heyas,

    i was wondering if theres a way to determind if a float has a decimal value?

    i.e i might have the following times

    24:00
    09:00
    14:30

    i want to know if i can iterate through an array or any collection and test if the '.30' exists.

    or even more simply a function like isDecimal()

    tia

    -twans

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    my appologies,

    those times should read:

    24.00
    09.00
    14.30

    as im storing them as floats! :P

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If the number minus the floor of the number is anything other than zero, then there are values stored after the decimal place.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The only thing I can think of, is to subtract .30 from the number, and then use the modulus operator with a 1. If it was n.30, your answer will be 1.

    Remember though, that floating point values can never be trusted to be exact, so don't use the == operator to test if it's 1. You could find the absolute value of (1 - yourNumber), and then test to see if that number is less then .1 or something.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    works a charm,

    those that are interested
    Code:
    #include <cmath>
    
    floor(float);
    thanks for your help josh

    -twan

    [edit: silly typo on the include]
    Last edited by twans; 04-07-2005 at 08:40 AM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You're welcome.

    The standard library function floor() is in <cmath>. What is <floor>?

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    can the modulus operator be used on floats?

    -thanks for the reply too sean.
    Last edited by twans; 04-07-2005 at 08:44 AM.

  8. #8
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Modulus only works on ints I believe.

    You could get dirty and cast to int and back to float... something like this:

    Code:
    float x = 3.2f;
    bool hasDecimal = x != (float)((int)x);
    Note: this code hasn't been tested, I just wanted to get the idea across
    Last edited by TheColonial; 04-07-2005 at 08:53 AM. Reason: Changed == to !=

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    There's an fmod(float x, float y) function in <cmath> that returns the remainder of x/y.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by twans
    can the modulus operator be used on floats?

    -thanks for the reply too sean.
    Use the fmod function to calculate modulus on floating-point values.

    [edit]Darn... beaten! [/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM