Skip to content

Commit

Permalink
adding overload for Sqrt(Integer_type)
Browse files Browse the repository at this point in the history
to cast the integer type to double
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Apr 24, 2016
1 parent 242e94f commit 1ed9c0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/wrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <Eigen/Core>
#include <boost/lexical_cast.hpp>
Expand Down Expand Up @@ -460,12 +461,18 @@ Derived SignedAbsSqrt(const Eigen::ArrayBase<Derived>& m)
return m.unaryExpr(std::ptr_fun(SignedAbsSqrt_d));
}

template <class T>
template <class T, typename = typename std::enable_if<std::is_floating_point<T>::value,T>::type>
T Sqrt(T a)
{
return std::sqrt(a);
}

template <class T, typename = typename std::enable_if<std::is_integral<T>::value,T>::type>
double Sqrt(T a)
{
return std::sqrt(static_cast<double>(a));
}

template <typename Scalar, int M, int N>
Eigen::Array<Scalar, M, N> Sqrt(const Eigen::Array<Scalar, M, N>& m)
{
Expand Down
8 changes: 8 additions & 0 deletions test/test_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ BOOST_AUTO_TEST_CASE(test_Sqr_vector_Eigen_Array)
Sqr(v);
}

BOOST_AUTO_TEST_CASE(test_Sqrt_overloads)
{
BOOST_CHECK_EQUAL(Sqrt(2.f), std::sqrt(2.f));
BOOST_CHECK_EQUAL(Sqrt(2.) , std::sqrt(2.));
BOOST_CHECK_EQUAL(Sqrt(2.L), std::sqrt(2.L));
BOOST_CHECK_EQUAL(Sqrt(2) , std::sqrt(2.));
}

BOOST_AUTO_TEST_CASE(test_Sqrt_vector)
{
std::vector<double> v(3);
Expand Down

0 comments on commit 1ed9c0a

Please sign in to comment.