aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2016-07-25 11:13:01 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2016-07-25 11:13:01 +0200
commit8fffc81606b2ab1ec1be8a8fd8335a15f79ee4ec (patch)
treefbfc2c795b7c4a30ba2e5beb0a066c1a92abf044
parent5f03584752e9b6e615281686f19057b5c10779b1 (diff)
Add NumTraits::digits10() function based on numeric_limits::digits10 and make use of it for printing matrices.
-rw-r--r--Eigen/src/Core/IO.h24
-rw-r--r--Eigen/src/Core/NumTraits.h41
2 files changed, 46 insertions, 19 deletions
diff --git a/Eigen/src/Core/IO.h b/Eigen/src/Core/IO.h
index 8c794837f..94e00f58b 100644
--- a/Eigen/src/Core/IO.h
+++ b/Eigen/src/Core/IO.h
@@ -125,32 +125,18 @@ DenseBase<Derived>::format(const IOFormat& fmt) const
namespace internal {
-template<typename Scalar, bool IsInteger>
-struct significant_decimals_default_impl
-{
- typedef typename NumTraits<Scalar>::Real RealScalar;
- static inline int run()
- {
- using std::ceil;
- using std::log10;
- return cast<RealScalar,int>(ceil(-log10(NumTraits<RealScalar>::epsilon())));
- }
-};
-
+// NOTE: This helper is kept for backward compatibility with previous code specializing
+// this internal::significant_decimals_impl structure. In the future we should directly
+// call digits10() which has been introduced in July 2016 in 3.3.
template<typename Scalar>
-struct significant_decimals_default_impl<Scalar, true>
+struct significant_decimals_impl
{
static inline int run()
{
- return 0;
+ return NumTraits<Scalar>::digits10();
}
};
-template<typename Scalar>
-struct significant_decimals_impl
- : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
-{};
-
/** \internal
* print the matrix \a _m to the output stream \a s using the output format \a fmt */
template<typename Derived>
diff --git a/Eigen/src/Core/NumTraits.h b/Eigen/src/Core/NumTraits.h
index 03f64a8e9..a688df504 100644
--- a/Eigen/src/Core/NumTraits.h
+++ b/Eigen/src/Core/NumTraits.h
@@ -12,6 +12,37 @@
namespace Eigen {
+namespace internal {
+
+// default implementation of digits10(), based on numeric_limits if specialized,
+// 0 for integer types, and log10(epsilon()) otherwise.
+template< typename T,
+ bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
+ bool is_integer = NumTraits<T>::IsInteger>
+struct default_digits10_impl
+{
+ static int run() { return std::numeric_limits<T>::digits10; }
+};
+
+template<typename T>
+struct default_digits10_impl<T,false,false> // Floating point
+{
+ static int run() {
+ using std::log10;
+ using std::ceil;
+ typedef typename NumTraits<T>::Real Real;
+ return int(ceil(-log10(NumTraits<Real>::epsilon())));
+ }
+};
+
+template<typename T>
+struct default_digits10_impl<T,false,true> // Integer
+{
+ static int run() { return 0; }
+};
+
+} // end namespace internal
+
/** \class NumTraits
* \ingroup Core_Module
*
@@ -48,6 +79,9 @@ namespace Eigen {
* \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
* value by the fuzzy comparison operators.
* \li highest() and lowest() functions returning the highest and lowest possible values respectively.
+ * \li digits10() function returning the number of decimal digits that can be represented without change. This is
+ * the analogue of <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/digits10">std::numeric_limits<T>::digits10</a>
+ * which is used as the default implementation if specialized.
*/
template<typename T> struct GenericNumTraits
@@ -93,6 +127,13 @@ template<typename T> struct GenericNumTraits
{
return numext::numeric_limits<T>::epsilon();
}
+
+ EIGEN_DEVICE_FUNC
+ static inline int digits10()
+ {
+ return internal::default_digits10_impl<T>::run();
+ }
+
EIGEN_DEVICE_FUNC
static inline Real dummy_precision()
{