Names specified here
Name Description Notes Source Availability
ceil() Round to nearest integer towards +∞ M (·) <tgmath.h> C99 C11
ceil() Round to nearest integer towards +∞ (·) <math.h> C89 C90 C95 C99 C11
ceilf() Round to nearest integer towards +∞ (·) <math.h> C99 C11
ceill() Round to nearest integer towards +∞ (·) <math.h> C99 C11
FE_DOWNWARD Floating-point rounding mode: downward ? M <fenv.h> C99 C11
FE_TONEAREST Floating-point rounding mode: to nearest ? M <fenv.h> C99 C11
FE_TOWARDZERO Floating-point rounding mode: toward zero ? M <fenv.h> C99 C11
FE_UPWARD Floating-point rounding mode: upward ? M <fenv.h> C99 C11
FLT_ROUNDS Characteristic of float, double and long double L M <float.h> C89 C90 C95 C99 C11
fegetround() Get floating-point rounding direction (·) <fenv.h> C99 C11
fesetround() Set floating-point rounding direction (·) <fenv.h> C99 C11
floor() Round to nearest integer towards −∞ M (·) <tgmath.h> C99 C11
floor() Round to nearest integer towards −∞ (·) <math.h> C89 C90 C95 C99 C11
floorf() Round to nearest integer towards −∞ (·) <math.h> C99 C11
floorl() Round to nearest integer towards −∞ (·) <math.h> C99 C11
llrint() Round to nearest integer in current rounding direction M (·) <tgmath.h> C99 C11
llrint() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
llrintf() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
llrintl() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
llround() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
llround() Round to nearest integer away from zero at midpoint M (·) <tgmath.h> C99 C11
llroundf() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
llroundl() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
lrint() Round to nearest integer in current rounding direction M (·) <tgmath.h> C99 C11
lrint() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
lrintf() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
lrintl() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
lround() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
lround() Round to nearest integer away from zero at midpoint M (·) <tgmath.h> C99 C11
lroundf() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
lroundl() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
nearbyint() Round to nearest integer in current rounding direction without exception M (·) <tgmath.h> C99 C11
nearbyint() Round to nearest integer in current rounding direction without exception (·) <math.h> C99 C11
nearbyintf() Round to nearest integer in current rounding direction without exception (·) <math.h> C99 C11
nearbyintl() Round to nearest integer in current rounding direction without exception (·) <math.h> C99 C11
rint() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
rint() Round to nearest integer in current rounding direction M (·) <tgmath.h> C99 C11
rintf() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
rintl() Round to nearest integer in current rounding direction (·) <math.h> C99 C11
round() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
round() Round to nearest integer away from zero at midpoint M (·) <tgmath.h> C99 C11
roundf() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
roundl() Round to nearest integer away from zero at midpoint (·) <math.h> C99 C11
trunc() Round to nearest integer towards zero M (·) <tgmath.h> C99 C11
trunc() Round to nearest integer towards zero (·) <math.h> C99 C11
truncf() Round to nearest integer towards zero (·) <math.h> C99 C11
truncl() Round to nearest integer towards zero (·) <math.h> C99 C11

C provides many different ways of rounding real numbers. When a real floating-point value is converted to an integer, the value is truncated.

int x = 4.4; // x will take the value 4.

// Print 4.
printf("%d\n", (int) 4.4);

These operations, and most of the rounding functions below, can raise FE_INEXACT, if the result is not the same as the input.

[ Work in progress : But presumably without raising SIGFPE…?]
#include <math.h>
float ceilf(float x);
double ceil(double x);
long double ceill(long double x);
#include <tgmath.h>
real-floating-type ceil(real-floating-type x);

The ceil functions return the argument rounded to the nearest integer in the positive direction, ⌈x⌉.

#include <math.h>
float floorf(float x);
double floor(double x);
long double floorl(long double x);
#include <tgmath.h>
real-floating-type floor(real-floating-type x);

The floor functions return the argument rounded to the nearest integer in the negative direction, ⌊x⌋.

#include <math.h>
float truncf(float x);
double trunc(double x);
long double truncl(long double x);
#include <tgmath.h>
real-floating-type trunc(real-floating-type x);

The trunc functions return the argument rounded to the nearest integer towards zero.

C has the notion of rounding direction or rounding mode, and specifies four types, as shown in the table below:

Rounding directions
Direction Symbol for fegetround and fesetround FLT_ROUNDS Examples
-4.5 -3.8 +3.8 +4.5
towards zero FE_TOWARDZERO 0 -4.0 -3.0 +3.0 +4.0
nearest FE_TONEAREST 1 -5.0 or -4.0 -4.0 +4.0 +4.0 or +5.0
towards +∞ FE_UPWARD 2 -4.0 -3.0 +4.0 +5.0
towards -∞ FE_DOWNWARD 3 -5.0 -4.0 +3.0 +4.0

Macros with names matching ^FE_[A-Z] might be added to <fenv.h>.

<float.h> defines FLT_ROUNDS, which is an integer expression with one of the values listed above, indicating the rounding direction for floating-point addition. It expands to -1 if the rounding mode is indeterminable, and it may expand to other implementation-defined values.

#include <fenv.h>
#pragma STDC FENV_ACCESS ON
int fegetround(void);
int fesetround(int m);

A current rounding mode is defined as part of the floating-point environment, and affects the functions below. fegetround returns the current rounding mode (equal to one of the symbols in the table above), or -1 if it cannot be determined. fesetround attempts to set the current mode to m. It returns zero on success, and non-zero on failure.

#include <math.h>
float nearbyintf(float x);
double nearbyint(double x);
long double nearbyintl(long double x);
#include <tgmath.h>
real-floating-type nearbyint(real-floating-type x);

The nearbyint functions return the argument rounded to the nearest integer in the current rounding direction. They do not raise FE_INEXACT.

#include <math.h>
float rintf(float x);
double rint(double x);
long double rintl(long double x);
#include <tgmath.h>
real-floating-type rint(real-floating-type x);

The rint functions return the argument rounded to the nearest integer in the current rounding direction.

#include <math.h>
float roundf(float x);
double round(double x);
long double roundl(long double x);
#include <tgmath.h>
real-floating-type round(real-floating-type x);

The round functions return the argument rounded to the nearest integer in the current rounding direction, but always rounding half-way values away from zero.

#include <math.h>
long lrintf(float x);
long lrint(double x);
long lrintl(long double x);
long long llrintf(float x);
long long llrint(double x);
long long llrintl(long double x);
#include <tgmath.h>
long lrint(real-floating-type x);
long long llrint(real-floating-type x);

The lrint and llrint functions return the argument rounded to the nearest integer in the current rounding direction. They may produce a domain error or range error if the result does not fit in the return type.

#include <math.h>
long lroundf(float x);
long lround(double x);
long lroundl(long double x);
long long llroundf(float x);
long long llround(double x);
long long llroundl(long double x);
#include <tgmath.h>
long lround(real-floating-type x);
long long llround(real-floating-type x);

The lround and llround functions return the argument rounded to the nearest integer in the current rounding direction, but always rounding half-way values away from zero. They may produce a domain error or range error if the result does not fit in the return type.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000