From 8d92c898eef382e902175c2bf2cb5cead2bcd29f Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 22 Mar 2017 10:46:59 -0400 Subject: [PATCH] stepcompress: Always return 0 on negative number in safe_sqrt() sqrt() of a negative number in the C code returns NaN. This value results in behavior that is difficult to debug. Always return 0.0 instead as this results in better behavior that is easier to track down. Also, on some code paths, safe_sqrt is called on numbers that are multiplied by very large amounts (eg, 16000000**2) and thus distinguishing between large and small negative numbers is difficult. For now, report in the log if the value is below -0.001. Signed-off-by: Kevin O'Connor --- klippy/stepcompress.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/klippy/stepcompress.c b/klippy/stepcompress.c index 962039cd..a139a1f1 100644 --- a/klippy/stepcompress.c +++ b/klippy/stepcompress.c @@ -281,11 +281,11 @@ check_line(struct stepcompress *sc, struct step_move move) static double _safe_sqrt(double v) { - if (v > -0.001) - // Due to floating point truncation, it's possible to get a - // small negative number - treat it as zero. - return 0.; - return sqrt(v); + // Due to floating point truncation, it's possible to get a small + // negative number - treat it as zero. + if (v < -0.001) + errorf("safe_sqrt of %.9f", v); + return 0.; } static inline double safe_sqrt(double v) { return likely(v >= 0.) ? sqrt(v) : _safe_sqrt(v);