fix math::pow case x is negative and y is integer

This commit is contained in:
bolero-MURAKAMI 2014-07-15 22:25:04 +09:00
parent fca3a2e724
commit 5c10e4d85c
5 changed files with 186 additions and 9 deletions

View file

@ -35,8 +35,8 @@ Supported Compilers
Linux:
* GCC, C++11/14 mode: 4.7.0, 4.7.1, 4.7.2, 4.7.3, 4.8.0, 4.8.1, 4.8.2, 4.9.0
* Clang, C++11/14 mode: 3.2, 3.3, 3.4
* GCC, C++11/14 mode: 4.7.0, 4.7.1, 4.7.2, 4.7.3, 4.7.4, 4.8.0, 4.8.1, 4.8.2, 4.8.3, 4.9.0
* Clang, C++11/14 mode: 3.2, 3.3, 3.4, 3.4.1
*******************************************************************************
Author

View file

@ -8,7 +8,9 @@
#ifndef SPROUT_COMPLEX_VALUES_HPP
#define SPROUT_COMPLEX_VALUES_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/limits.hpp>
#include <sprout/complex/complex.hpp>
#include <sprout/complex/operators.hpp>
#include <sprout/math/sin.hpp>
@ -16,25 +18,49 @@
#include <sprout/math/atan2.hpp>
#include <sprout/math/sqrt.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/signbit.hpp>
#include <sprout/math/copysign.hpp>
#include <sprout/limits.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/complex_promote.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
template<typename T>
SPROUT_CONSTEXPR T
norm(sprout::complex<T> const& x);
//
// 26.4.7, values:
//
template<typename T>
inline SPROUT_CONSTEXPR T const&
real(sprout::complex<T> const& x) {
return x.real();
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType>::type
real(ArithmeticType x) {
return x;
}
template<typename T>
inline SPROUT_CONSTEXPR T const&
imag(sprout::complex<T> const& x) {
return x.imag();
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType>::type
imag(ArithmeticType) {
return 0;
}
template<typename T>
inline SPROUT_CONSTEXPR T&
real(sprout::complex<T>& x) {
@ -45,37 +71,112 @@ namespace sprout {
imag(sprout::complex<T>& x) {
return x.imag();
}
template<typename T>
inline SPROUT_CONSTEXPR T
abs(sprout::complex<T> const& x) {
return sprout::sqrt(sprout::norm(x));
}
template<typename T>
inline SPROUT_CONSTEXPR T
arg(sprout::complex<T> const& x) {
return sprout::atan2(x.imag(), x.real());
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType>::type
arg(ArithmeticType x) {
return sprout::atan2(ArithmeticType(0), x);
}
template<typename T>
inline SPROUT_CONSTEXPR T
norm(sprout::complex<T> const& x) {
return x.real() * x.real() + x.imag() * x.imag();
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType>::type
norm(ArithmeticType x) {
typedef typename sprout::complex_promote<ArithmeticType>::type type;
return type(x) * type(x);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
conj(sprout::complex<T> const& x) {
return sprout::complex<T>(x.real(), -x.imag());
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::complex_promote<ArithmeticType>::type
conj(ArithmeticType x) {
typedef typename sprout::complex_promote<ArithmeticType>::type type;
return type(x);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
proj(sprout::complex<T> const& x) {
return sprout::isinf(x.real()) || sprout::isinf(x.imag())
? sprout::complex<T>(sprout::numeric_limits<T>::infinity(), sprout::copysign(T(), x.imag()))
: x;
return sprout::math::isinf(x.real()) || sprout::math::isinf(x.imag())
? sprout::complex<T>(sprout::numeric_limits<T>::infinity(), sprout::math::copysign(T(), x.imag()))
: x
;
}
template<
typename ArithmeticType,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::complex_promote<ArithmeticType>::type
proj(ArithmeticType x) {
typedef typename sprout::complex_promote<ArithmeticType>::type type;
return sprout::math::isinf(x)
? type(sprout::numeric_limits<ArithmeticType>::infinity())
: type(x)
;
}
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
polar_impl(T const& x, T const& y) {
typedef sprout::complex<T> type;
return type(
sprout::math::isnan(x) ? T() : x,
sprout::math::isnan(y) ? T() : y
);
}
} // namespace detail
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
polar(T const& rho, T const& theta = 0) {
return sprout::complex<T>(rho * sprout::cos(theta), rho * sprout::sin(theta));
polar(T const& rho, T const& theta = T()) {
typedef sprout::complex<T> type;
return sprout::math::isnan(rho) || sprout::math::signbit(rho)
? type(sprout::numeric_limits<T>::quiet_NaN(), sprout::numeric_limits<T>::quiet_NaN())
: sprout::math::isnan(theta)
? sprout::math::isinf(rho) ? type(rho, theta)
: type(theta, theta)
: sprout::math::isinf(theta)
? sprout::math::isinf(rho) ? type(rho, sprout::numeric_limits<T>::quiet_NaN())
: type(sprout::numeric_limits<T>::quiet_NaN(), sprout::numeric_limits<T>::quiet_NaN())
: sprout::detail::polar_impl(rho * sprout::cos(theta), rho * sprout::sin(theta))
;
}
template<
typename ArithmeticType1, typename ArithmeticType2,
typename sprout::enabler_if<std::is_arithmetic<ArithmeticType1>::value && std::is_arithmetic<ArithmeticType2>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::complex_promote<ArithmeticType1, ArithmeticType2>::type
polar(ArithmeticType1 const& rho, ArithmeticType2 const& theta) {
typedef typename sprout::complex_promote<ArithmeticType1, ArithmeticType2>::type::value_type value_type;
return sprout::polar<value_type>(rho, theta);
}
} // namespace sprout

View file

@ -44,7 +44,11 @@ namespace sprout {
template<typename T>
inline SPROUT_CONSTEXPR T
pow_impl(T x, T y) {
return sprout::math::exp(y * sprout::math::log(x));
return x < 0
? is_odd(y) ? -sprout::math::exp(y * sprout::math::log(-x))
: sprout::math::exp(y * sprout::math::log(-x))
: sprout::math::exp(y * sprout::math::log(x))
;
}
} // namespace detail
//

View file

@ -30,6 +30,7 @@
#include <sprout/type_traits/common_decay.hpp>
#include <sprout/type_traits/arithmetic_promote.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/complex_promote.hpp>
#include <sprout/type_traits/inherit_if_type.hpp>
#include <sprout/type_traits/enable_if.hpp>
#include <sprout/type_traits/enabler_if.hpp>

View file

@ -0,0 +1,71 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_TYPE_TRAITS_COMPLEX_PROMOTE_HPP
#define SPROUT_TYPE_TRAITS_COMPLEX_PROMOTE_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/complex/complex.hpp>
#include <sprout/complex/type_traits.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/arithmetic_promote.hpp>
namespace sprout {
namespace detail {
template<typename T, bool = sprout::is_complex<T>::value>
struct complex_promote1
: public sprout::identity<T>
{};
template<typename T>
struct complex_promote1<T, false>
: public sprout::identity<sprout::complex<typename sprout::float_promote<T>::type> >
{};
template<typename T, typename U>
struct complex_promote2
: public sprout::identity<sprout::complex<
typename sprout::arithmetic_promote<
typename sprout::detail::complex_promote1<T>::type::value_type,
typename sprout::detail::complex_promote1<U>::type::value_type
>::type
>>
{};
template<typename... Types>
struct complex_promote_impl;
template<typename T, typename U, typename... Tail>
struct complex_promote_impl<T, U, Tail...>
: public sprout::detail::complex_promote_impl<
typename sprout::detail::complex_promote2<T, U>::type,
Tail...
>
{};
template<typename T>
struct complex_promote_impl<T>
: public sprout::detail::complex_promote1<T>
{};
} // namespace detail
//
// complex_promote
//
template<typename... Types>
struct complex_promote
: public sprout::detail::complex_promote_impl<
typename std::remove_cv<Types>::type...
>
{};
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename... Types>
using complex_promote_t = typename sprout::complex_promote<Types...>::type;
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
} // namespace sprout
#endif // #ifndef SPROUT_TYPE_TRAITS_COMPLEX_PROMOTE_HPP