diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-28 13:54:54 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-28 13:54:54 +0200 |
| commit | bca7a28c57adbb585de1e7b6ef0b1990d0837c4f (patch) | |
| tree | bd39ccfdd34c39cc1bd9c34b59ec71142625f413 | |
| parent | 7258392baba60c0f4d71ff62ca4e50015cd22ae4 (diff) | |
feat: base_math.h: fixing the `surd` function for powers other than 2.v0.0.3e2-release
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | dev/LibC++/base_math.h | 21 |
1 files changed, 17 insertions, 4 deletions
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 <size_t Base> 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; } |
