1 /* Boost example/horner.cpp
2 * example of unprotecting rounding for a whole function computation
4 * Copyright 2002-2003 Guillaume Melquiond
6 * Distributed under the Boost Software License, Version 1.0.
7 * (See accompanying file LICENSE_1_0.txt or
8 * copy at http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/numeric/interval.hpp>
14 // I is an interval class, the polynom is a simple array
16 I
horner(const I
& x
, const I p
[], int n
) {
18 // initialize and restore the rounding mode
19 typename
I::traits_type::rounding rnd
;
21 // define the unprotected version of the interval type
22 typedef typename
boost::numeric::interval_lib::unprotect
<I
>::type R
;
26 for(int i
= n
- 2; i
>= 0; i
--) {
27 y
= y
* a
+ (const R
&)(p
[i
]);
31 // restore the rounding mode with the destruction of rnd
34 template<class T
, class Policies
>
35 std::ostream
&operator<<(std::ostream
&os
,
36 const boost::numeric::interval
<T
, Policies
> &x
) {
37 os
<< "[" << x
.lower() << ", " << x
.upper() << "]";
42 typedef boost::numeric::interval
<double> I
;
43 I p
[3] = { -1.0, 0, 1.0 };
45 std::cout
<< horner(x
, p
, 3) << std::endl
;