Skip to content

Use better math constants #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api-changes/0.55.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# API Breaking changes for the 0.55 development cycle

## 0.55.2+

- **Deprecation:** The `M_PI`, `M_SQRT2` and `M_ROOT3` definitions are deprecated in favor of constants from `common/Math/Constant.h`.
- **Deprecation:** The `DegToRad()` and `RadToDeg()` macros are deprecated in favor of `Math::DegToRad()` and `Math::RadToDeg()`.
34 changes: 34 additions & 0 deletions src/common/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <algorithm>

#include "math/Constants.h"

namespace Math {

// This is designed to return min if value is NaN. That's not guaranteed to work when
Expand Down Expand Up @@ -64,8 +66,40 @@ namespace Math {
volatile uint64_t bits = Util::bit_cast<uint64_t>(x);
return ~bits & 0x7ff0000000000000;
}

inline float DegToRad( float angle )
{
return angle * divpi_180_f;
}

inline float RadToDeg( float angle )
{
return angle * div180_pi_f;
}

inline double DegToRad( double angle )
{
return angle * divpi_180_d;
}

inline double RadToDeg( double angle )
{
return angle * div180_pi_d;
}
}

#include "math/Vector.h"

template<typename A>
DEPRECATED A DEG2RAD( const A a )
{
return Math::DegToRad( a );
}

template<typename A>
DEPRECATED A RAD2DEG( const A a )
{
return Math::RadToDeg ( a );
}

#endif //COMMON_MATH_H_
65 changes: 65 additions & 0 deletions src/common/math/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Generated with tools/math-constants/math-constants.py
// Do not modify!

#ifndef COMMON_MATH_CONSTANTS_H_
#define COMMON_MATH_CONSTANTS_H_

