From f80c5980ea5bf84828a22dc6b4985a8747f85df4 Mon Sep 17 00:00:00 2001 From: meggert Date: Mon, 19 Jun 2023 17:04:45 +0200 Subject: [PATCH 1/2] max value calc fix --- src/fsfw/globalfunctions/math/VectorOperations.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fsfw/globalfunctions/math/VectorOperations.h b/src/fsfw/globalfunctions/math/VectorOperations.h index 197cd46a..1e84330e 100644 --- a/src/fsfw/globalfunctions/math/VectorOperations.h +++ b/src/fsfw/globalfunctions/math/VectorOperations.h @@ -54,7 +54,7 @@ class VectorOperations { } static T maxAbsValue(const T *vector, uint8_t size, uint8_t *index = 0) { - T max = -1; + T max = vector[size - 1]; for (; size > 0; size--) { T abs = vector[size - 1]; @@ -72,7 +72,7 @@ class VectorOperations { } static T maxValue(const T *vector, uint8_t size, uint8_t *index = 0) { - T max = -1; + T max = vector[size - 1]; for (; size > 0; size--) { if (vector[size - 1] > max) { From c7037d417a8dbc0e0b7d2c5244c02bb1fc3c1312 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Tue, 20 Jun 2023 11:54:15 +0200 Subject: [PATCH 2/2] nullptr check for optional argument --- .../globalfunctions/math/VectorOperations.h | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/fsfw/globalfunctions/math/VectorOperations.h b/src/fsfw/globalfunctions/math/VectorOperations.h index 1e84330e..b8f6b00f 100644 --- a/src/fsfw/globalfunctions/math/VectorOperations.h +++ b/src/fsfw/globalfunctions/math/VectorOperations.h @@ -53,8 +53,9 @@ class VectorOperations { mulScalar(vector, 1 / norm(vector, size), normalizedVector, size); } - static T maxAbsValue(const T *vector, uint8_t size, uint8_t *index = 0) { + static T maxAbsValue(const T *vector, uint8_t size, uint8_t *index = nullptr) { T max = vector[size - 1]; + uint8_t foundIndex = size - 1; for (; size > 0; size--) { T abs = vector[size - 1]; @@ -64,24 +65,35 @@ class VectorOperations { if (abs > max) { max = abs; if (index != 0) { - *index = size - 1; + foundIndex = size - 1; } } } + + if (index != nullptr) { + *index = foundIndex; + } + return max; } - static T maxValue(const T *vector, uint8_t size, uint8_t *index = 0) { + static T maxValue(const T *vector, uint8_t size, uint8_t *index = nullptr) { T max = vector[size - 1]; + uint8_t foundIndex = size - 1; for (; size > 0; size--) { if (vector[size - 1] > max) { max = vector[size - 1]; if (index != 0) { - *index = size - 1; + foundIndex = size - 1; } } } + + if (index != nullptr) { + *index = foundIndex; + } + return max; }