add math/classifications.hpp

This commit is contained in:
bolero-MURAKAMI 2012-05-10 13:34:49 +09:00
parent 6bb2d0fb87
commit 64b422546e
35 changed files with 335 additions and 31 deletions

View file

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

View file

@ -22,7 +22,7 @@ namespace sprout {
acosh(FloatType x) {
return x < 1 ? std::numeric_limits<FloatType>::quiet_NaN()
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
: sprout::math::detail::log(x + sprout::math::detail::sqrt(x * x - 1))
: sprout::math::log(x + sprout::math::sqrt(x * x - 1))
;
}

View file

@ -47,7 +47,7 @@ namespace sprout {
inline SPROUT_CONSTEXPR T
asin_impl(T x) {
return x > sprout::math::half_root_two<T>()
? sprout::math::half_pi<T>() - sprout::math::detail::asin_impl_1(sprout::math::detail::sqrt(1 - x * x))
? sprout::math::half_pi<T>() - sprout::math::detail::asin_impl_1(sprout::math::sqrt(1 - x * x))
: sprout::math::detail::asin_impl_1(x)
;
}

View file

@ -22,7 +22,7 @@ namespace sprout {
asinh(FloatType x) {
return x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
: sprout::math::detail::log(x + sprout::math::detail::sqrt(x * x + 1))
: sprout::math::log(x + sprout::math::sqrt(x * x + 1))
;
}

View file

@ -21,8 +21,8 @@ namespace sprout {
inline SPROUT_CONSTEXPR FloatType
atan2(FloatType y, FloatType x) {
return x < 0
? sprout::math::detail::atan(y / x) + (y < 0 ? -1 : 1) * sprout::math::pi<FloatType>()
: sprout::math::detail::atan(y / x)
? sprout::math::atan(y / x) + (y < 0 ? -1 : 1) * sprout::math::pi<FloatType>()
: sprout::math::atan(y / x)
;
}

View file

@ -22,7 +22,7 @@ namespace sprout {
return x < -1 || x > 1 ? std::numeric_limits<FloatType>::quiet_NaN()
: x == -1 ? -std::numeric_limits<FloatType>::infinity()
: x == 1 ? std::numeric_limits<FloatType>::infinity()
: sprout::math::detail::log((1 + x) / (1 - x)) / 2
: sprout::math::log((1 + x) / (1 - x)) / 2
;
}

View file

@ -19,8 +19,8 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
cbrt(FloatType x) {
return x < 0 ? -sprout::math::detail::pow(-x, sprout::math::third<FloatType>())
: sprout::math::detail::pow(x, sprout::math::third<FloatType>())
return x < 0 ? -sprout::math::pow(-x, sprout::math::third<FloatType>())
: sprout::math::pow(x, sprout::math::third<FloatType>())
;
}

View file

@ -0,0 +1,14 @@
#ifndef SPROUT_MATH_CLASSIFICATIONS_HPP
#define SPROUT_MATH_CLASSIFICATIONS_HPP
#include <sprout/config.hpp>
#include <sprout/math/fpclassify.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/iszero.hpp>
#include <sprout/math/issubnormal.hpp>
#include <sprout/math/isnormal.hpp>
#include <sprout/math/isfinite.hpp>
#include <sprout/math/signbit.hpp>
#endif // #ifndef SPROUT_MATH_CLASSIFICATIONS_HPP

View file

@ -16,7 +16,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
exp10(FloatType x) {
return sprout::math::detail::exp(x * sprout::math::ln_ten<FloatType>());
return sprout::math::exp(x * sprout::math::ln_ten<FloatType>());
}
template<

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
exp2(FloatType x) {
return sprout::math::detail::exp(x * sprout::math::ln_two<FloatType>());
return sprout::math::exp(x * sprout::math::ln_two<FloatType>());
}
template<

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
expm1(FloatType x) {
return sprout::math::detail::exp(x) - 1;
return sprout::math::exp(x) - 1;
}
template<

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_EXPONENTIAL_HPP
#define SPROUT_MATH_EXPONENTIAL_HPP
#include <sprout/config.hpp>
#include <sprout/math/exp.hpp>
#include <sprout/math/exp10.hpp>
#include <sprout/math/exp2.hpp>

View file

@ -0,0 +1,41 @@
#ifndef SPROUT_MATH_FPCLASSIFY_HPP
#define SPROUT_MATH_FPCLASSIFY_HPP
#include <cmath>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/iszero.hpp>
#include <sprout/math/issubnormal.hpp>
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 int
fpclassify(FloatType x) {
return sprout::math::isnan(x) ? FP_NAN
: sprout::math::isinf(x) ? FP_INFINITE
: sprout::math::iszero(x) ? FP_ZERO
: sprout::math::detail::issubnormal_or_zero(x) ? FP_SUBNORMAL
: FP_NORMAL
;
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::fpclassify;
# else
using sprout::math::detail::fpclassify;
# endif
} // namespace math
using sprout::math::fpclassify;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_FPCLASSIFY_HPP

View file

@ -7,5 +7,6 @@
#include <sprout/math/power.hpp>
#include <sprout/math/trigonometric.hpp>
#include <sprout/math/hyperbolic.hpp>
#include <sprout/math/classifications.hpp>
#endif // #ifndef SPROUT_MATH_FUNCTIONS_HPP

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_HYPERBOLIC_HPP
#define SPROUT_MATH_HYPERBOLIC_HPP
#include <sprout/config.hpp>
#include <sprout/math/sinh.hpp>
#include <sprout/math/cosh.hpp>
#include <sprout/math/tanh.hpp>

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
hypot(FloatType x, FloatType y) {
return sprout::math::detail::sqrt(x * x + y * y);
return sprout::math::sqrt(x * x + y * y);
}
template<

38
sprout/math/isfinite.hpp Normal file
View file

@ -0,0 +1,38 @@
#ifndef SPROUT_MATH_ISFINITE_HPP
#define SPROUT_MATH_ISFINITE_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.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 int
isfinite(FloatType x) {
return !sprout::math::isnan(x)
&& !sprout::math::isinf(x)
;
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::isfinite;
# else
using sprout::math::detail::isfinite;
# endif
} // namespace math
using sprout::math::isfinite;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISFINITE_HPP

37
sprout/math/isinf.hpp Normal file
View file

@ -0,0 +1,37 @@
#ifndef SPROUT_MATH_ISINF_HPP
#define SPROUT_MATH_ISINF_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.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 int
isinf(FloatType x) {
return x == std::numeric_limits<FloatType>::infinity()
|| x == -std::numeric_limits<FloatType>::infinity()
;
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::isinf;
# else
using sprout::math::detail::isinf;
# endif
} // namespace math
using sprout::math::isinf;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISINF_HPP

34
sprout/math/isnan.hpp Normal file
View file

@ -0,0 +1,34 @@
#ifndef SPROUT_MATH_ISNAN_HPP
#define SPROUT_MATH_ISNAN_HPP
#include <type_traits>
#include <sprout/config.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 int
isnan(FloatType x) {
return !(x == x);
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::isnan;
# else
using sprout::math::detail::isnan;
# endif
} // namespace math
using sprout::math::isnan;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISNAN_HPP

40
sprout/math/isnormal.hpp Normal file
View file

@ -0,0 +1,40 @@
#ifndef SPROUT_MATH_ISNORMAL_HPP
#define SPROUT_MATH_ISNORMAL_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/issubnormal.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 int
isnormal(FloatType x) {
return !sprout::math::isnan(x)
&& !sprout::math::isinf(x)
&& !sprout::math::detail::issubnormal_or_zero(x)
;
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::isnormal;
# else
using sprout::math::detail::isnormal;
# endif
} // namespace math
using sprout::math::isnormal;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISNORMAL_HPP

View file

@ -0,0 +1,43 @@
#ifndef SPROUT_MATH_ISSUBNORMAL_HPP
#define SPROUT_MATH_ISSUBNORMAL_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/math/iszero.hpp>
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 int
issubnormal_or_zero(FloatType x) {
return x > 0
? x < std::numeric_limits<double>::min()
: x > -std::numeric_limits<double>::min()
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR int
issubnormal(FloatType x) {
return !sprout::math::iszero(x)
&& sprout::math::detail::issubnormal_or_zero(x)
;
}
} // namespace detail
using sprout::math::detail::issubnormal;
} // namespace math
using sprout::math::issubnormal;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISSUBNORMAL_HPP

27
sprout/math/iszero.hpp Normal file
View file

@ -0,0 +1,27 @@
#ifndef SPROUT_MATH_ISZERO_HPP
#define SPROUT_MATH_ISZERO_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/enabler_if.hpp>
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 int
iszero(FloatType x) {
return x == 0;
}
} // namespace detail
using sprout::math::detail::iszero;
} // namespace math
using sprout::math::iszero;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ISZERO_HPP

View file

@ -42,7 +42,7 @@ namespace sprout {
inline SPROUT_CONSTEXPR T
log_impl(T x) {
return !(x > sprout::math::root_two<T>()) ? sprout::math::detail::log_impl_1(x - 1)
: 2 * sprout::math::detail::log_impl(sprout::math::detail::sqrt(x))
: 2 * sprout::math::detail::log_impl(sprout::math::sqrt(x))
;
}

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
log10(FloatType x) {
return sprout::math::detail::log(x) / sprout::math::ln_ten<FloatType>();
return sprout::math::log(x) / sprout::math::ln_ten<FloatType>();
}
template<

View file

@ -18,7 +18,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
log1p(FloatType x) {
return sprout::math::detail::log(1 + x);
return sprout::math::log(1 + x);
}
template<

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
log2(FloatType x) {
return sprout::math::detail::log(x) / sprout::math::ln_two<FloatType>();
return sprout::math::log(x) / sprout::math::ln_two<FloatType>();
}
template<

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_OPERATIONS_HPP
#define SPROUT_MATH_OPERATIONS_HPP
#include <sprout/config.hpp>
#include <sprout/math/abs.hpp>
#include <sprout/math/fabs.hpp>
#include <sprout/math/fma.hpp>

View file

@ -22,7 +22,7 @@ namespace sprout {
inline SPROUT_CONSTEXPR FloatType
pow(FloatType x, FloatType y) {
return x == 0 && y > 0 ? FloatType(0)
: sprout::math::detail::exp(y * sprout::math::detail::log(x))
: sprout::math::exp(y * sprout::math::log(x))
;
}

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_POWER_HPP
#define SPROUT_MATH_POWER_HPP
#include <sprout/config.hpp>
#include <sprout/math/sqrt.hpp>
#include <sprout/math/cbrt.hpp>
#include <sprout/math/pow.hpp>

34
sprout/math/signbit.hpp Normal file
View file

@ -0,0 +1,34 @@
#ifndef SPROUT_MATH_SIGNBIT_HPP
#define SPROUT_MATH_SIGNBIT_HPP
#include <type_traits>
#include <sprout/config.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 int
signbit(FloatType x) {
return x < 0;
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::signbit;
# else
using sprout::math::detail::signbit;
# endif
} // namespace math
using sprout::math::signbit;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SIGNBIT_HPP

View file

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

View file

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

View file

@ -19,7 +19,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
tanh(FloatType x) {
return sprout::math::detail::sinh(x) / sprout::math::detail::cosh(x);
return sprout::math::sinh(x) / sprout::math::cosh(x);
}
template<

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_TRIGONOMETRIC_HPP
#define SPROUT_MATH_TRIGONOMETRIC_HPP
#include <sprout/config.hpp>
#include <sprout/math/sin.hpp>
#include <sprout/math/cos.hpp>
#include <sprout/math/tan.hpp>

View file

@ -11,16 +11,6 @@
#include <sprout/random/uniform_01.hpp>
namespace sprout {
namespace detail {
template<typename T>
SPROUT_CONSTEXPR T floor(T x) {
return x >= T(0) ? std::floor(x) : -std::ceil(-x);
}
template<typename T>
SPROUT_CONSTEXPR T ceil(T x) {
return x >= T(0) ? std::ceil(x) : -std::floor(-x);
}
} // namespace detail
namespace random {
//
// geometric_distribution