From 84a08ac9776569b9e6504293fb44769a66a5a3fe Mon Sep 17 00:00:00 2001 From: Niklas Fehn Date: Fri, 18 Oct 2019 14:30:24 +0200 Subject: [PATCH] refactor function Utilities::needed_digits() --- source/base/utilities.cc | 19 ++++++------------- tests/base/utilities_01.cc | 5 +++++ tests/base/utilities_01.output | 5 +++++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 1af3fe351f..3f3a31cd70 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -466,20 +466,13 @@ namespace Utilities unsigned int needed_digits(const unsigned int max_number) { - if (max_number < 10) + if (max_number > 0) + return static_cast( + std::ceil(std::log10(std::fabs(max_number + 0.1)))); + else return 1; - if (max_number < 100) - return 2; - if (max_number < 1000) - return 3; - if (max_number < 10000) - return 4; - if (max_number < 100000) - return 5; - if (max_number < 1000000) - return 6; - AssertThrow(false, ExcInvalidNumber(max_number)); - return 0; + + return 1; } diff --git a/tests/base/utilities_01.cc b/tests/base/utilities_01.cc index 9caea3ca1a..cc45dbd414 100644 --- a/tests/base/utilities_01.cc +++ b/tests/base/utilities_01.cc @@ -27,6 +27,11 @@ test() deallog << Utilities::int_to_string(42, 4) << std::endl; deallog << Utilities::int_to_string(42) << std::endl; deallog << Utilities::needed_digits(424) << std::endl; + deallog << Utilities::needed_digits(0) << std::endl; + deallog << Utilities::needed_digits(1) << std::endl; + deallog << Utilities::needed_digits(2) << std::endl; + deallog << Utilities::needed_digits(10) << std::endl; + deallog << Utilities::needed_digits(1000000000) << std::endl; deallog << Utilities::string_to_int(" 413 ") << std::endl; std::vector v; diff --git a/tests/base/utilities_01.output b/tests/base/utilities_01.output index aa659424e0..70f7fb9b73 100644 --- a/tests/base/utilities_01.output +++ b/tests/base/utilities_01.output @@ -2,6 +2,11 @@ DEAL::0042 DEAL::42 DEAL::3 +DEAL::1 +DEAL::1 +DEAL::1 +DEAL::2 +DEAL::10 DEAL::413 DEAL::1 DEAL::-12 -- 2.39.5