FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD

From cppreference.com
< cpp‎ | numeric‎ | fenv

Defined in header `<cfenv>` |  |
#define FE_DOWNWARD /*implementation defined*/ |  | (since C++11)
#define FE_TONEAREST /*implementation defined*/ |  | (since C++11)
#define FE_TOWARDZERO /*implementation defined*/ |  | (since C++11)
#define FE_UPWARD /*implementation defined*/ |  | (since C++11)

Each of these macro constants expands to a nonnegative integer constant expression, which can be used with std::fesetround and std::fegetround to indicate one of the supported floating-point rounding modes. The implementation may define additional rounding mode constants in `<cfenv>`, 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 std::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.

The current rounding mode affects the following:

- results of floating-point arithmetic operators outside of constant expressions

    double x = 1;
    x/10; // 0.09999999999999999167332731531132594682276248931884765625
       // or 0.1000000000000000055511151231257827021181583404541015625

- results of standard library mathematical functions

    std::sqrt(2); // 1.41421356237309492343001693370752036571502685546875
               // or 1.4142135623730951454746218587388284504413604736328125

- floating-point to floating-point implicit conversion and casts

    double d = 1 + std::numeric_limits<double>::epsilon();
    float f = d; //  1.00000000000000000000000
               // or 1.00000011920928955078125

- string conversions such as std::strtod or std::printf

    std::stof("0.1"); // 0.0999999940395355224609375
                   // or 0.100000001490116119384765625

- the library rounding functions std::nearbyint, std::rint, std::lrint

    std::lrint(2.1); // 2 or 3

The current rounding mode does NOT affect the following:

- floating-point to integer implicit conversion and casts (always towards zero)
- results of floating-point arithmetic operators in constant expressions (always to nearest)
- the library functions std::round, std::lround, std::ceil, std::floor, std::trunc

As with any floating-point environment functionality, rounding is only guaranteed if `#pragma STDC FENV_ACCESS ON` is set.

### [edit] Example

Run this code

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cfenv>
    #include <cmath>
    int main()
    {
    #pragma STDC FENV_ACCESS ON
        std::fesetround(FE_DOWNWARD);
        std::cout << "rounding down: \n" << std::setprecision(50)
                  << "         pi = " << std::acos(-1.f) << '\n'
                  << "stof(\"1.1\") = " << std::stof("1.1") << '\n'
                  << "  rint(2.1) = " << std::rint(2.3) << "\n\n";
        std::fesetround(FE_UPWARD);
        std::cout << "rounding up: \n"
                  << "         pi = " << std::acos(-1.f) << '\n'
                  << "stof(\"1.1\") = " << std::stof("1.1") << '\n'
                  << "  rint(2.1) = " << std::rint(2.3) << '\n';
    }

Output:

    rounding down:
             pi = 3.141592502593994140625
    stof("1.1") = 1.099999904632568359375
      rint(2.1) = 2

    rounding up:
             pi = 3.1415927410125732421875
    stof("1.1") = 1.10000002384185791015625
      rint(2.1) = 3

### [edit] See also

float_round_style | indicates floating-point rounding modes (enum) [edit]
fegetroundfesetround (C++11)(C++11) | gets or sets rounding direction (function) [edit]
C documentation for floating-point rounding macros

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/fenv/FE_round&oldid=72046"