]> https://gitweb.dealii.org/ - dealii.git/commitdiff
refactor function Utilities::needed_digits()
authorNiklas Fehn <fehn@lnm.mw.tum.de>
Fri, 18 Oct 2019 12:30:24 +0000 (14:30 +0200)
committerNiklas Fehn <fehn@lnm.mw.tum.de>
Fri, 18 Oct 2019 13:01:47 +0000 (15:01 +0200)
source/base/utilities.cc
tests/base/utilities_01.cc
tests/base/utilities_01.output

index 1af3fe351fa86f2d1aa8b802af913b962120fcdc..3f3a31cd705c84c365a8ca3daba8294047846236 100644 (file)
@@ -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<int>(
+        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;
   }
 
 
index 9caea3ca1ad6927f4142dd2da1752a83a2fc63c9..cc45dbd414e5fc33e6b2d0b7676420b8de194470 100644 (file)
@@ -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<std::string> v;
index aa659424e0a2e0d93f3bc8fe353d7a6a20509c03..70f7fb9b73f93ab0ebeefa87a95b71b41a23306d 100644 (file)
@@ -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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.