function
<cfenv>

fesetround

int fesetround (int rdir);
Set rounding direction mode
Sets rdir as the current rounding direction mode for the floating point environment.

Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Parameters

rdir
One of the values defined as rounding direction mode:
macro valuedescription
FE_DOWNWARDRound downward.
FE_TONEARESTRound to nearest.
FE_TOWARDZERORound toward zero.
FE_UPWARDRound upward.
Certain library implementations may support additional floating-point rounding directions values (with their corresponding macros also beginning with FE_).
Libraries may define in <fenv.h> only the macro values above they support (the others may not be defined).
At least all of the above macro values are defined in <cfenv>.
If an int value other than those defined by these macros is passed, the function does not change the rounding direction mode and returns a non-zero value.

Return Value

Zero, if the requested rounding direction was successfully set.
Otherwise, a non-zero value.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* fesetround example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* fesetround, FE_* */
#include <math.h>       /* rint */
#pragma STDC FENV_ACCESS on

int main ()
{
  printf ("rounding -3.8:\n");

  fesetround(FE_DOWNWARD);
  printf ("FE_DOWNWARD: %.1f\n", rint(-3.8));

  fesetround(FE_TONEAREST);
  printf ("FE_TONEAREST: %.1f\n", rint(-3.8));

  fesetround(FE_TOWARDZERO);
  printf ("FE_TOWARDZERO: %.1f\n", rint(-3.8));

  fesetround(FE_UPWARD);
  printf ("FE_UPWARD: %.1f\n", rint(-3.8));
  return 0;
}


Possible output:

rounding -3.8:
FE_DOWNWARD: -4.0
FE_TONEAREST: -4.0
FE_TOWARDZERO: -3.0
FE_UPWARD: -3.0

Data races

Each thread maintains a separate floating-point environment with its own state. Spawning a new thread copies the current state. [This applies to C11 and C++11 implementations]

Exceptions

No-throw guarantee: this function never throws exceptions.

See also