Skip to content

Commit d98fcfb

Browse files
committed
Use inlined example and update the expected output
1 parent 3074148 commit d98fcfb

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

doc/modules/ROOT/pages/cfenv.adoc

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,55 +57,40 @@ rounding_mode fesetround(rounding_mode round) noexcept;
5757

5858
IMPORTANT: Much like `std::fesetround`, `boost::decimal::fesetround` is not thread safe.
5959

60-
Below is an example of the effects of changing the runtime rounding mode if your compiler supports it:
60+
[#examples_rounding_mode]
6161

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+
====
6264
[source, c++]
6365
----
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);
74-
75-
assert(default_rounding_mode != new_rounding_mode);
76-
77-
const auto lhs {"5e+50"_DF};
78-
const auto rhs {"4e+40"_DF};
79-
80-
// With upward rounding the result will be "5.000001e+50"_DF
81-
// Even though the difference in order of magnitude is greater than the precision of the type,
82-
// any addition in this mode will result in at least a one ULP difference
83-
const auto upward_res {lhs + rhs};
84-
assert(upward_res == "5.000001e+50"_DF);
85-
86-
boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_downward);
87-
88-
// 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[]
9467
----
9568
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+
9681
As shown, changing the rounding mode *WILL* change your numerical results.
9782
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.
9883
Before attempting to change the rounding mode ensure this is actually what you want to happen.
9984

10085
You can similarly change the default rounding mode at compile time with *ANY* compiler (unlike at runtime) using similarly named macros:
10186

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`
10792

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).
10994
At most *ONE* of these macros are allowed to be user defined.
11095
A `#error` will be emitted if more than one is detected.
11196
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:
117102

118103
2. You want to change the compile time rounding mode
119104

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.
106+
====
120107
[source, c++]
121108
----
122-
#define BOOST_DECIMAL_FE_DEC_DOWNWARD
123-
#include <boost/decimal.hpp>
124-
125-
int main()
126-
{
127-
using namespace boost::decimal::literals;
128-
129-
constexpr auto lhs {"5e+50"_DF};
130-
constexpr auto rhs {"4e+40"_DF};
131-
constexpr auto downward_res {lhs - rhs};
132-
static_assert(downward_res == "4.999999e+50"_DF, "Incorrectly rounded result");
133-
134-
return 0;
135-
}
109+
include::example$rounding_mode_compile_time.cpp[]
136110
----
137111
112+
.Expected Output
113+
....
114+
....
115+
====
116+
138117
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

Comments
 (0)