From 72300a653d7d249b6d522e359e39252288278058 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 25 Sep 2026 08:32:39 +0100 Subject: [PATCH] Code review updates --- docs/maths/math.md | 48 ++++++++--------- include/etl/format.h | 24 ++++----- include/etl/math.h | 14 ++--- include/etl/platform.h | 14 +---- include/etl/private/to_string_helper.h | 6 +-- test/test_math.cpp | 72 ++++++++++++++++++-------- test/vs2022/etl.vcxproj | 23 +++++++- test/vs2022/etl.vcxproj.filters | 2 +- 8 files changed, 118 insertions(+), 85 deletions(-) diff --git a/docs/maths/math.md b/docs/maths/math.md index 25c23aea..3ec1cc88 100644 --- a/docs/maths/math.md +++ b/docs/maths/math.md @@ -49,7 +49,7 @@ ETL_CONSTEXPR bool is_exactly_equal(T value1, T value2) ## floor -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -58,7 +58,7 @@ ETL_NODISCARD float floor(float value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::floorf`. +A wrapper around `::floorf` in ``. **Returns** `::floorf(value)` @@ -70,7 +70,7 @@ ETL_NODISCARD double floor(double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::floor`. +A wrapper around `::floor` in ``. **Returns** `::floor(value)` @@ -82,7 +82,7 @@ ETL_NODISCARD long double floor(long double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::floorl`. +A wrapper around `::floorl` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns @@ -92,7 +92,7 @@ otherwise ## ceil -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -101,7 +101,7 @@ ETL_NODISCARD float ceil(float value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::ceilf`. +A wrapper around `::ceilf` in ``. **Returns** `::ceilf(value)` @@ -113,7 +113,7 @@ ETL_NODISCARD double ceil(double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::ceil`. +A wrapper around `::ceil` in ``. **Returns** `::ceil(value)` @@ -125,7 +125,7 @@ ETL_NODISCARD long double ceil(long double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::ceill`. +A wrapper around `::ceill` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns @@ -135,7 +135,7 @@ otherwise ## log10 -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -144,7 +144,7 @@ ETL_NODISCARD float log10(float value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::log10f`. +A wrapper around `::log10f` in ``. **Returns** `::log10f(value)` @@ -156,7 +156,7 @@ ETL_NODISCARD double log10(double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::log10`. +A wrapper around `::log10` in ``. **Returns** `::log10(value)` @@ -168,7 +168,7 @@ ETL_NODISCARD long double log10(long double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::log10l`. +A wrapper around `::log10l` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns @@ -178,7 +178,7 @@ otherwise ## round -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -187,7 +187,7 @@ ETL_NODISCARD float round(float value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::roundf`. +A wrapper around `::roundf` in ``. **Returns** `::roundf(value)` @@ -199,7 +199,7 @@ ETL_NODISCARD double round(double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::round`. +A wrapper around `::round` in ``. **Returns** `::round(value)` @@ -211,7 +211,7 @@ ETL_NODISCARD long double round(long double value) ETL_NOEXCEPT ``` **Description** -A wrapper around `::roundl`. +A wrapper around `::roundl` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns @@ -221,7 +221,7 @@ otherwise ## pow -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -230,7 +230,7 @@ ETL_NODISCARD float pow(float base, float exponent) ETL_NOEXCEPT ``` **Description** -A wrapper around `::powf`. +A wrapper around `::powf` in ``. **Returns** `::powf(base, exponent)` @@ -242,7 +242,7 @@ ETL_NODISCARD double pow(double base, double exponent) ETL_NOEXCEPT ``` **Description** -A wrapper around `::pow`. +A wrapper around `::pow` in ``. **Returns** `::pow(base, exponent)` @@ -254,7 +254,7 @@ ETL_NODISCARD long double pow(long double base, long double exponent) ETL_NOEXCEPT ``` **Description** -A wrapper around `::powl`. +A wrapper around `::powl` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns @@ -264,7 +264,7 @@ otherwise ## modf -Defined in the `etl::clib` namespace. +Defined in the `etl::math` namespace. --- @@ -273,7 +273,7 @@ ETL_NODISCARD float modf(float value, float* intpart) ETL_NOEXCEPT ``` **Description** -A wrapper around `::modff`. +A wrapper around `::modff` in ``. **Returns** `::modff(value, intpart)` @@ -285,7 +285,7 @@ ETL_NODISCARD double modf(double value, double* intpart) ETL_NOEXCEPT ``` **Description** -A wrapper around `::modf`. +A wrapper around `::modf` in ``. **Returns** `::modf(value, intpart)` @@ -297,7 +297,7 @@ ETL_NODISCARD long double modf(long double value, long double* intpart) ETL_NOEXCEPT ``` **Description** -A wrapper around `::modfl`. +A wrapper around `::modfl` in ``. **Returns** If `ETL_NO_LONG_DOUBLE_MATH` is defined then returns diff --git a/include/etl/format.h b/include/etl/format.h index 05d05896..860d3c52 100644 --- a/include/etl/format.h +++ b/include/etl/format.h @@ -1756,7 +1756,7 @@ namespace etl } T integral; - T fractional = etl::clib::modf(value, &integral); + T fractional = etl::math::modf(value, &integral); // Take absolute values to avoid casting negative values to unsigned if (sign) @@ -1766,7 +1766,7 @@ namespace etl } unsigned long long int scale = int_pow(10, fractional_decimals); - unsigned long long int fractional_int = static_cast(etl::clib::round(fractional * scale)); + unsigned long long int fractional_int = static_cast(etl::math::round(fractional * scale)); unsigned long long int integral_int = static_cast(integral); if (fractional_int == scale) @@ -1793,20 +1793,20 @@ namespace etl bool sign = signbit(value); T integral; - T fractional = etl::clib::modf(value, &integral); + T fractional = etl::math::modf(value, &integral); while (value >= 0x10 || value <= -0x10) { ++exponent_int; value /= 0x10; - fractional = etl::clib::modf(value, &integral); + fractional = etl::math::modf(value, &integral); } while ((value > 0.0000000000001 && value < 1) || (value < -0.0000000000001 && value > -1)) { --exponent_int; value *= 0x10; - fractional = etl::clib::modf(value, &integral); + fractional = etl::math::modf(value, &integral); } // Take absolute values to avoid casting negative values to unsigned @@ -1817,7 +1817,7 @@ namespace etl } unsigned long long int scale = int_pow(0x10, fractional_decimals); - unsigned long long int fractional_int = static_cast(etl::clib::round(fractional * scale)); + unsigned long long int fractional_int = static_cast(etl::math::round(fractional * scale)); unsigned long long int integral_int = static_cast(integral); if (fractional_int == scale) @@ -1861,8 +1861,8 @@ namespace etl if (abs_value > static_cast(0)) { - exponent_int = static_cast(etl::clib::floor(etl::clib::log10(abs_value))); - value = abs_value / etl::clib::pow(static_cast(10), static_cast(exponent_int)); + exponent_int = static_cast(etl::math::floor(etl::math::log10(abs_value))); + value = abs_value / etl::math::pow(static_cast(10), static_cast(exponent_int)); // Correct for floating-point rounding in log10/pow if (value >= static_cast(10)) { @@ -1881,10 +1881,10 @@ namespace etl } T integral; - T fractional = etl::clib::modf(value, &integral); + T fractional = etl::math::modf(value, &integral); unsigned long long int scale = int_pow(10, fractional_decimals); - unsigned long long int fractional_int = static_cast(etl::clib::round(fractional * scale)); + unsigned long long int fractional_int = static_cast(etl::math::round(fractional * scale)); unsigned long long int integral_int = static_cast(integral); if (fractional_int == scale) @@ -1922,7 +1922,7 @@ namespace etl bool sign = signbit(value); T integral; - T fractional = etl::clib::modf(value, &integral); + T fractional = etl::math::modf(value, &integral); // Take absolute values to avoid casting negative values to unsigned if (sign) @@ -1932,7 +1932,7 @@ namespace etl } unsigned long long int scale = int_pow(10, fractional_decimals); - unsigned long long int fractional_int = static_cast(etl::clib::round(fractional * scale)); + unsigned long long int fractional_int = static_cast(etl::math::round(fractional * scale)); unsigned long long int integral_int = static_cast(integral); if (fractional_int == scale) diff --git a/include/etl/math.h b/include/etl/math.h index 400d5f8a..079b51fe 100644 --- a/include/etl/math.h +++ b/include/etl/math.h @@ -133,14 +133,14 @@ namespace etl #include "private/diagnostic_pop.h" //*************************************************************************** - // These functions wrap the C library math functions. + // These functions wrap the C math.h functions. // The functions are overloaded for the different floating point types. // They provide a consistent interface for float, double and long double. - // They are in the clib namespace to avoid conflicts with the C library and ETL functions. + // They are in the `math` namespace to avoid conflicts with the C library and ETL functions. // These take the place of std::floor, std::ceil etc which may not be available `No STL` environments. // Avoids unpredictable behaviour for global calls of ::floor, ::ceil etc when both and are included. //*************************************************************************** - namespace clib + namespace math { //*************************************************************************** // floor @@ -296,7 +296,7 @@ namespace etl #else //********************************* ETL_NODISCARD - inline long double power(long double base, long double exponent) ETL_NOEXCEPT + inline long double pow(long double base, long double exponent) ETL_NOEXCEPT { return static_cast(::pow(static_cast(base), static_cast(exponent))); } @@ -305,14 +305,12 @@ namespace etl //*************************************************************************** // modf //*************************************************************************** - ETL_NODISCARD inline float modf(float value, float* intpart) ETL_NOEXCEPT { return ::modff(value, intpart); } //********************************* - ETL_NODISCARD inline double modf(double value, double* intpart) ETL_NOEXCEPT { return ::modf(value, intpart); @@ -320,14 +318,12 @@ namespace etl #if ETL_USING_LONG_DOUBLE_MATH //********************************* - ETL_NODISCARD inline long double modf(long double value, long double* intpart) ETL_NOEXCEPT { return ::modfl(value, intpart); } #else //********************************* - ETL_NODISCARD inline long double modf(long double value, long double* intpart) ETL_NOEXCEPT { double d_iptr; @@ -337,7 +333,7 @@ namespace etl return static_cast(result); } #endif - } // namespace clib + } // namespace math } // namespace etl #endif diff --git a/include/etl/platform.h b/include/etl/platform.h index 9305461d..0eb78ae5 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -174,24 +174,12 @@ SOFTWARE. #define ETL_NOT_USING_FORMAT_FLOATING_POINT 0 #endif -//************************************* -// Helper macro for ETL_FORMAT_NO_LONG_DOUBLE_MATH. -// Define ETL_FORMAT_NO_LONG_DOUBLE_MATH if the toolchain does not provide -// long double math functions (log10l, floorl, powl, modfl, roundl). -// When defined, long double arguments are cast to double for math operations. -#if defined(ETL_FORMAT_NO_LONG_DOUBLE_MATH) - #define ETL_USING_FORMAT_LONG_DOUBLE_MATH 0 - #define ETL_NOT_USING_FORMAT_LONG_DOUBLE_MATH 1 -#else - #define ETL_USING_FORMAT_LONG_DOUBLE_MATH 1 - #define ETL_NOT_USING_FORMAT_LONG_DOUBLE_MATH 0 -#endif - //************************************* // Helper macro for ETL_NO_LONG_DOUBLE_MATH. // Define ETL_NO_LONG_DOUBLE_MATH if the toolchain does not provide // long double math functions (log10l, floorl, powl, modfl, roundl). // When defined, long double arguments are cast to double for math operations. +// ETL_FORMAT_NO_LONG_DOUBLE_MATH is deprecated, but still supported for backwards compatibility. #if defined(ETL_NO_LONG_DOUBLE_MATH) || defined(ETL_FORMAT_NO_LONG_DOUBLE_MATH) #define ETL_USING_LONG_DOUBLE_MATH 0 #define ETL_NOT_USING_LONG_DOUBLE_MATH 1 diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index 56bc0eae..4baaaf76 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -345,11 +345,11 @@ namespace etl } // Find the integral part of the floating point - T f_integral = etl::clib::floor(scaled); + T f_integral = etl::math::floor(scaled); uworkspace_t integral = static_cast(f_integral); // Find the fractional part of the floating point. - uworkspace_t fractional = static_cast(etl::clib::round((scaled - f_integral) * multiplier)); + uworkspace_t fractional = static_cast(etl::math::round((scaled - f_integral) * multiplier)); // Check for a rounding carry to the integral. if (fractional == multiplier) @@ -402,7 +402,7 @@ namespace etl } // Find the integral part of the floating point - T f_integral = etl::clib::floor(etl::absolute(value)); + T f_integral = etl::math::floor(etl::absolute(value)); uworkspace_t integral = static_cast(f_integral); // Find the fractional part of the floating point. diff --git a/test/test_math.cpp b/test/test_math.cpp index 258590ee..2592d7db 100644 --- a/test/test_math.cpp +++ b/test/test_math.cpp @@ -141,12 +141,12 @@ namespace long double ld1 = 1.7L; long double ld2 = -1.7L; - CHECK_CLOSE(1.0f, etl::clib::floor(f1), 0.0001f); - CHECK_CLOSE(-2.0f, etl::clib::floor(f2), 0.0001f); - CHECK_CLOSE(1.0, etl::clib::floor(d1), 0.0001); - CHECK_CLOSE(-2.0, etl::clib::floor(d2), 0.0001); - CHECK_CLOSE(1.0L, etl::clib::floor(ld1), 0.0001L); - CHECK_CLOSE(-2.0L, etl::clib::floor(ld2), 0.0001L); + CHECK_CLOSE(1.0f, etl::math::floor(f1), 0.0001f); + CHECK_CLOSE(-2.0f, etl::math::floor(f2), 0.0001f); + CHECK_CLOSE(1.0, etl::math::floor(d1), 0.0001); + CHECK_CLOSE(-2.0, etl::math::floor(d2), 0.0001); + CHECK_CLOSE(1.0L, etl::math::floor(ld1), 0.0001L); + CHECK_CLOSE(-2.0L, etl::math::floor(ld2), 0.0001L); } //************************************************************************* @@ -159,12 +159,12 @@ namespace long double ld1 = 1.2L; long double ld2 = -1.2L; - CHECK_CLOSE(2.0f, etl::clib::ceil(f1), 0.0001f); - CHECK_CLOSE(-1.0f, etl::clib::ceil(f2), 0.0001f); - CHECK_CLOSE(2.0, etl::clib::ceil(d1), 0.0001); - CHECK_CLOSE(-1.0, etl::clib::ceil(d2), 0.0001); - CHECK_CLOSE(2.0L, etl::clib::ceil(ld1), 0.0001L); - CHECK_CLOSE(-1.0L, etl::clib::ceil(ld2), 0.0001L); + CHECK_CLOSE(2.0f, etl::math::ceil(f1), 0.0001f); + CHECK_CLOSE(-1.0f, etl::math::ceil(f2), 0.0001f); + CHECK_CLOSE(2.0, etl::math::ceil(d1), 0.0001); + CHECK_CLOSE(-1.0, etl::math::ceil(d2), 0.0001); + CHECK_CLOSE(2.0L, etl::math::ceil(ld1), 0.0001L); + CHECK_CLOSE(-1.0L, etl::math::ceil(ld2), 0.0001L); } //************************************************************************* @@ -174,9 +174,39 @@ namespace double d1 = 1000.0; long double ld1 = 10000.0L; - CHECK_CLOSE(2.0f, etl::clib::log10(f1), 0.0001f); - CHECK_CLOSE(3.0, etl::clib::log10(d1), 0.0001); - CHECK_CLOSE(4.0L, etl::clib::log10(ld1), 0.0001L); + CHECK_CLOSE(2.0f, etl::math::log10(f1), 0.0001f); + CHECK_CLOSE(3.0, etl::math::log10(d1), 0.0001); + CHECK_CLOSE(4.0L, etl::math::log10(ld1), 0.0001L); + } + + //************************************************************************* + TEST(test_round) + { + float f1 = 1.5f; + float f2 = -1.5f; + float f3 = 1.4f; + float f4 = -1.4f; + double d1 = 2.5; + double d2 = -2.5; + double d3 = 2.4; + double d4 = -2.4; + long double ld1 = 3.5L; + long double ld2 = -3.5L; + long double ld3 = 3.4L; + long double ld4 = -3.4L; + + CHECK_CLOSE(2.0f, etl::math::round(f1), 0.0001f); + CHECK_CLOSE(-2.0f, etl::math::round(f2), 0.0001f); + CHECK_CLOSE(1.0f, etl::math::round(f3), 0.0001f); + CHECK_CLOSE(-1.0f, etl::math::round(f4), 0.0001f); + CHECK_CLOSE(3.0, etl::math::round(d1), 0.0001); + CHECK_CLOSE(-3.0, etl::math::round(d2), 0.0001); + CHECK_CLOSE(2.0, etl::math::round(d3), 0.0001); + CHECK_CLOSE(-2.0, etl::math::round(d4), 0.0001); + CHECK_CLOSE(4.0L, etl::math::round(ld1), 0.0001L); + CHECK_CLOSE(-4.0L, etl::math::round(ld2), 0.0001L); + CHECK_CLOSE(3.0L, etl::math::round(ld3), 0.0001L); + CHECK_CLOSE(-3.0L, etl::math::round(ld4), 0.0001L); } //************************************************************************* @@ -189,9 +219,9 @@ namespace long double ldb = 2.0L; long double lde = 4.0L; - CHECK_CLOSE(8.0f, etl::clib::pow(fb, fe), 0.0001f); - CHECK_CLOSE(1024.0, etl::clib::pow(db, de), 0.0001); - CHECK_CLOSE(16.0L, etl::clib::pow(ldb, lde), 0.0001L); + CHECK_CLOSE(8.0f, etl::math::pow(fb, fe), 0.0001f); + CHECK_CLOSE(1024.0, etl::math::pow(db, de), 0.0001); + CHECK_CLOSE(16.0L, etl::math::pow(ldb, lde), 0.0001L); } //************************************************************************* @@ -204,9 +234,9 @@ namespace long double ld1 = 3.75L; long double ld_intpart = 0.0L; - float f_frac = etl::clib::modf(f1, &f_intpart); - double d_frac = etl::clib::modf(d1, &d_intpart); - long double ld_frac = etl::clib::modf(ld1, &ld_intpart); + float f_frac = etl::math::modf(f1, &f_intpart); + double d_frac = etl::math::modf(d1, &d_intpart); + long double ld_frac = etl::math::modf(ld1, &ld_intpart); CHECK_CLOSE(3.0f, f_intpart, 0.0001f); CHECK_CLOSE(0.75f, f_frac, 0.0001f); diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index 7a69c37e..010846c4 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -6304,7 +6304,26 @@ true true - + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -11807,4 +11826,4 @@ - + \ No newline at end of file diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index 10afee24..d27cedd4 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -4124,4 +4124,4 @@ Resource Files\Images - + \ No newline at end of file