@@ -103,42 +103,45 @@ Returned Value: 0.25
103103
104104[#examples_generic_programming]
105105== Generic Programming
106+
107+ This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/adl.cpp[adl.cpp].
108+
106109[source, c++]
107110----
111+ #include "test.hpp"
108112#include <boost/decimal.hpp>
109- #include <limits>
110113#include <cmath>
111114
112- int error_counter = 0;
113-
114115template <typename T>
115- bool float_equal (T lhs, T rhs )
116+ void sin_identity (T val )
116117{
117- using std::fabs;
118- return fabs(lhs - rhs) < std::numeric_limits<T>::epsilon(); // numeric_limits is overloaded for all decimal types
119- }
120-
121- template <typename T>
122- void test(T val)
123- {
124- using std::sin; // ADL allows builtin and decimal types to both be used
125- if (!float_equal(sin(val), -sin(-val))) // sin(x) == -sin(-x)
126- {
127- ++error_counter;
128- }
118+ // ADL allows builtin and decimal types to both be used
119+ // Boost.Decimal is not allowed to overload std::sin so it must be provided in its own namespace
120+ // You must also include using std::sin to ensure that it is found for the float, double, and long double cases.
121+ // It is preferred to have using statements for the functions you intend to use instead of using namespace XXX.
122+ using std::sin;
123+ using boost::decimal::sin;
124+
125+ // sin(x) = -sin(-x)
126+ // The call here MUST be unqualified, or you will get compiler errors
127+ // For example calling std::sin here would not allow any of the decimal types to be used
128+ BOOST_DECIMAL_TEST_EQ(sin(val), -sin(-val));
129129}
130130
131131int main()
132132{
133- test(-0.5F);
134- test(-0.5);
135- test(-0.5L);
133+ // Because of the two using statements in the above function we can now call it with built-in floating point,
134+ // or our decimal types as show below
135+
136+ sin_identity(-0.5F);
137+ sin_identity(-0.5);
138+ sin_identity(-0.5L);
136139
137- test (boost::decimal::decimal32_t{5, -1, true });
138- test (boost::decimal::decimal64_t{5, -1, true });
139- test (boost::decimal::decimal128_t{5, -1, true });
140+ sin_identity (boost::decimal::decimal32_t{"-0.5" });
141+ sin_identity (boost::decimal::decimal64_t{"-0.5" });
142+ sin_identity (boost::decimal::decimal128_t{"-0.5" });
140143
141- return error_counter ;
144+ return boost::decimal::test::report_errors() ;
142145}
143146----
144147
0 commit comments