1 /* Boost interval/transc.hpp template implementation file
2  *
3  * Copyright 2000 Jens Maurer
4  * Copyright 2002 Herv� Br�nnimann, Guillaume Melquiond, Sylvain Pion
5  *
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)
9  */
10 
11 #ifndef BOOST_NUMERIC_INTERVAL_TRANSC_HPP
12 #define BOOST_NUMERIC_INTERVAL_TRANSC_HPP
13 
14 #include <boost/config.hpp>
15 #include <boost/numeric/interval/detail/interval_prototype.hpp>
16 #include <boost/numeric/interval/detail/bugs.hpp>
17 #include <boost/numeric/interval/detail/test_input.hpp>
18 #include <boost/numeric/interval/rounding.hpp>
19 #include <boost/numeric/interval/constants.hpp>
20 #include <boost/numeric/interval/arith.hpp>
21 #include <boost/numeric/interval/arith2.hpp>
22 #include <algorithm>
23 
24 namespace boost {
25 namespace numeric {
26 
27 template<class T, class Policies> inline
exp(const interval<T,Policies> & x)28 interval<T, Policies> exp(const interval<T, Policies>& x)
29 {
30   typedef interval<T, Policies> I;
31   if (interval_lib::detail::test_input(x))
32     return I::empty();
33   typename Policies::rounding rnd;
34   return I(rnd.exp_down(x.lower()), rnd.exp_up(x.upper()), true);
35 }
36 
37 template<class T, class Policies> inline
log(const interval<T,Policies> & x)38 interval<T, Policies> log(const interval<T, Policies>& x)
39 {
40   typedef interval<T, Policies> I;
41   if (interval_lib::detail::test_input(x) ||
42       !interval_lib::user::is_pos(x.upper()))
43     return I::empty();
44   typename Policies::rounding rnd;
45   typedef typename Policies::checking checking;
46   T l = !interval_lib::user::is_pos(x.lower())
47              ? checking::neg_inf() : rnd.log_down(x.lower());
48   return I(l, rnd.log_up(x.upper()), true);
49 }
50 
51 template<class T, class Policies> inline
cos(const interval<T,Policies> & x)52 interval<T, Policies> cos(const interval<T, Policies>& x)
53 {
54   if (interval_lib::detail::test_input(x))
55     return interval<T, Policies>::empty();
56   typename Policies::rounding rnd;
57   typedef interval<T, Policies> I;
58   typedef typename interval_lib::unprotect<I>::type R;
59 
60   // get lower bound within [0, pi]
61   const R pi2 = interval_lib::pi_twice<R>();
62   R tmp = fmod((const R&)x, pi2);
63   if (width(tmp) >= pi2.lower())
64     return I(static_cast<T>(-1), static_cast<T>(1), true); // we are covering a full period
65   if (tmp.lower() >= interval_lib::constants::pi_upper<T>())
66     return -cos(tmp - interval_lib::pi<R>());
67   T l = tmp.lower();
68   T u = tmp.upper();
69 
70   BOOST_USING_STD_MIN();
71   // separate into monotone subintervals
72   if (u <= interval_lib::constants::pi_lower<T>())
73     return I(rnd.cos_down(u), rnd.cos_up(l), true);
74   else if (u <= pi2.lower())
75     return I(static_cast<T>(-1), rnd.cos_up(min BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.sub_down(pi2.lower(), u), l)), true);
76   else
77     return I(static_cast<T>(-1), static_cast<T>(1), true);
78 }
79 
80 template<class T, class Policies> inline
sin(const interval<T,Policies> & x)81 interval<T, Policies> sin(const interval<T, Policies>& x)
82 {
83   typedef interval<T, Policies> I;
84   if (interval_lib::detail::test_input(x))
85     return I::empty();
86   typename Policies::rounding rnd;
87   typedef typename interval_lib::unprotect<I>::type R;
88   I r = cos((const R&)x - interval_lib::pi_half<R>());
89   (void)&rnd;
90   return r;
91 }
92 
93 template<class T, class Policies> inline
tan(const interval<T,Policies> & x)94 interval<T, Policies> tan(const interval<T, Policies>& x)
95 {
96   typedef interval<T, Policies> I;
97   if (interval_lib::detail::test_input(x))
98     return I::empty();
99   typename Policies::rounding rnd;
100   typedef typename interval_lib::unprotect<I>::type R;
101 
102   // get lower bound within [-pi/2, pi/2]
103   const R pi = interval_lib::pi<R>();
104   R tmp = fmod((const R&)x, pi);
105   const T pi_half_d = interval_lib::constants::pi_half_lower<T>();
106   if (tmp.lower() >= pi_half_d)
107     tmp -= pi;
108   if (tmp.lower() <= -pi_half_d || tmp.upper() >= pi_half_d)
109     return I::whole();
110   return I(rnd.tan_down(tmp.lower()), rnd.tan_up(tmp.upper()), true);
111 }
112 
113 template<class T, class Policies> inline
asin(const interval<T,Policies> & x)114 interval<T, Policies> asin(const interval<T, Policies>& x)
115 {
116   typedef interval<T, Policies> I;
117   if (interval_lib::detail::test_input(x)
118      || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
119     return I::empty();
120   typename Policies::rounding rnd;
121   T l = (x.lower() <= static_cast<T>(-1))
122              ? -interval_lib::constants::pi_half_upper<T>()
123              : rnd.asin_down(x.lower());
124   T u = (x.upper() >= static_cast<T>(1) )
125              ?  interval_lib::constants::pi_half_upper<T>()
126              : rnd.asin_up  (x.upper());
127   return I(l, u, true);
128 }
129 
130 template<class T, class Policies> inline
acos(const interval<T,Policies> & x)131 interval<T, Policies> acos(const interval<T, Policies>& x)
132 {
133   typedef interval<T, Policies> I;
134   if (interval_lib::detail::test_input(x)
135      || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
136     return I::empty();
137   typename Policies::rounding rnd;
138   T l = (x.upper() >= static_cast<T>(1) )
139           ? static_cast<T>(0)
140           : rnd.acos_down(x.upper());
141   T u = (x.lower() <= static_cast<T>(-1))
142           ? interval_lib::constants::pi_upper<T>()
143           : rnd.acos_up  (x.lower());
144   return I(l, u, true);
145 }
146 
147 template<class T, class Policies> inline
atan(const interval<T,Policies> & x)148 interval<T, Policies> atan(const interval<T, Policies>& x)
149 {
150   typedef interval<T, Policies> I;
151   if (interval_lib::detail::test_input(x))
152     return I::empty();
153   typename Policies::rounding rnd;
154   return I(rnd.atan_down(x.lower()), rnd.atan_up(x.upper()), true);
155 }
156 
157 template<class T, class Policies> inline
sinh(const interval<T,Policies> & x)158 interval<T, Policies> sinh(const interval<T, Policies>& x)
159 {
160   typedef interval<T, Policies> I;
161   if (interval_lib::detail::test_input(x))
162     return I::empty();
163   typename Policies::rounding rnd;
164   return I(rnd.sinh_down(x.lower()), rnd.sinh_up(x.upper()), true);
165 }
166 
167 template<class T, class Policies> inline
cosh(const interval<T,Policies> & x)168 interval<T, Policies> cosh(const interval<T, Policies>& x)
169 {
170   typedef interval<T, Policies> I;
171   if (interval_lib::detail::test_input(x))
172     return I::empty();
173   typename Policies::rounding rnd;
174   if (interval_lib::user::is_neg(x.upper()))
175     return I(rnd.cosh_down(x.upper()), rnd.cosh_up(x.lower()), true);
176   else if (!interval_lib::user::is_neg(x.lower()))
177     return I(rnd.cosh_down(x.lower()), rnd.cosh_up(x.upper()), true);
178   else
179     return I(static_cast<T>(0), rnd.cosh_up(-x.lower() > x.upper() ? x.lower() : x.upper()), true);
180 }
181 
182 template<class T, class Policies> inline
tanh(const interval<T,Policies> & x)183 interval<T, Policies> tanh(const interval<T, Policies>& x)
184 {
185   typedef interval<T, Policies> I;
186   if (interval_lib::detail::test_input(x))
187     return I::empty();
188   typename Policies::rounding rnd;
189   return I(rnd.tanh_down(x.lower()), rnd.tanh_up(x.upper()), true);
190 }
191 
192 template<class T, class Policies> inline
asinh(const interval<T,Policies> & x)193 interval<T, Policies> asinh(const interval<T, Policies>& x)
194 {
195   typedef interval<T, Policies> I;
196   if (interval_lib::detail::test_input(x))
197     return I::empty();
198   typename Policies::rounding rnd;
199   return I(rnd.asinh_down(x.lower()), rnd.asinh_up(x.upper()), true);
200 }
201 
202 template<class T, class Policies> inline
acosh(const interval<T,Policies> & x)203 interval<T, Policies> acosh(const interval<T, Policies>& x)
204 {
205   typedef interval<T, Policies> I;
206   if (interval_lib::detail::test_input(x) || x.upper() < static_cast<T>(1))
207     return I::empty();
208   typename Policies::rounding rnd;
209   T l = x.lower() <= static_cast<T>(1) ? static_cast<T>(0) : rnd.acosh_down(x.lower());
210   return I(l, rnd.acosh_up(x.upper()), true);
211 }
212 
213 template<class T, class Policies> inline
atanh(const interval<T,Policies> & x)214 interval<T, Policies> atanh(const interval<T, Policies>& x)
215 {
216   typedef interval<T, Policies> I;
217   if (interval_lib::detail::test_input(x)
218       || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
219     return I::empty();
220   typename Policies::rounding rnd;
221   typedef typename Policies::checking checking;
222   T l = (x.lower() <= static_cast<T>(-1))
223              ? checking::neg_inf() : rnd.atanh_down(x.lower());
224   T u = (x.upper() >= static_cast<T>(1) )
225              ? checking::pos_inf() : rnd.atanh_up  (x.upper());
226   return I(l, u, true);
227 }
228 
229 } // namespace numeric
230 } // namespace boost
231 
232 #endif // BOOST_NUMERIC_INTERVAL_TRANSC_HPP
233