Added my Math class.

This commit is contained in:
Relintai 2021-11-01 22:18:08 +01:00
parent 2c8e5e33b6
commit a19bcea0f0
2 changed files with 161 additions and 0 deletions

72
core/math.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "math.h"
float Math::inv_sqrt(const float x) {
return (float)(1.0 / ::sqrtf(x));
}
float Math::fast_inv_sqrt(const float number) {
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = *(long *)&y;
i = 0x5f3759df - (i >> 1);
y = *(float *)&i;
y = y * (threehalfs - (x2 * y * y));
return y;
}
float Math::is_equal_approx(const float a, const float b) {
if (a + EPSILON < b && a - EPSILON > b) {
return true;
}
return false;
}
float Math::is_zero_approx(const float a) {
if (a + EPSILON < 0 && a - EPSILON > 0) {
return true;
}
return false;
}
void Math::seed(const unsigned int s) {
srand(s);
}
void Math::randomize() {
srand(time(NULL));
}
int Math::rand() {
return ::rand();
}
float Math::randf() {
return ::rand() / static_cast<float>(RANDOM_32BIT_MAX);
}
double Math::randd() {
return ::rand() / static_cast<double>(RANDOM_32BIT_MAX);
}
int Math::rand(const int m) {
return rand() % m;
}
int Math::rand(const int from, const int to) {
return (rand() % (to - from)) + from;
}
float Math::rand(const float from, const float to) {
return randf() * (to - from) + from;
}
float Math::rand(const double from, const double to) {
return randd() * (to - from) + from;
}

89
core/math.h Normal file
View File

@ -0,0 +1,89 @@
#ifndef MATH_H
#define MATH_H
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <cstdint>
#define MATH_PI 3.1415926535897932384626433833
#define EPSILON 0.00001
class Math {
public:
static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF;
inline static float sin(const float x) { return ::sinf(x); }
inline static double sin(const double x) { return ::sin(x); }
inline static float cos(const float x) { return ::cosf(x); }
inline static double cos(const double x) { return ::cos(x); }
inline static float tan(const float x) { return ::tanf(x); }
inline static double tan(const double x) { return ::tan(x); }
inline static float asin(const float x) { return ::asinf(x); }
inline static double asin(const double x) { return ::asin(x); }
inline static float acos(const float x) { return ::acosf(x); }
inline static double acos(const double x) { return ::acos(x); }
inline static float atan(const float x) { return ::atanf(x); }
inline static double atan(const double x) { return ::atan(x); }
inline static float atan2(const float x, const float y) { return ::atan2f(x, y); }
inline static double atan2(const double x, const float y) { return ::atan2(x, y); }
inline static float sqrt(const float x) { return ::sqrtf(x); }
inline static double sqrt(const double x) { return ::sqrt(x); }
inline static float fmod(const float x, const float y) { return ::fmodf(x, y); }
inline static double fmod(const double x, const float y) { return ::fmod(x, y); }
inline static float floor(const float x) { return ::floorf(x); }
inline static double floor(const double x) { return ::floor(x); }
inline static float ceil(const float x) { return ::ceilf(x); }
inline static double ceil(const double x) { return ::ceil(x); }
inline static float pow(const float x, const float y) { return ::powf(x, y); }
inline static double pow(const double x, const float y) { return ::pow(x, y); }
inline static float log(const float x) { return ::logf(x); }
inline static double log(const double x) { return ::log(x); }
static float inv_sqrt(const float x);
static float fast_inv_sqrt(const float x);
inline static float abs(const float x) { return x > 0 ? x : -x; }
inline static double abs(const double x) { return x > 0 ? x : -x; }
inline static int abs(const int x) { return x > 0 ? x : -x; }
inline static float deg2rad(const float x) { return x * MATH_PI / 180.0; }
inline static double deg2rad(const double x) { return x * MATH_PI / 180.0; }
inline static int deg2rad(const int x) { return x * MATH_PI / 180.0; }
inline static float rad2deg(const float x) { return x * 180.0 / MATH_PI; }
inline static double rad2deg(const double x) { return x * 180.0 / MATH_PI; }
inline static int rad2deg(const int x) { return x * 180.0 / MATH_PI; }
static float is_equal_approx(const float a, const float b);
static float is_zero_approx(const float a);
static void seed(const unsigned int s);
static void randomize();
static int rand();
static float randf();
static double randd();
static int rand(const int m);
static int rand(const int from, const int to);
static float rand(const float from, const float to);
static float rand(const double from, const double to);
};
#endif