summaryrefslogtreecommitdiffhomepage
path: root/dev/LibC++/base_math.h
diff options
context:
space:
mode:
authorAmlal <amlal.elmahrouss@icloud.com>2024-11-01 19:41:55 +0100
committerAmlal <amlal.elmahrouss@icloud.com>2024-11-01 19:41:55 +0100
commit9832d04842245987a1af284866628c5f9bb1fa1b (patch)
tree68ce21ad31b164c15d841e85451540292b624348 /dev/LibC++/base_math.h
parent65fb4a1abc5ad8b0dd4f2f31199f53a748167f24 (diff)
IMP: Fixes and improvements to LibC++, add shell script to generate standard C+ headers.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/LibC++/base_math.h')
-rw-r--r--dev/LibC++/base_math.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/dev/LibC++/base_math.h b/dev/LibC++/base_math.h
new file mode 100644
index 0000000..bee116a
--- /dev/null
+++ b/dev/LibC++/base_math.h
@@ -0,0 +1,62 @@
+/* -------------------------------------------
+
+ Copyright EL Mahrouss Logic.
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibC++/defines.h>
+
+/// @file Math.h
+/// @brief Math functions.
+
+#ifdef __ZKA_USE_DOUBLE__
+typedef double real_type;
+#else
+typedef float real_type;
+#endif
+
+namespace std::base_math
+{
+ /// @brief Power function, with Repeat argument.
+ template <size_t Exponent>
+ inline real_type pow(real_type in)
+ {
+ if (Exponent == 0)
+ return 1; // Any number to the power of 0 is 1.
+
+ if (Exponent == 1)
+ return in; // Any number to the power of 1 is itself.
+
+ size_t cnt = Exponent;
+
+ real_type result = 1;
+
+ for (auto i = 0; i < cnt; ++i)
+ result *= in;
+
+ return result;
+ }
+
+ /// @brief Square of function, with Base template argument.
+ /// @param of Base argument to find sqquare of
+ template <size_t Base>
+ inline real_type sqr(real_type in)
+ {
+ if (in == 0)
+ return 0;
+
+ return pow<1 / Base>(in);
+ }
+
+ /// @brief Linear interpolation equation solver.
+ /// @param from where?
+ /// @param to to?
+ /// @param Updated diff value according to difference.
+ inline real_type lerp(real_type to, real_type from, real_type stat)
+ {
+ real_type diff = (to - from);
+ return from + (diff * stat);
+ }
+} // namespace std::base_math