From 234757af0728ddb7c180f41f35f3be564f19baba Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Fri, 4 May 2012 12:09:06 +0900 Subject: [PATCH] add sprout::sin, cos, tan --- libs/weed/example/remove_space.cpp | 72 +++++++++++++++--------------- sprout/cmath.hpp | 7 +++ sprout/config/auto_config.hpp | 9 ++++ sprout/config/compiler/gcc.hpp | 4 ++ sprout/config/config.hpp | 6 +++ sprout/math/cos.hpp | 64 ++++++++++++++++++++++++++ sprout/math/factorial.hpp | 1 - sprout/math/sin.hpp | 45 +++++++++++++++++++ sprout/math/tan.hpp | 45 +++++++++++++++++++ sprout/math/trigonometric.hpp | 8 ++++ 10 files changed, 224 insertions(+), 37 deletions(-) create mode 100644 sprout/cmath.hpp create mode 100644 sprout/math/cos.hpp create mode 100644 sprout/math/sin.hpp create mode 100644 sprout/math/tan.hpp create mode 100644 sprout/math/trigonometric.hpp diff --git a/libs/weed/example/remove_space.cpp b/libs/weed/example/remove_space.cpp index 23600f52..e155670b 100644 --- a/libs/weed/example/remove_space.cpp +++ b/libs/weed/example/remove_space.cpp @@ -1,36 +1,36 @@ -// -// Sprout C++ Library -// -// Copyright (c) 2012 -// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ -// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ -// -// Readme: -// https://github.com/osyo-manga/cpp-half/blob/master/README -// -// License: -// Boost Software License - Version 1.0 -// -// -#include - - -int -main(){ - namespace w = sprout::weed; - - static constexpr auto max_string_size = 32; - static constexpr auto space = *w::omit[ w::space ]; - static constexpr auto remove_space = *w::lim(space >> w::char_); - - static constexpr auto source = sprout::to_string(" homu : mami= 10 "); - static constexpr auto result = w::parse( - sprout::begin(source), sprout::end(source), - remove_space - ); - - static_assert(result.success(), "fail remove_space parse"); - static_assert(result.attr() == "homu:mami=10", ""); - - return 0; -} +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/osyo-manga/cpp-half/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + + +int +main(){ + namespace w = sprout::weed; + + static constexpr auto max_string_size = 32; + static constexpr auto space = *w::omit[ w::space ]; + static constexpr auto remove_space = *w::lim(space >> w::char_); + + static constexpr auto source = sprout::to_string(" homu : mami= 10 "); + static constexpr auto result = w::parse( + sprout::begin(source), sprout::end(source), + remove_space + ); + + static_assert(result.success(), "fail remove_space parse"); + static_assert(result.attr() == "homu:mami=10", ""); + + return 0; +} diff --git a/sprout/cmath.hpp b/sprout/cmath.hpp new file mode 100644 index 00000000..1568cb14 --- /dev/null +++ b/sprout/cmath.hpp @@ -0,0 +1,7 @@ +#ifndef SPROUT_CMATH_HPP +#define SPROUT_CMATH_HPP + +#include +#include + +#endif // #ifndef SPROUT_CMATH_HPP diff --git a/sprout/config/auto_config.hpp b/sprout/config/auto_config.hpp index b694fe93..2728c9dc 100644 --- a/sprout/config/auto_config.hpp +++ b/sprout/config/auto_config.hpp @@ -52,6 +52,15 @@ // 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_SUPPORT_TEMPORARY_CONTAINER_ITERATION diff --git a/sprout/config/compiler/gcc.hpp b/sprout/config/compiler/gcc.hpp index 701757eb..6df80508 100644 --- a/sprout/config/compiler/gcc.hpp +++ b/sprout/config/compiler/gcc.hpp @@ -21,4 +21,8 @@ # define SPROUT_NO_DELEGATING_CONSTRUCTORS #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 diff --git a/sprout/config/config.hpp b/sprout/config/config.hpp index 889ba4ea..7afacb47 100644 --- a/sprout/config/config.hpp +++ b/sprout/config/config.hpp @@ -55,6 +55,12 @@ # define NS_SSCRISK_CEL_OR_SPROUT 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 # define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1 #else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION diff --git a/sprout/math/cos.hpp b/sprout/math/cos.hpp new file mode 100644 index 00000000..3b0443b8 --- /dev/null +++ b/sprout/math/cos.hpp @@ -0,0 +1,64 @@ +#ifndef SPROUT_MATH_COS_HPP +#define SPROUT_MATH_COS_HPP + +#include +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR T + cos_impl(T x, T tmp, std::size_t n, T x2n) { + return 2 * n > sprout::math::factorial_limit() ? tmp + : sprout::math::detail::cos_impl( + x, + tmp + (n % 2 ? -1 : 1) / sprout::math::factorial(2 * n) * x2n, + n + 1, + x2n * x * x + ) + ; + } + + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + cos(FloatType x) { + typedef double type; + return static_cast(sprout::math::detail::cos_impl( + static_cast(x), + type(1), + 1, + static_cast(x) * static_cast(x) + )); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + cos(IntType x) { + return sprout::math::detail::cos(static_cast(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 diff --git a/sprout/math/factorial.hpp b/sprout/math/factorial.hpp index ed2239e0..109aed89 100644 --- a/sprout/math/factorial.hpp +++ b/sprout/math/factorial.hpp @@ -651,4 +651,3 @@ namespace sprout { } // namespace sprout #endif // #ifndef SPROUT_MATH_FACTORIAL_HPP - diff --git a/sprout/math/sin.hpp b/sprout/math/sin.hpp new file mode 100644 index 00000000..2c13ce44 --- /dev/null +++ b/sprout/math/sin.hpp @@ -0,0 +1,45 @@ +#ifndef SPROUT_MATH_SIN_HPP +#define SPROUT_MATH_SIN_HPP + +#include +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + sin(FloatType x) { + return -sprout::math::detail::cos(x + sprout::math::pi_div_two()); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + sin(IntType x) { + return sprout::math::detail::sin(static_cast(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 diff --git a/sprout/math/tan.hpp b/sprout/math/tan.hpp new file mode 100644 index 00000000..e6593917 --- /dev/null +++ b/sprout/math/tan.hpp @@ -0,0 +1,45 @@ +#ifndef SPROUT_MATH_TAN_HPP +#define SPROUT_MATH_TAN_HPP + +#include +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::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::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + tan(IntType x) { + return sprout::math::detail::tan(static_cast(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 diff --git a/sprout/math/trigonometric.hpp b/sprout/math/trigonometric.hpp new file mode 100644 index 00000000..78e0130c --- /dev/null +++ b/sprout/math/trigonometric.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_MATH_TRIGONOMETRIC_HPP +#define SPROUT_MATH_TRIGONOMETRIC_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_MATH_TRIGONOMETRIC_HPP