Guard result.rms against division by zero (#2317)

Co-authored-by: Roman Lebedev <lebedev.ri@gmail.com>
diff --git a/src/complexity.cc b/src/complexity.cc
index c431a3f..60c40d4 100644
--- a/src/complexity.cc
+++ b/src/complexity.cc
@@ -115,7 +115,9 @@
 
   // Normalized RMS by the mean of the observed values
   double mean = sigma_time / static_cast<double>(n.size());
-  result.rms = std::sqrt(rms / static_cast<double>(n.size())) / mean;
+  result.rms = std::fpclassify(mean) == FP_ZERO
+                   ? 0.0
+                   : std::sqrt(rms / static_cast<double>(n.size())) / mean;
 
   return result;
 }
diff --git a/test/complexity_test.cc b/test/complexity_test.cc
index 49622dc..f9ebabd 100644
--- a/test/complexity_test.cc
+++ b/test/complexity_test.cc
@@ -298,6 +298,42 @@
                      enum_auto_big_o_n, /*family_index=*/10, "ms",
                      "1\\.[0-9]+e-03");
 
+void BM_Complexity_O1_ZeroTime(benchmark::State& state) {
+  for (auto _ : state) {
+    state.SetIterationTime(0.0);
+  }
+  state.SetComplexityN(state.range(0));
+}
+BENCHMARK(BM_Complexity_O1_ZeroTime)
+    ->Range(1, 2)
+    ->UseManualTime()
+    ->Complexity(benchmark::o1);
+BENCHMARK(BM_Complexity_O1_ZeroTime)
+    ->Range(1, 2)
+    ->UseManualTime()
+    ->Complexity();
+BENCHMARK(BM_Complexity_O1_ZeroTime)
+    ->Range(1, 2)
+    ->UseManualTime()
+    ->Complexity([](benchmark::IterationCount) { return 1.0; });
+
+constexpr char one_test_name_zero_time[] =
+    "BM_Complexity_O1_ZeroTime/manual_time";
+constexpr char big_o_1_test_name_zero_time[] =
+    "BM_Complexity_O1_ZeroTime/manual_time_BigO";
+constexpr char rms_o_1_test_name_zero_time[] =
+    "BM_Complexity_O1_ZeroTime/manual_time_RMS";
+
+ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
+                     rms_o_1_test_name_zero_time, enum_auto_big_o_1, 11, "ns",
+                     "%float");
+ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
+                     rms_o_1_test_name_zero_time, enum_auto_big_o_1, 12, "ns",
+                     "%float");
+ADD_COMPLEXITY_CASES(one_test_name_zero_time, big_o_1_test_name_zero_time,
+                     rms_o_1_test_name_zero_time, lambda_big_o_1, 13, "ns",
+                     "%float");
+
 }  // end namespace
 
 // ========================================================================= //