mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add sprout::sin, cos, tan
This commit is contained in:
parent
cc329f20e6
commit
234757af07
10 changed files with 224 additions and 37 deletions
|
@ -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
|
||||
// <http://www.boost.org/LICENSE_1_0.txt>
|
||||
//
|
||||
#include <sprout/weed.hpp>
|
||||
|
||||
|
||||
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<max_string_size>(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
|
||||
// <http://www.boost.org/LICENSE_1_0.txt>
|
||||
//
|
||||
#include <sprout/weed.hpp>
|
||||
|
||||
|
||||
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<max_string_size>(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;
|
||||
}
|
||||
|
|
7
sprout/cmath.hpp
Normal file
7
sprout/cmath.hpp
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
64
sprout/math/cos.hpp
Normal file
64
sprout/math/cos.hpp
Normal 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
|
|
@ -651,4 +651,3 @@ namespace sprout {
|
|||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_FACTORIAL_HPP
|
||||
|
||||
|
|
45
sprout/math/sin.hpp
Normal file
45
sprout/math/sin.hpp
Normal 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
45
sprout/math/tan.hpp
Normal 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
|
8
sprout/math/trigonometric.hpp
Normal file
8
sprout/math/trigonometric.hpp
Normal 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
|
Loading…
Reference in a new issue