@@ -218,6 +218,39 @@ the evaluation and get the result:
218218 xt::xarray<double> arr = some_init_function({3, 2, 4, 6, 5});
219219 double res = xt::reduce([](double a, double b) { return a*a + b*b; }, arr)();
220220
221+ The ``value_type `` of a reducer is the traditional result type of the reducing operation. For instance,
222+ the ``value_type `` of the reducer for the sum is:
223+
224+ - ``int `` if the underlying expression holds ``int `` values
225+ - ``int `` if the underlying expression holds ``short `` values, because ``short + short `` = ``int ``
226+
227+ You can pass a template argument to the reducer functions to specify the type of the initial value of
228+ the reduction. This allows you to "promote" the value type of the reducer and limit overflows in
229+ computation:
230+
231+ .. code ::
232+
233+ #include "xtensor/xarray.hpp"
234+ #include "xtensor/xreducer.hpp"
235+
236+ xt::xarray<int> arr = some_init_function({3, 2, 4, 6, 5});
237+ auto s1 = xt::sum<short>(arr); // No effect, short + int = int
238+ auto s2 = xt::sum<long int>(arr); // The value_type of s2 is long int
239+
240+ When you write generic code and you want to limit overflows, you can use ``xt::big_promote_value_type_t ``
241+ as shown below:
242+
243+ .. code ::
244+
245+ #include "xtensor/xarray.hpp"
246+ #include "xtensor/xreducer.hpp"
247+
248+ template <class E>
249+ void my_computation(E&& e)
250+ {
251+ auto s = xt::sum<xt::big_promote_value_type_t<E>>(e);
252+ }
253+
221254 Accumulators
222255------------
223256
0 commit comments