namespace Math {
// Type Name Value Operation std::numbers GNU GLM
constexpr float phi_f = 1.61803399e+0f; // φ phif golden_ratio
constexpr float egamma_f = 1.56746826e+0f; // Γe egammaf
constexpr float ln2_f = 6.93147181e-1f; // ln(2) ln2f M_LN2f ln_two
constexpr float ln10_f = 2.30258509e+0f; // ln(10) ln10f M_LN10f ln_ten
constexpr float log2e_f = 1.44269504e+0f; // log2(e) log2ef M_LOG2Ef
constexpr float log10e_f = 4.34294482e-1f; // log10(e) log10ef M_LOG10Ef
constexpr float pi_f = 3.14159265e+0f; // π pif M_PIf
constexpr float mul2_pi_f = 6.28318531e+0f; // 2π two_pi
constexpr float inv_pi_f = 3.18309886e-1f; // 1÷π inv_pif M_1_PIf one_over_pi
constexpr float inv_mul2_pi_f = 1.59154943e-1f; // 1÷2π one_over_two_pi
constexpr float div2_pi_f = 6.36619772e-1f; // 2÷π M_2_PIf two_over_pi
constexpr float div180_pi_f = 57.2957795e+0f; // 180÷π
constexpr float divpi_2_f = 1.57079633e+0f; // π÷2 M_PI_2f half_pi
constexpr float divpi_4_f = 7.85398163e-1f; // π÷4 M_PI_4f quarter_pi
constexpr float divpi_90_f = 3.49065850e-2f; // π÷90
constexpr float divpi_180_f = 1.74532925e-2f; // π÷180
constexpr float divpi_360_f = 8.72664626e-3f; // π÷360
constexpr float sqrtpi_f = 1.77245385e+0f; // √π sqrtpif root_pi
constexpr float sqrt2_f = 1.41421356e+0f; // √2 sqrt2f M_SQRT2f root_two
constexpr float sqrt3_f = 1.73205081e+0f; // √3 sqrt3f root_three
constexpr float inv_sqrtpi_f = 1.77245385e+0f; // 1÷√π inv_sqrtpif
constexpr float inv_sqrt2_f = 7.07106781e-1f; // 1÷√2 inv_sqrt2f M_SQRT1_2f one_over_root_two
constexpr float inv_sqrt3_f = 5.77350269e-1f; // 1÷√3 inv_sqrt3f
constexpr float div2_sqrtpi_f = 1.12837917e+0f; // 2÷√π M_2_SQRTPIf two_over_root_pi
constexpr float inv_3_f = 3.33333333e-1f; // 1÷3 third
constexpr float div2_3_f = 6.66666667e-1f; // 2÷3 two_third

// Type Name Value Operation std::numbers GNU GLM
constexpr double phi_d = 1.6180339887498948482e+0; // φ phi golden_ratio
constexpr double egamma_d = 1.5674682557740530749e+0; // Γe egamma
constexpr double ln2_d = 6.9314718055994530942e-1; // ln(2) ln2 M_LN2 ln_two
constexpr double ln10_d = 2.3025850929940456840e+0; // ln(10) ln10 M_LN10 ln_ten
constexpr double log2e_d = 1.4426950408889634074e+0; // log2(e) log2e M_LOG2E
constexpr double log10e_d = 4.3429448190325182765e-1; // log10(e) log10e M_LOG10E
constexpr double pi_d = 3.1415926535897932385e+0; // π pi M_PI
constexpr double mul2_pi_d = 6.2831853071795864769e+0; // 2π two_pi
constexpr double inv_pi_d = 3.1830988618379067154e-1; // 1÷π inv_pi M_1_PI one_over_pi
constexpr double inv_mul2_pi_d = 1.5915494309189533577e-1; // 1÷2π one_over_two_pi
constexpr double div2_pi_d = 6.3661977236758134308e-1; // 2÷π M_2_PI two_over_pi
constexpr double div180_pi_d = 57.295779513082320877e+0; // 180÷π
constexpr double divpi_2_d = 1.5707963267948966192e+0; // π÷2 M_PI_2 half_pi
constexpr double divpi_4_d = 7.8539816339744830962e-1; // π÷4 M_PI_4 quarter_pi
constexpr double divpi_90_d = 3.4906585039886591538e-2; // π÷90
constexpr double divpi_180_d = 1.7453292519943295769e-2; // π÷180
constexpr double divpi_360_d = 8.7266462599716478846e-3; // π÷360
constexpr double sqrtpi_d = 1.7724538509055160273e+0; // √π sqrtpi root_pi
constexpr double sqrt2_d = 1.4142135623730950488e+0; // √2 sqrt2 M_SQRT2 root_two
constexpr double sqrt3_d = 1.7320508075688772935e+0; // √3 sqrt3 root_three
constexpr double inv_sqrtpi_d = 1.7724538509055160273e+0; // 1÷√π inv_sqrtpi
constexpr double inv_sqrt2_d = 7.0710678118654752440e-1; // 1÷√2 inv_sqrt2 M_SQRT1_2 one_over_root_two
constexpr double inv_sqrt3_d = 5.7735026918962576451e-1; // 1÷√3 inv_sqrt3
constexpr double div2_sqrtpi_d = 1.1283791670955125739e+0; // 2÷√π M_2_SQRTPI two_over_root_pi
constexpr double inv_3_d = 3.3333333333333333333e-1; // 1÷3 third
constexpr double div2_3_d = 6.6666666666666666667e-1; // 2÷3 two_third
}

#endif // COMMON_MATH_CONSTANTS_H_
2 changes: 1 addition & 1 deletion src/engine/audio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ namespace Audio {
const float PERCEPTUAL_C = 0.866224835960518f;
const float PERCEPTUAL_D = 0.187108105667604f;

return std::pow(10.0f, (perceptual - PERCEPTUAL_D) / 3.0f) - PERCEPTUAL_C;
return std::pow(10.0f, (perceptual - PERCEPTUAL_D) * Math::inv_3_f) - PERCEPTUAL_C;
}

float SliderToAmplitude(float slider) {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/client/cl_scrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void SCR_DrawConsoleFontUnichar( float x, float y, int ch )
{
glyphInfo_t *glyph = Glyph( ch );
float yadj = glyph->top;
float xadj = ( SCR_ConsoleFontUnicharWidth( ch ) - glyph->xSkip ) / 2.0;
float xadj = ( SCR_ConsoleFontUnicharWidth( ch ) - glyph->xSkip ) * 0.5f;

re.DrawStretchPic( x + xadj, y - yadj, glyph->imageWidth, glyph->imageHeight,
glyph->s, glyph->t,
Expand Down
Loading
Loading