floor, floorf
Calculates the floor of a value.
double floor( double x ); float floor( float x ); // C++ only long double floor( long double x ); // C++ only float floorf( float x );
Parameters
- x
-
Floating-point value.
The floor function returns a floating-point value representing the largest integer that is less than or equal to x. There is no error return.
| Input | SEH Exception | Matherr Exception |
|---|---|---|
| ± QNAN,IND | none | _DOMAIN |
floor has an implementation that uses Streaming SIMD Extensions 2 (SSE2). See _set_SSE2_enable for information and restrictions on using the SSE2 implementation.
| Function | Required header | Compatibility |
|---|---|---|
| floor, floorf | <math.h> | ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For additional compatibility information, see Compatibility in the Introduction.
// crt_floor.c
// This example displays the largest integers
// less than or equal to the floating-point values 2.8
// and -2.8. It then shows the smallest integers greater
// than or equal to 2.8 and -2.8.
#include <math.h>
#include <stdio.h>
int main( void )
{
double y;
y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );
y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}
Output
The floor of 2.8 is 2.000000 The floor of -2.8 is -3.000000 The ceil of 2.8 is 3.000000 The ceil of -2.8 is -2.000000
In case you want want to round-off floating point numbers to closest (upper/lower) integers like this:
1.0 -> round-off to 1
1.2 -> round-off to 1
1.5 -> round-off to 2
1.8 -> round-off to 2
2.4 -> round-off to 2
2.5 -> round-off to 3
Use something like this:
int round ( float f_val )
{
return ( f_val < 0.0 ) ? (int) ( f_val - 0.5 ) : (int) ( f_val + 0.5 );
}