- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - fesetround() function
The C fenv library fesetround() function sets the rounding mode for arithmetic operations(floating-point). While performing this function to real world application it require specific calculation based on floating-point such as banking/financial software always round the value towards zero(interest calculation).
Syntax
Following is the C library syntax of the fesetround() function.
int fesetround(int round);
Parameters
This function accepts only a single parameter which is an interger. Below is the list of following parameter that determine the desired value(round) −
FE_TONEAREST: This parameter define the round to nearest which is zero.
FE_DOWNWARD: This parameter define the round value toward negative infinity.
FE_UPWARD: This parameter define the round value toward positive infinity.
FE_TOWARDZERO: This parameter define the round value zero.
Return Value
This function returns an integer value(zero), if it is successful. If the parameter is not a supported rounding mode, a nonzero value is returned.
Example 1
Following is basic C library program set the parameter FE_TONEAREST to the function fesetround() to see its demonstration.
#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main() {
double value = 9.5;
double results[4];
// Set rounding modes and perform calculations
fesetround(FE_TONEAREST);
results[0] = nearbyint(value);
fesetround(FE_DOWNWARD);
results[1] = nearbyint(value);
fesetround(FE_UPWARD);
results[2] = nearbyint(value);
fesetround(FE_TOWARDZERO);
results[3] = nearbyint(value);
// Print results
printf("Rounding FE_TONEAREST: %f\n", results[0]);
printf("Rounding FE_DOWNWARD: %f\n", results[1]);
printf("Rounding FE_UPWARD: %f\n", results[2]);
printf("Rounding FE_TOWARDZERO: %f\n", results[3]);
return 0;
}
Output
The above code produces the following result −
Rounding mode set to FE_TONEAREST successfully.
Example 2
Below the program demonstrates each and every parameter that compares the value of rounded mode with a single calculation.
#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main() {
double value = 9.5;
double results[4];
// Set rounding modes and perform calculations
fesetround(FE_TONEAREST);
results[0] = nearbyint(value);
fesetround(FE_DOWNWARD);
results[1] = nearbyint(value);
fesetround(FE_UPWARD);
results[2] = nearbyint(value);
fesetround(FE_TOWARDZERO);
results[3] = nearbyint(value);
// Print results
printf("Rounding FE_TONEAREST: %f\n", results[0]);
printf("Rounding FE_DOWNWARD: %f\n", results[1]);
printf("Rounding FE_UPWARD: %f\n", results[2]);
printf("Rounding FE_TOWARDZERO: %f\n", results[3]);
return 0;
}
Output
On execution of above code, we get the following result −
Rounding FE_TONEAREST: 10.000000 Rounding FE_DOWNWARD: 9.000000 Rounding FE_UPWARD: 10.000000 Rounding FE_TOWARDZERO: 9.000000
Example 3
In this program, we set the round mode(floating-point) to perform the task of division.
#include <stdio.h>
#include <fenv.h>
int main() {
// Set rounding mode to upward
fesetround(FE_UPWARD);
// Perform a division
double result = 10.0 / 3.0;
printf("Result with FE_UPWARD: %f\n", result);
return 0;
}
Output
After executing the above code, we get the following result −
Result with FE_UPWARD: 3.333334