From c17ecdc35b518e7a1403c2f1b2c84895294c345f Mon Sep 17 00:00:00 2001 From: Eduardo Alonso Date: Fri, 6 Dec 2024 17:51:22 +0100 Subject: [PATCH] monitor: Fix dms to decimal degrees conversion Actual algorithm does not work properly for negative values. For example, if we input the value -54'15": * In the first loop it will set tval to -0.900 * In the second loop it will add .004166 to that value, which is wrong. This approach uses a variable to control if the value is negative. --- src/monitor/nwindowedit.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/monitor/nwindowedit.cpp b/src/monitor/nwindowedit.cpp index 888b52608..81e4c7a3f 100644 --- a/src/monitor/nwindowedit.cpp +++ b/src/monitor/nwindowedit.cpp @@ -296,6 +296,7 @@ bool NWindowEditDegrees::passKey (int key) double NWindowEditDegrees::getValueDouble () { + unsigned short neg = 0; char buf[getLength () + 1]; char *endptr; mvwinnstr (getWriteWindow (), 0, 0, buf, getLength ()); @@ -303,6 +304,10 @@ double NWindowEditDegrees::getValueDouble () double tval = 0; while (*p != '\0') { + if (*p == '-') { + neg = 1; + p++; + } double v = strtod (p, &endptr); switch (*endptr) { @@ -320,13 +325,13 @@ double NWindowEditDegrees::getValueDouble () tval += v / 3600.0; break; default: - return tval; + return neg ? -tval : tval; } p = endptr; if (*p != '\0') p++; } - return tval; + return neg ? -tval : tval; }