Name

floor

Synopsis

Rounds a real number down to an integer value

#include <math.h>
doublefloor( double x );
float floorf( float x );        (C99)
long double floorl( long double x );        (C99)

The floor() function returns the greatest integer that is less than or equal to its argument. However, the function does not have an integer type; it returns an integer value, but with a floating-point type.

Example

/* Scale a point by independent x and y factors */
struct point { int x, y; };

int width_orig = 1024, height_orig = 768;
int width_new = 800, height_new = 600;

struct point scale( struct point orig )
{
  struct point new;
  new.x = (int)floor( orig.x * (double)width_new  / (double)width_orig  );
  new.y = (int)floor( orig.y * (double)height_new / (double)height_orig );
  return new;
}

See Also

ceil(), round(); the C99 rounding functions that return floating-point types: trunc(), rint(), nearbyint(), nextafter(), and nexttoward(); the C99 rounding functions that return integer types: lrint(), lround(), llrint(), and llround(); the fesetround() and fegetround() functions, which operate on the C99 floating-point environment.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.