From 20e8297768cec6b9eae229272dead5d873624217 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 30 Jul 2012 16:31:51 +1000 Subject: [PATCH] Add query for decimal digits of an integer --- maths.cpp | 18 ++++++++++++++++++ maths.hpp | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/maths.cpp b/maths.cpp index 2d9cc7fc..5ae25e7e 100644 --- a/maths.cpp +++ b/maths.cpp @@ -65,6 +65,24 @@ template bool is_integer (const double&); template bool is_integer (const float&); +//----------------------------------------------------------------------------- +template <> +unsigned +digits (const uint32_t &v) { + return (v >= 1000000000) ? 10 : + (v >= 100000000) ? 9 : + (v >= 10000000) ? 8 : + (v >= 1000000) ? 7 : + (v >= 100000) ? 6 : + (v >= 10000) ? 5 : + (v >= 1000) ? 4 : + (v >= 100) ? 3 : + (v >= 10) ? 2 : + 1; +} + + +//----------------------------------------------------------------------------- template <> bool almost_equal (const float &a, const float &b) diff --git a/maths.hpp b/maths.hpp index a15d210d..3653e824 100644 --- a/maths.hpp +++ b/maths.hpp @@ -55,6 +55,10 @@ bool is_integer (const T& value) pure; +template +unsigned +digits (const T& value) pure; + /** * Check if two floating point numbers are approximately equal. Returns true