Thread: int floor(double)?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    int floor(double)?

    is there a variation of the floor function that accepts a double parameter and returns an int?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well, the standard floor() accepts a double and returns a 'whole' double.

    So you could simply cast the result back to an int, ie

    Code:
    int my_floor(double n)
    {
        return (int) floor(n);
    }
    Or something, providing it fits in an integer
    Last edited by zacs7; 03-28-2008 at 09:08 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. (There are some rounding functions that return long int, like lround.)

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Woudnt casting it to an int have the same effect as floor()?
    Code:
    int floor(double a)
    {
      return (int)a;
    }
    EDIT: changed "int round(double a)" to "int floor(double a)"
    Last edited by 39ster; 03-28-2008 at 11:10 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by 39ster View Post
    Woudnt casting it to an int have the same effect as floor()?
    Code:
    int round(double a)
    {
      return (int)a;
    }
    Casting to int truncates, so -9.82 would go to -9, not -10.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Cast after you 'floor', truncating the 0 isn't going to mean anything.

    ie, floor 9.82 = 9.0, cast 9.0 = 9

  7. #7
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by 39ster View Post
    Woudnt casting it to an int have the same effect as floor()?
    Code:
    int round(double a)
    {
      return (int)a;
    }
    Code:
    int round(double a)
    {
      return (int) ( a + .5 );
    }
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    casting worked! lround() doesn't seem to work despite including math.h as a header file.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chickenandfries View Post
    casting worked! lround() doesn't seem to work despite including math.h as a header file.
    I thought it was clear from the name, but lround() rounds to nearest and doesn't calculate a floor. Or did you mean that it wasn't there? It's in math.h as of C99 at least.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Yes, I used lround() after performing floor(), but the compiler indicated "undefined reference to lround".

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by chickenandfries View Post
    is there a variation of the floor function that accepts a double parameter and returns an int?
    The best way to achieve this is exactly as you stated. Pass to floor(), and cast the result to int.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chickenandfries View Post
    Yes, I used lround() after performing floor(), but the compiler indicated "undefined reference to lround".
    What compiler? If gcc, be sure to include "-lm" at the end so that the linker finds the math library. If MSVC6, well, that's missing some C99 things. Everything else I'm aware of should have it.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by tabstop View Post
    Casting to int truncates, so -9.82 would go to -9, not -10.
    Forgot about negatives :/

    Quote Originally Posted by Aia View Post
    Code:
    int round(double a)
    {
      return (int) ( a + .5 );
    }
    Whoops. I meant int floor(double a), but it woudnt work anyways (as stated by tabstop)
    Last edited by 39ster; 03-28-2008 at 11:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM