sl@0: /* Boost interval/transc.hpp template implementation file sl@0: * sl@0: * Copyright 2000 Jens Maurer sl@0: * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion sl@0: * sl@0: * Distributed under the Boost Software License, Version 1.0. sl@0: * (See accompanying file LICENSE_1_0.txt or sl@0: * copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: */ sl@0: sl@0: #ifndef BOOST_NUMERIC_INTERVAL_TRANSC_HPP sl@0: #define BOOST_NUMERIC_INTERVAL_TRANSC_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: namespace numeric { sl@0: sl@0: template inline sl@0: interval exp(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: return I(rnd.exp_down(x.lower()), rnd.exp_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval log(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x) || sl@0: !interval_lib::user::is_pos(x.upper())) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: typedef typename Policies::checking checking; sl@0: T l = !interval_lib::user::is_pos(x.lower()) sl@0: ? checking::neg_inf() : rnd.log_down(x.lower()); sl@0: return I(l, rnd.log_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval cos(const interval& x) sl@0: { sl@0: if (interval_lib::detail::test_input(x)) sl@0: return interval::empty(); sl@0: typename Policies::rounding rnd; sl@0: typedef interval I; sl@0: typedef typename interval_lib::unprotect::type R; sl@0: sl@0: // get lower bound within [0, pi] sl@0: const R pi2 = interval_lib::pi_twice(); sl@0: R tmp = fmod((const R&)x, pi2); sl@0: if (width(tmp) >= pi2.lower()) sl@0: return I(static_cast(-1), static_cast(1), true); // we are covering a full period sl@0: if (tmp.lower() >= interval_lib::constants::pi_upper()) sl@0: return -cos(tmp - interval_lib::pi()); sl@0: T l = tmp.lower(); sl@0: T u = tmp.upper(); sl@0: sl@0: BOOST_USING_STD_MIN(); sl@0: // separate into monotone subintervals sl@0: if (u <= interval_lib::constants::pi_lower()) sl@0: return I(rnd.cos_down(u), rnd.cos_up(l), true); sl@0: else if (u <= pi2.lower()) sl@0: return I(static_cast(-1), rnd.cos_up(min BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.sub_down(pi2.lower(), u), l)), true); sl@0: else sl@0: return I(static_cast(-1), static_cast(1), true); sl@0: } sl@0: sl@0: template inline sl@0: interval sin(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: typedef typename interval_lib::unprotect::type R; sl@0: I r = cos((const R&)x - interval_lib::pi_half()); sl@0: (void)&rnd; sl@0: return r; sl@0: } sl@0: sl@0: template inline sl@0: interval tan(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: typedef typename interval_lib::unprotect::type R; sl@0: sl@0: // get lower bound within [-pi/2, pi/2] sl@0: const R pi = interval_lib::pi(); sl@0: R tmp = fmod((const R&)x, pi); sl@0: const T pi_half_d = interval_lib::constants::pi_half_lower(); sl@0: if (tmp.lower() >= pi_half_d) sl@0: tmp -= pi; sl@0: if (tmp.lower() <= -pi_half_d || tmp.upper() >= pi_half_d) sl@0: return I::whole(); sl@0: return I(rnd.tan_down(tmp.lower()), rnd.tan_up(tmp.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval asin(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x) sl@0: || x.upper() < static_cast(-1) || x.lower() > static_cast(1)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: T l = (x.lower() <= static_cast(-1)) sl@0: ? -interval_lib::constants::pi_half_upper() sl@0: : rnd.asin_down(x.lower()); sl@0: T u = (x.upper() >= static_cast(1) ) sl@0: ? interval_lib::constants::pi_half_upper() sl@0: : rnd.asin_up (x.upper()); sl@0: return I(l, u, true); sl@0: } sl@0: sl@0: template inline sl@0: interval acos(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x) sl@0: || x.upper() < static_cast(-1) || x.lower() > static_cast(1)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: T l = (x.upper() >= static_cast(1) ) sl@0: ? static_cast(0) sl@0: : rnd.acos_down(x.upper()); sl@0: T u = (x.lower() <= static_cast(-1)) sl@0: ? interval_lib::constants::pi_upper() sl@0: : rnd.acos_up (x.lower()); sl@0: return I(l, u, true); sl@0: } sl@0: sl@0: template inline sl@0: interval atan(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: return I(rnd.atan_down(x.lower()), rnd.atan_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval sinh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: return I(rnd.sinh_down(x.lower()), rnd.sinh_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval cosh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: if (interval_lib::user::is_neg(x.upper())) sl@0: return I(rnd.cosh_down(x.upper()), rnd.cosh_up(x.lower()), true); sl@0: else if (!interval_lib::user::is_neg(x.lower())) sl@0: return I(rnd.cosh_down(x.lower()), rnd.cosh_up(x.upper()), true); sl@0: else sl@0: return I(static_cast(0), rnd.cosh_up(-x.lower() > x.upper() ? x.lower() : x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval tanh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: return I(rnd.tanh_down(x.lower()), rnd.tanh_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval asinh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: return I(rnd.asinh_down(x.lower()), rnd.asinh_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval acosh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x) || x.upper() < static_cast(1)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: T l = x.lower() <= static_cast(1) ? static_cast(0) : rnd.acosh_down(x.lower()); sl@0: return I(l, rnd.acosh_up(x.upper()), true); sl@0: } sl@0: sl@0: template inline sl@0: interval atanh(const interval& x) sl@0: { sl@0: typedef interval I; sl@0: if (interval_lib::detail::test_input(x) sl@0: || x.upper() < static_cast(-1) || x.lower() > static_cast(1)) sl@0: return I::empty(); sl@0: typename Policies::rounding rnd; sl@0: typedef typename Policies::checking checking; sl@0: T l = (x.lower() <= static_cast(-1)) sl@0: ? checking::neg_inf() : rnd.atanh_down(x.lower()); sl@0: T u = (x.upper() >= static_cast(1) ) sl@0: ? checking::pos_inf() : rnd.atanh_up (x.upper()); sl@0: return I(l, u, true); sl@0: } sl@0: sl@0: } // namespace numeric sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_NUMERIC_INTERVAL_TRANSC_HPP