From bca7a28c57adbb585de1e7b6ef0b1990d0837c4f Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 28 Aug 2025 13:54:54 +0200 Subject: feat: base_math.h: fixing the `surd` function for powers other than 2. Signed-off-by: Amlal El Mahrouss --- dev/LibC++/base_math.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'dev/LibC++') diff --git a/dev/LibC++/base_math.h b/dev/LibC++/base_math.h index 220d357..cf4313f 100644 --- a/dev/LibC++/base_math.h +++ b/dev/LibC++/base_math.h @@ -36,17 +36,30 @@ inline real_type pow(real_type in) { return result; } +inline real_type sqrt(real_type in) { + if (in == 0) return 0; + if (in == not_a_number) return not_a_number; + + auto constexpr const static Base = 2; + + auto x = in / Base; + + for (int i = 0; i < 10; ++i) { + x = (x + in / x) / Base; + } + + return x; +} + /// @brief Square of function, with Base template argument. /// @param of Base argument to find sqquare of template inline real_type surd(real_type in) { if (in == 0) return 0; if (in == 1) return 1; + if (Base == 1) return in; - - auto x = in / Base; - - if (Base == 2) return (x + in / x) / 2; + if (Base == 2) return sqrt(in); return not_a_number; } -- cgit v1.2.3