From b017cd53d6b0c4d0e6ae321a8ae196e5dfd3eba8 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 6 Jul 2026 22:04:37 +0200 Subject: [PATCH] Avoid floating point underflow in thermal computation --- cmdline/thermal.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmdline/thermal.c b/cmdline/thermal.c index 17bdc749..6d0545a7 100644 --- a/cmdline/thermal.c +++ b/cmdline/thermal.c @@ -8,6 +8,7 @@ #include "io.h" #include +#include struct snapraid_thermal* thermal_alloc(uint64_t dev, const char* name) { @@ -103,7 +104,13 @@ struct snapraid_thermal_params fit_thermal_model(const struct snapraid_thermal_p } model.rmse = sqrt(sum_squared_residuals / n_points); - model.r_squared = 1.0 - (sum_squared_residuals / sum_total); + + /* avoid sum_total underflow and division by zero */ + if (sum_total > FLT_EPSILON) { + model.r_squared = 1.0 - (sum_squared_residuals / sum_total); + } else { + model.r_squared = 1.0; + } return model; }