The fegetround()
function gets the floating-point rounding direction, or the current environment’s mode of rounding.
#include<fenv.h>
The following is the declaration of fegetround()
:
int fegetround();
This function does not take any parameters.
The four modes of possible rounding that can be returned are:
In this mode, the floating point value is rounded to the nearest integer.
In this mode, the function returns floor of the floating-point value.
In this mode, the function returns ceil of the floating-point value.
In this mode, the function returns the integer that is closer to zero of the floating-point value.
Note: The default mode of rounding is
FE_TONEAREST
.
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 numberif (fegetround() == FE_TONEAREST) {printf("\nfegetround() = FE_TONEAREST\n");}fesetround(FE_DOWNWARD);printf("\n%ld", lrint(1.8)); //calculating floor of numberif (fegetround() == FE_DOWNWARD) {printf("\nfegetround() = FE_DOWNWARD\n");}fesetround(FE_UPWARD);printf("\n%ld", lrint(1.2)); //calculating ceil of numberif (fegetround() == FE_UPWARD) {printf("\nfegetround() = FE_TOUPWARD\n");}fesetround(FE_TOWARDZERO);printf("\n%ld", lrint(0.8)); //return number closer to zeroif (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