Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

rint — rounding using the current mode

rint rounds a finite double to an integer-valued result using the current rounding direction, while retaining the double type.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <math.h>
double rint(double x);

For a finite argument, rint(x) from <math.h> returns an integer-valued result of type double, not int. The result depends on the current rounding direction; under FE_DOWNWARD, 2.75 becomes 2.0. On implementations conforming to Annex F, rint returns an infinity unchanged and returns a NaN for a quiet NaN input.

Unlike nearbyint, rint may raise the inexact exception; implementations conforming to Annex F raise it when the result differs from the finite argument. In contrast, round ignores the current direction and rounds halfway cases away from zero.

The example explicitly sets the direction through fesetround and requires support for FENV_ACCESS. Do not infer floating-point environment support solely from a C23 compiler flag.

Examples

#include <fenv.h>
#include <math.h>
#include <stdio.h>

#pragma STDC FENV_ACCESS ON

int main(void)
{
#ifdef FE_DOWNWARD
    if (fesetround(FE_DOWNWARD) != 0) {
        return 1;
    }
    volatile double x = 2.75;
    printf("%.0f\n", rint(x));
    return 0;
#else
    return 1;
#endif
}

Result

2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources