Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

floor — C23 mathematical function

`floor(x)` from `<math.h>` returns the greatest integral floating value not greater than `x`; the result still has type `double`. `floorf` and `floorl` serve `float` and `long double`, and the environment rounding direction does not change this contract.

Version: C23

Syntax

double floor(double x);

floor(x) from <math.h> returns the greatest integral floating value not greater than x; the result still has type double. floorf and floorl serve float and long double, and the environment rounding direction does not change this contract.

The example calls floor for 3.8, -3.2, and -0.0. Output 3 -4 1 demonstrates rounding toward negative infinity, the difference from truncation toward zero, and preservation of the negative-zero sign.

A finite argument causes no domain or range error, but integral magnitudes beyond double precision may already have lost units before the call. Do not confuse floor with trunc, especially for negative values. Annex F requires preservation of ±0 and ±∞ and propagation of NaN.

Examples

#include <math.h>
#include <stdio.h>
int main(void) { printf("%.0f %.0f %d\n", floor(3.8), floor(-3.2), signbit(floor(-0.0)) != 0); return 0; }

Result

3 -4 1

The example calls `floor` for `3.8`, `-3.2`, and `-0.0`. Output `3 -4 1` demonstrates rounding toward negative infinity, the difference from truncation toward zero, and preservation of the negative-zero sign.

Sources