mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Added a few smaller helpers to math.h.
This commit is contained in:
parent
748a3ac49a
commit
f50f52e593
@ -5,6 +5,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <cstdint>
|
||||
#include "math_defs.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
#define MATH_PI 3.1415926535897932384626433833
|
||||
#define EPSILON 0.00001
|
||||
@ -55,8 +57,8 @@ public:
|
||||
static float inv_sqrt(const float x);
|
||||
static float fast_inv_sqrt(const float x);
|
||||
|
||||
inline static float absf(const float x) { return x > 0 ? x : -x; }
|
||||
inline static double absd(const double x) { return x > 0 ? x : -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 absi(const int x) { return x > 0 ? x : -x; }
|
||||
|
||||
inline static float deg2rad(const float x) { return x * MATH_PI / 180.0; }
|
||||
@ -67,9 +69,27 @@ public:
|
||||
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; }
|
||||
|
||||
inline static double lerp(double from, double to, double weight) { return from + (to - from) * weight; }
|
||||
inline static float lerp(float from, float to, float weight) { return from + (to - from) * weight; }
|
||||
|
||||
static float is_equal_approx(const float a, const float b);
|
||||
static float is_zero_approx(const float a);
|
||||
|
||||
//Taken from the Godot Engine (MIT License)
|
||||
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
static _ALWAYS_INLINE_ bool is_equal_approx_ratio(float a, float b, float epsilon = CMP_EPSILON, float min_epsilon = CMP_EPSILON) {
|
||||
// this is an approximate way to check that numbers are close, as a ratio of their average size
|
||||
// helps compare approximate numbers that may be very big or very small
|
||||
real_t diff = abs(a - b);
|
||||
if (diff == 0.0 || diff < min_epsilon) {
|
||||
return true;
|
||||
}
|
||||
real_t avg_size = (abs(a) + abs(b)) / 2.0;
|
||||
diff /= avg_size;
|
||||
return diff < epsilon;
|
||||
}
|
||||
|
||||
static void seed(const unsigned int s);
|
||||
static void randomize();
|
||||
|
||||
|
@ -701,7 +701,7 @@ String String::num(double p_num, int p_decimals) {
|
||||
|
||||
if (p_decimals < 0) {
|
||||
p_decimals = 14;
|
||||
const double abs_num = Math::absd(p_num);
|
||||
const double abs_num = Math::abs(p_num);
|
||||
if (abs_num > 10) {
|
||||
// We want to align the digits to the above sane default, so we only
|
||||
// need to subtract log10 for numbers with a positive power of ten.
|
||||
@ -868,7 +868,7 @@ String String::num_real(double p_num, bool p_trailing) {
|
||||
// Integer part.
|
||||
|
||||
bool neg = p_num < 0;
|
||||
p_num = Math::absd(p_num);
|
||||
p_num = Math::abs(p_num);
|
||||
int64_t intn = (int64_t)p_num;
|
||||
|
||||
// Decimal part.
|
||||
|
Loading…
Reference in New Issue
Block a user