What is fegetround() in C?

The fegetround() function gets the floating-point rounding direction, or the current environment’s mode of rounding.

Library

#include<fenv.h>

Declaration

The following is the declaration of fegetround():

int fegetround();

This function does not take any parameters.

Modes of rounding

The four modes of possible rounding that can be returned are:

1. FE_TONEAREST

In this mode, the floating point value is rounded to the nearest integer.

FE_TONEAREST

2. FE_DOWNWARD

In this mode, the function returns floor of the floating-point value.

FE_DOWNWARD

3. FE_UPWARD

In this mode, the function returns ceil of the floating-point value.

FE_UPWARD

4. FE_TOWARDZERO

In this mode, the function returns the integer that is closer to zero of the floating-point value.

FE_TOWARDZERO

Note: The default mode of rounding is FE_TONEAREST.

Code

The code below illustrates the use of fegetround().

#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main()
{
printf("%ld", lrint(-0.7)); //rounding off the number
if (fegetround() == FE_TONEAREST) {
printf("\nfegetround() = FE_TONEAREST\n");
}
fesetround(FE_DOWNWARD);
printf("\n%ld", lrint(1.8)); //calculating floor of number
if (fegetround() == FE_DOWNWARD) {
printf("\nfegetround() = FE_DOWNWARD\n");
}
fesetround(FE_UPWARD);
printf("\n%ld", lrint(1.2)); //calculating ceil of number
if (fegetround() == FE_UPWARD) {
printf("\nfegetround() = FE_TOUPWARD\n");
}
fesetround(FE_TOWARDZERO);
printf("\n%ld", lrint(0.8)); //return number closer to zero
if (fegetround() == FE_TOWARDZERO) {
printf("\nfegetround() = FE_TOWARDZERO\n");
}
return 0;
}

In this code, the modes are set using fesetround(). The functionality of fegetround() is then verified using an if statement.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved