summaryrefslogtreecommitdiffhomepage
path: root/dev/LibC++/base_math.h
blob: dc839cce38cd18383770db464ccb5b5e35b6dac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* -------------------------------------------

	Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#pragma once

#include <LibC++/defines.h>

/// @file Math.h
/// @brief Math functions.

#ifdef __NE_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