Thread: floor() and int()

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    floor() and int()

    Hi,
    I was wondering what's the difference between floor() and int() when taking the nearest smaller integer to a floating number? Which one is prefered?
    Thanks and regards!

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    They do different things. floor() rounds down, int() will truncate. The return types differ as well:

    floor(-3.5) == -4 (double)
    int(-3.5) == -3 (int)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I was wondering what's the difference between floor() and int() when taking the nearest smaller integer to a floating number?

    Conversion from float to int is (potentially) limited, so your best bet would be to stick with floating point specific functions.

    EDIT:
    >> floor(-3.5) == -4 (double)

    Missed that one.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by lehe View Post
    Hi,
    I was wondering what's the difference between floor() and int() when taking the nearest smaller integer to a floating number? Which one is prefered?
    Thanks and regards!
    It depends whether you needs to put the value of the double into an integer or not. If you don't need to, then you'd just use floor.
    Code:
    doubld d2 = floor(d1);
    If you do need to, I would consider using both to be more explicit.
    Code:
    int i = int(floor(d));
    By the way, int() as you queried about, is the constructor form of casting. The normal C++ way would be:
    Code:
    int i = static_cast<int>(floor(d));
    and the C way is
    Code:
    int i = (int)floor(d);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    39
    I always thought that if the values are integer and positive the would both return the same value...but that is the only time that is the case...

    Code:
    (int) floor (1.1) = 1
    (int)1.1 = 1
    If they were negative and integer then ceiling and truncation would give the same

    Code:
    (int) ceiling (-1.1) = -1
    (int) -1.1 = -1
    could be wrong however...

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    There's also trunc(). :-)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Cactus_Hugger View Post
    There's also trunc(). :-)
    ... is specific to unix (I'm not 100% sure, but suspect it may be specific to particular flavours or versions of unix).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by grumpy View Post
    ... is specific to unix (I'm not 100% sure, but suspect it may be specific to particular flavours or versions of unix).
    I don't think so, but I haven't taken to 30+ minutes of booting windows to check. The man page seems to indicate that it should be available in windows:
    Code:
    CONFORMING TO
           C99, POSIX.1-2001.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    39
    I don't think so, but I haven't taken to 30+ minutes of booting windows to check. The man page seems to indicate that it should be available in windows:
    CH is correct- trunc is available in C99 standard:

    trunc- Truncate to nearest integral value:

    double trunc(double x);

    returns x rounded to the integer nearest to it but no larger in magnitude

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ninety3gd View Post
    CH is correct- trunc is available in C99 standard:
    This is a C++ forum. C++ is only backward compatible to C89, not forward compatible to C99. trunc() is not included in either the C++ or C89 standards.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed