You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IMPORTANT: Much like `std::fesetround`, `boost::decimal::fesetround` is not thread safe.
59
59
60
-
Below is an example of the effects of changing the runtime rounding mode if your compiler supports it:
60
+
[#examples_rounding_mode]
61
61
62
+
.This https://github.com/cppalliance/decimal/blob/develop/examples/rounding_mode.cpp[example] demonstrates how to set, get, and effects of the global rounding mode.
63
+
====
62
64
[source, c++]
63
65
----
64
-
#include <boost/decimal.hpp>
65
-
#include <cassert>
66
-
67
-
int main()
68
-
{
69
-
using namespace boost::decimal::literals;
70
-
71
-
auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest
72
-
73
-
auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_upward);
// Similar to above in the downward rounding mode any subtraction will result in at least a one ULP difference
89
-
const auto downward_res {lhs - rhs};
90
-
assert(downward_res == "4.999999e+50"_DF);
91
-
92
-
return 0;
93
-
}
66
+
include::example$rounding_mode.cpp[]
94
67
----
95
68
69
+
.Expected Output:
70
+
....
71
+
The default rounding mode is: fe_dec_to_nearest
72
+
The current rounding mode is: fe_dec_upward
73
+
lhs equals: 5e+50
74
+
rhs equals: 4e+40
75
+
Sum with upward rounding: 5.000001e+50
76
+
The current rounding mode is: fe_dec_downward
77
+
Sum with downward rounding: 4.999999e+50
78
+
....
79
+
====
80
+
96
81
As shown, changing the rounding mode *WILL* change your numerical results.
97
82
If you are coming from the Intel library (or other C-style libs) where every mathematical function takes a rounding mode, that is not the case in this library; the only way to change the rounding mode for individual operations is via the global rounding mode.
98
83
Before attempting to change the rounding mode ensure this is actually what you want to happen.
99
84
100
85
You can similarly change the default rounding mode at compile time with *ANY* compiler (unlike at runtime) using similarly named macros:
101
86
102
-
1. BOOST_DECIMAL_FE_DEC_DOWNWARD
103
-
2. BOOST_DECIMAL_FE_DEC_TO_NEAREST
104
-
3. BOOST_DECIMAL_FE_DEC_TO_NEAREST_FROM_ZERO
105
-
4. BOOST_DECIMAL_FE_DEC_TOWARD_ZERO
106
-
5. BOOST_DECIMAL_FE_DEC_UPWARD
87
+
1. `BOOST_DECIMAL_FE_DEC_DOWNWARD`
88
+
2. `BOOST_DECIMAL_FE_DEC_TO_NEAREST`
89
+
3. `BOOST_DECIMAL_FE_DEC_TO_NEAREST_FROM_ZERO`
90
+
4. `BOOST_DECIMAL_FE_DEC_TOWARD_ZERO`
91
+
5. `BOOST_DECIMAL_FE_DEC_UPWARD`
107
92
108
-
If none of the above macros are defined, the default rounding mode for compile time is the same as the default runtime (i.e. as if BOOST_DECIMAL_FE_DEC_TO_NEAREST were defined).
93
+
If none of the above macros are defined, the default rounding mode for compile time is the same as the default runtime (i.e. as if `BOOST_DECIMAL_FE_DEC_TO_NEAREST` were defined).
109
94
At most *ONE* of these macros are allowed to be user defined.
110
95
A `#error` will be emitted if more than one is detected.
111
96
The macro must be defined before the inclusion of any decimal library header.
@@ -117,22 +102,16 @@ This same example can be reduced for the cases where:
117
102
118
103
2. You want to change the compile time rounding mode
119
104
105
+
.This https://github.com/cppalliance/decimal/blob/develop/examples/rounding_mode.cpp[example] demonstrates how the compile time rounding mode can be set, and it's effects on compile time and run time evaluation.
IMPORTANT: 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 naming convention.
0 commit comments