1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add sprout::sin, cos, tan

This commit is contained in:
bolero-MURAKAMI 2012-05-04 12:09:06 +09:00
parent cc329f20e6
commit 234757af07
10 changed files with 224 additions and 37 deletions

7
sprout/cmath.hpp Normal file
View file

@ -0,0 +1,7 @@
#ifndef SPROUT_CMATH_HPP
#define SPROUT_CMATH_HPP
#include <sprout/config.hpp>
#include <sprout/math/trigonometric.hpp>
#endif // #ifndef SPROUT_CMATH_HPP

View file

@ -52,6 +52,15 @@
// SPROUT_CONFIG_USE_SSCRISK_CEL // SPROUT_CONFIG_USE_SSCRISK_CEL
// //
//
// SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
//
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# ifndef SPROUT_HAS_CONSTEXPR_CMATH_FUNCTION
# define SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# endif // #ifdef SPROUT_HAS_CONSTEXPR_CMATH_FUNCTION
#endif // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
// //
// SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION // SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
// SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION // SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION

View file

@ -21,4 +21,8 @@
# define SPROUT_NO_DELEGATING_CONSTRUCTORS # define SPROUT_NO_DELEGATING_CONSTRUCTORS
#endif #endif
#if ((__GNUC__ >= 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__GXX_EXPERIMENTAL_CXX0X__))
# define SPROUT_HAS_CONSTEXPR_CMATH_FUNCTION
#endif
#endif // #ifndef SPROUT_CONFIG_COMPILER_GCC_HPP #endif // #ifndef SPROUT_CONFIG_COMPILER_GCC_HPP

View file

@ -55,6 +55,12 @@
# define NS_SSCRISK_CEL_OR_SPROUT sscrisk::cel # define NS_SSCRISK_CEL_OR_SPROUT sscrisk::cel
#endif // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL #endif // #ifndef SPROUT_CONFIG_USE_SSCRISK_CEL
#ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
# define SPROUT_USE_BUILTIN_CMATH_FUNCTION 0
#endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_CMATH_FUNCTION
#ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION
# define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1 # define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1
#else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION #else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION

64
sprout/math/cos.hpp Normal file
View file

@ -0,0 +1,64 @@
#ifndef SPROUT_MATH_COS_HPP
#define SPROUT_MATH_COS_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.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
cos_impl(T x, T tmp, std::size_t n, T x2n) {
return 2 * n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::cos_impl(
x,
tmp + (n % 2 ? -1 : 1) / sprout::math::factorial<T>(2 * n) * x2n,
n + 1,
x2n * x * x
)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
cos(FloatType x) {
typedef double type;
return static_cast<FloatType>(sprout::math::detail::cos_impl(
static_cast<type>(x),
type(1),
1,
static_cast<type>(x) * static_cast<type>(x)
));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
cos(IntType x) {
return sprout::math::detail::cos(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::cos;
# else
using sprout::math::detail::cos;
# endif
} // namespace math
using sprout::math::cos;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_COS_HPP

View file

@ -651,4 +651,3 @@ namespace sprout {
} // namespace sprout } // namespace sprout
#endif // #ifndef SPROUT_MATH_FACTORIAL_HPP #endif // #ifndef SPROUT_MATH_FACTORIAL_HPP

45
sprout/math/sin.hpp Normal file
View file

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_SIN_HPP
#define SPROUT_MATH_SIN_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/cos.hpp>
#include <sprout/math/constants.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
namespace sprout {
namespace math {
namespace detail {
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
sin(FloatType x) {
return -sprout::math::detail::cos(x + sprout::math::pi_div_two<FloatType>());
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
sin(IntType x) {
return sprout::math::detail::sin(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::sin;
# else
using sprout::math::detail::sin;
# endif
} // namespace math
using sprout::math::sin;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SIN_HPP

45
sprout/math/tan.hpp Normal file
View file

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_TAN_HPP
#define SPROUT_MATH_TAN_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/sin.hpp>
#include <sprout/math/cos.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
namespace sprout {
namespace math {
namespace detail {
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
tan(FloatType x) {
return sprout::math::detail::sin(x) / sprout::math::detail::cos(x);
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
tan(IntType x) {
return sprout::math::detail::tan(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::tan;
# else
using sprout::math::detail::tan;
# endif
} // namespace math
using sprout::math::tan;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_TAN_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_MATH_TRIGONOMETRIC_HPP
#define SPROUT_MATH_TRIGONOMETRIC_HPP
#include <sprout/math/sin.hpp>
#include <sprout/math/cos.hpp>
#include <sprout/math/tan.hpp>
#endif // #ifndef SPROUT_MATH_TRIGONOMETRIC_HPP