First public contribution.
1 /* Boost interval/transc.hpp template implementation file
3 * Copyright 2000 Jens Maurer
4 * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
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 #ifndef BOOST_NUMERIC_INTERVAL_TRANSC_HPP
12 #define BOOST_NUMERIC_INTERVAL_TRANSC_HPP
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>
27 template<class T, class Policies> inline
28 interval<T, Policies> exp(const interval<T, Policies>& x)
30 typedef interval<T, Policies> I;
31 if (interval_lib::detail::test_input(x))
33 typename Policies::rounding rnd;
34 return I(rnd.exp_down(x.lower()), rnd.exp_up(x.upper()), true);
37 template<class T, class Policies> inline
38 interval<T, Policies> log(const interval<T, Policies>& x)
40 typedef interval<T, Policies> I;
41 if (interval_lib::detail::test_input(x) ||
42 !interval_lib::user::is_pos(x.upper()))
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);
51 template<class T, class Policies> inline
52 interval<T, Policies> cos(const interval<T, Policies>& x)
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;
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>());
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);
77 return I(static_cast<T>(-1), static_cast<T>(1), true);
80 template<class T, class Policies> inline
81 interval<T, Policies> sin(const interval<T, Policies>& x)
83 typedef interval<T, Policies> I;
84 if (interval_lib::detail::test_input(x))
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>());
93 template<class T, class Policies> inline
94 interval<T, Policies> tan(const interval<T, Policies>& x)
96 typedef interval<T, Policies> I;
97 if (interval_lib::detail::test_input(x))
99 typename Policies::rounding rnd;
100 typedef typename interval_lib::unprotect<I>::type R;
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)
108 if (tmp.lower() <= -pi_half_d || tmp.upper() >= pi_half_d)
110 return I(rnd.tan_down(tmp.lower()), rnd.tan_up(tmp.upper()), true);
113 template<class T, class Policies> inline
114 interval<T, Policies> asin(const interval<T, Policies>& x)
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))
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);
130 template<class T, class Policies> inline
131 interval<T, Policies> acos(const interval<T, Policies>& x)
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))
137 typename Policies::rounding rnd;
138 T l = (x.upper() >= static_cast<T>(1) )
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);
147 template<class T, class Policies> inline
148 interval<T, Policies> atan(const interval<T, Policies>& x)
150 typedef interval<T, Policies> I;
151 if (interval_lib::detail::test_input(x))
153 typename Policies::rounding rnd;
154 return I(rnd.atan_down(x.lower()), rnd.atan_up(x.upper()), true);
157 template<class T, class Policies> inline
158 interval<T, Policies> sinh(const interval<T, Policies>& x)
160 typedef interval<T, Policies> I;
161 if (interval_lib::detail::test_input(x))
163 typename Policies::rounding rnd;
164 return I(rnd.sinh_down(x.lower()), rnd.sinh_up(x.upper()), true);
167 template<class T, class Policies> inline
168 interval<T, Policies> cosh(const interval<T, Policies>& x)
170 typedef interval<T, Policies> I;
171 if (interval_lib::detail::test_input(x))
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);
179 return I(static_cast<T>(0), rnd.cosh_up(-x.lower() > x.upper() ? x.lower() : x.upper()), true);
182 template<class T, class Policies> inline
183 interval<T, Policies> tanh(const interval<T, Policies>& x)
185 typedef interval<T, Policies> I;
186 if (interval_lib::detail::test_input(x))
188 typename Policies::rounding rnd;
189 return I(rnd.tanh_down(x.lower()), rnd.tanh_up(x.upper()), true);
192 template<class T, class Policies> inline
193 interval<T, Policies> asinh(const interval<T, Policies>& x)
195 typedef interval<T, Policies> I;
196 if (interval_lib::detail::test_input(x))
198 typename Policies::rounding rnd;
199 return I(rnd.asinh_down(x.lower()), rnd.asinh_up(x.upper()), true);
202 template<class T, class Policies> inline
203 interval<T, Policies> acosh(const interval<T, Policies>& x)
205 typedef interval<T, Policies> I;
206 if (interval_lib::detail::test_input(x) || x.upper() < static_cast<T>(1))
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);
213 template<class T, class Policies> inline
214 interval<T, Policies> atanh(const interval<T, Policies>& x)
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))
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);
229 } // namespace numeric
232 #endif // BOOST_NUMERIC_INTERVAL_TRANSC_HPP