Namespaces
Variants
Views
Actions

FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD

From cppreference.com
< c‎ | numeric‎ | fenv
Defined in header <<fenv.h>>
#define FE_DOWNWARD     /*implementation defined*/
(since C99)
#define FE_TONEAREST    /*implementation defined*/
(since C99)
#define FE_TOWARDZERO   /*implementation defined*/
(since C99)
#define FE_UPWARD       /*implementation defined*/
(since C99)

Each of these macro constants expands to a nonnegative integer constant expression, which can be used with fesetround and fegetround to indicate one of the supported floating-point rounding modes. The implementation may define additional rounding mode constants in <fenv.h>, which should all begin with FE_ followed by at least one uppercase letter. Each macro is only defined if it is supported.

On most implementations, these macro constants expand to the values equal to the values of FLT_ROUNDS and float_round_style

Constant Explanation
FE_DOWNWARD rounding towards negative infinity
FE_TONEAREST rounding towards nearest integer
FE_TOWARDZERO rounding towards zero
FE_UPWARD rounding towards positive infinity

Additional rounding modes may be supported by an implementation.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <fenv.h>
 
#pragma STDC FENV_ACCESS ON
void show_fe_rounding_methods(void)
{
    printf("    +12.0 -> %+4.1f\n", nearbyint(+12.0));
    printf("    +12.1 -> %+4.1f\n", nearbyint(+12.1));
    printf("    -12.1 -> %+4.1f\n", nearbyint(-12.1));
    printf("    +12.5 -> %+4.1f\n", nearbyint(+12.5)); /* midway between two integers */
    printf("    +11.5 -> %+4.1f\n", nearbyint(+11.5)); /* midway between two integers */
    printf("    +12.9 -> %+4.1f\n", nearbyint(+12.9));
    printf("    -12.9 -> %+4.1f\n", nearbyint(-12.9));
    printf("\n");
}
 
int main()
{
    fesetround(FE_DOWNWARD);
    printf("rounding using FE_DOWNWARD:\n");
    show_fe_rounding_methods();
 
    fesetround(FE_TONEAREST);
    /* If the result is midway between two integers, */
    /* the even integer is chosen.                   */
    printf("rounding using FE_TONEAREST:\n");
    show_fe_rounding_methods();
 
    fesetround(FE_TOWARDZERO);
    /* truncation */
    printf("rounding using FE_TOWARDZERO:\n");
    show_fe_rounding_methods();
 
    fesetround(FE_UPWARD);
    printf("rounding using FE_UPWARD:\n");
    show_fe_rounding_methods();
 
    return 0;
}

Output:

rounding using FE_DOWNWARD:
    +12.0 -> +12.0
    +12.1 -> +12.0
    -12.1 -> -13.0
    +12.5 -> +12.0
    +11.5 -> +11.0
    +12.9 -> +12.0
    -12.9 -> -13.0
 
rounding using FE_TONEAREST:
    +12.0 -> +12.0
    +12.1 -> +12.0
    -12.1 -> -12.0
    +12.5 -> +12.0
    +11.5 -> +12.0
    +12.9 -> +13.0
    -12.9 -> -13.0
 
rounding using FE_TOWARDZERO:
    +12.0 -> +12.0
    +12.1 -> +12.0
    -12.1 -> -12.0
    +12.5 -> +12.0
    +11.5 -> +11.0
    +12.9 -> +12.0
    -12.9 -> -12.0
 
rounding using FE_UPWARD:
    +12.0 -> +12.0
    +12.1 -> +13.0
    -12.1 -> -12.0
    +12.5 -> +13.0
    +11.5 -> +12.0
    +12.9 -> +13.0
    -12.9 -> -12.0

[edit] See also

gets or sets rounding direction
(function) [edit]
C++ documentation for floating point rounding macros