Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is fegetround() in C?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

c

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring