From f50f52e593edc37fa364f73f2f1923e93a288908 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 16 Nov 2021 03:06:28 +0100 Subject: [PATCH] Added a few smaller helpers to math.h. --- core/math/math.h | 24 ++++++++++++++++++++++-- core/string.cpp | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/math/math.h b/core/math/math.h index 9338268..34bf3a5 100644 --- a/core/math/math.h +++ b/core/math/math.h @@ -5,6 +5,8 @@ #include #include #include +#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(); diff --git a/core/string.cpp b/core/string.cpp index 8e77162..8e05833 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -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.