programming_tutorials/02_math_osztaly/math.cpp
2021-03-27 20:33:09 +01:00

91 lines
2.1 KiB
C++

#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() / RANDOM_32BIT_MAX;
}
double Math::randd() {
return rand() / RANDOM_32BIT_MAX;
}
int rand(const int m) {
return rand() % m;
}
int 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;
}
/*
| + is_equal_approx(float a, float b) : bool { } | -> mint vektorban
| + is_zero_approx(float a) : bool { } |
| |
| + seed(unsigned int s) { } | -> srand(s);
| + randomize() { } | -> srand(time(NULL));
| |
| + rand() : int { } | -> return rand();
| + randf() : float { } | -> return rand() / RANDOM_32BIT_MAX;
| + randd() : float { } | -> return rand() / RANDOM_32BIT_MAX;
| |
| + rand(float from, float to) : float { } | -> return randf() * to + from;
| + rand(double from, double to) : double { } | -> return randd() * to + from;
|------------------------------------------------------------|
*/