add sprout::atan

This commit is contained in:
bolero-MURAKAMI 2012-05-04 22:13:17 +09:00
parent 1417a8acd2
commit 8c23e809a4
6 changed files with 96 additions and 5 deletions

View file

@ -361,7 +361,7 @@ namespace sprout {
namespace detail { namespace detail {
template<typename T> template<typename T>
SPROUT_CONSTEXPR sprout::complex<T> acos_impl(sprout::complex<T> const& t) { SPROUT_CONSTEXPR sprout::complex<T> acos_impl(sprout::complex<T> const& t) {
return sprout::complex<T>(sprout::math::pi_div_two<T>() - t.real(), -t.imag()); return sprout::complex<T>(sprout::math::half_pi<T>() - t.real(), -t.imag());
} }
} // namespace detail } // namespace detail
template<typename T> template<typename T>

View file

@ -164,7 +164,7 @@ namespace sprout {
+ sprout::darkroom::coords::z(normal) * sprout::darkroom::coords::z(normal) + sprout::darkroom::coords::z(normal) * sprout::darkroom::coords::z(normal)
) )
) )
/ sprout::math::pi_div_two<unit_type>() / sprout::math::half_pi<unit_type>()
) )
); );
} }

83
sprout/math/atan.hpp Normal file
View file

@ -0,0 +1,83 @@
#ifndef SPROUT_MATH_ATAN_HPP
#define SPROUT_MATH_ATAN_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/constants.hpp>
#include <sprout/math/factorial.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
namespace sprout {
namespace math {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
atan_impl_2(T x, T tmp, std::size_t n, T x2n1) {
return n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::atan_impl_2(
x,
tmp + (n % 2 ? -1 : 1) * x2n1 / (2 * n + 1),
n + 1,
x2n1 * x * x
)
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
atan_impl_1(T x) {
return sprout::math::detail::atan_impl_2(
x,
x,
1,
x * x * x
);
}
template<typename T>
inline SPROUT_CONSTEXPR T
atan_impl(T x) {
return x > sprout::math::root_two<T>() + 1
? sprout::math::half_pi<T>() - sprout::math::detail::atan_impl_1(1 / x)
: x > sprout::math::root_two<T>() - 1
? sprout::math::quarter_pi<T>() + sprout::math::detail::atan_impl_1((x - 1) / (x + 1))
: sprout::math::detail::atan_impl_1(x)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
atan(FloatType x) {
typedef double type;
return static_cast<FloatType>(
x < 0 ? -sprout::math::detail::atan_impl(static_cast<type>(-x))
: sprout::math::detail::atan_impl(static_cast<type>(x))
);
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
atan(IntType x) {
return sprout::math::detail::atan(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::atan;
# else
using sprout::math::detail::atan;
# endif
} // namespace math
using sprout::math::atan;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ATAN_HPP

View file

@ -13,13 +13,20 @@ namespace sprout {
return 3.141592653589793238462643383279502884197169399375105820974944L; return 3.141592653589793238462643383279502884197169399375105820974944L;
} }
// //
// pi_div_two // half_pi
// //
template<typename T> template<typename T>
inline SPROUT_CONSTEXPR T pi_div_two() { inline SPROUT_CONSTEXPR T half_pi() {
return 1.570796326794896619231321691639751442098584699687552910487472L; return 1.570796326794896619231321691639751442098584699687552910487472L;
} }
// //
// quarter_pi
//
template<typename T>
inline SPROUT_CONSTEXPR T quarter_pi() {
return 0.785398163397448309615660845819875721049292349843776455243736L;
}
//
// root_two // root_two
// //
template<typename T> template<typename T>

View file

@ -19,7 +19,7 @@ namespace sprout {
> >
inline SPROUT_CONSTEXPR FloatType inline SPROUT_CONSTEXPR FloatType
sin(FloatType x) { sin(FloatType x) {
return -sprout::math::detail::cos(x + sprout::math::pi_div_two<FloatType>()); return -sprout::math::detail::cos(x + sprout::math::half_pi<FloatType>());
} }
template< template<

View file

@ -4,5 +4,6 @@
#include <sprout/math/sin.hpp> #include <sprout/math/sin.hpp>
#include <sprout/math/cos.hpp> #include <sprout/math/cos.hpp>
#include <sprout/math/tan.hpp> #include <sprout/math/tan.hpp>
#include <sprout/math/atan.hpp>
#endif // #ifndef SPROUT_MATH_TRIGONOMETRIC_HPP #endif // #ifndef SPROUT_MATH_TRIGONOMETRIC_HPP