From: Niklas Fehn Date: Fri, 18 Oct 2019 12:30:24 +0000 (+0200) Subject: refactor function Utilities::needed_digits() X-Git-Tag: v9.2.0-rc1~967^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84a08ac9776569b9e6504293fb44769a66a5a3fe;p=dealii.git refactor function Utilities::needed_digits() --- 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