<cfenv>
support
<cfenv>
IEEE 754 defines 5 rounding modes for Decimal Floating Point Types.
-
Downward
-
To nearest (with ties to even)
-
To nearest from zero
-
Toward zero
-
Upward
The default rounding mode is to nearest with ties to even (#2) as specified in IEEE 754 Section 4.3.3 |
Using the following enum class
and functions you can change the rounding mode from the default at RUNTIME only if BOOST_DECIMAL_NO_CONSTEVAL_DETECTION
is not defined.
#include <boost/decimal/cfenv.hpp>
namespace boost {
namespace decimal {
enum class rounding_mode : unsigned
{
fe_dec_downward = 1 << 0,
fe_dec_to_nearest = 1 << 1,
fe_dec_to_nearest_from_zero = 1 << 2,
fe_dec_toward_zero = 1 << 3,
fe_dec_upward = 1 << 4,
fe_dec_default = fe_dec_to_nearest
};
rounding_mode fegetround() noexcept;
// Returns the rounding mode that has been set
//
// If your compiler defines BOOST_DECIMAL_NO_CONSTEVAL_DETECTION,
// this function will return the default rounding mode
// to alert you that the rounding mode has NOT changed
rounding_mode fesetround(rounding_mode round) noexcept;
} //namespace decimal
} //namespace boost
You can similarly change the default rounding mode at compile time with any compiler (unlike at runtime) using similarly named macros:
namespace boost {
namespace decimal {
static constexpr rounding_mode _boost_decimal_global_rounding_mode {
#if defined(BOOST_DECIMAL_FE_DEC_DOWNWARD)
rounding_mode::fe_dec_downward
#elif defined(BOOST_DECIMAL_FE_DEC_TO_NEAREST)
rounding_mode::fe_dec_to_nearest
#elif defined(BOOST_DECIMAL_FE_DEC_TO_NEAREST_FROM_ZERO)
rounding_mode::fe_dec_to_nearest_from_zero
#elif defined (BOOST_DECIMAL_FE_DEC_TOWARD_ZERO)
rounding_mde::fe_dec_toward_zero
#elif defined(BOOST_DECIMAL_FE_DEC_UPWARD)
rounding_mode::fe_dec_upward
#else
rounding_mode::fe_dec_default
#endif
};
} // namespace decimal
} // namespace boost
The default rounding mode for compile time is the same as the default run-time.
There is no requirement to explicitly define a compile-time nor run-time rounding mode |
Prior to v5.2.0 this header was <boost/decimal/fenv.hpp> , but has been changed to <boost/decimal/cfenv.hpp> for consistency with the STL.
|