Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Static Public Member Functions | List of all members
Eule::Math Class Reference

Math utility class containing basic functions. More...

#include <Math.h>

Static Public Member Functions

static constexpr double Max (const double a, const double b)
 Will return the bigger of two values. More...
 
static constexpr double Min (const double a, const double b)
 Will return the smaller of two values. More...
 
static constexpr double Clamp (const double v, const double min, const double max)
 Will return v, but at least min, and at most max More...
 
static constexpr double Lerp (double a, double b, double t)
 Will return the linear interpolation between a and b by t More...
 
static constexpr double Abs (const double a)
 Will return the absolute value of a More...
 
static constexpr bool Similar (const double a, const double b, const double epsilon=0.00001)
 Compares two double values with a given accuracy. More...
 
static int Mod (const int numerator, const int denominator)
 Will compute the actual modulo of a fraction. More...
 
static double Random ()
 Will return a random double between 0 and 1 More...
 
static unsigned int RandomUint ()
 Will return a random unsigned integer. More...
 
static unsigned int RandomInt ()
 Will return a random integer. More...
 
static double RandomRange (const double min, const double max)
 Will return a random double within a range
These bounds are INCLUSIVE! More...
 
static int RandomIntRange (const int max, const int min)
 Will return a random integer within a range. More...
 
static bool RandomChance (const double chance)
 Will 'roll' a dice, returning true \(100 * chance\) percent of the time. More...
 
static double Oscillate (const double a, const double b, const double counter, const double speed)
 Kind of like \(sin(counter)\), but it oscillates over \([a,b]\) instead of \([-1,1]\), by a given speed. More...
 

Detailed Description

Math utility class containing basic functions.

Definition at line 9 of file Math.h.

Member Function Documentation

◆ Abs()

constexpr double Eule::Math::Abs ( const double  a)
inlinestaticconstexpr

Will return the absolute value of a

Definition at line 97 of file Math.h.

98  {
99  return (a > 0.0) ? a : -a;
100  }

◆ Clamp()

constexpr double Eule::Math::Clamp ( const double  v,
const double  min,
const double  max 
)
inlinestaticconstexpr

Will return v, but at least min, and at most max

Definition at line 86 of file Math.h.

87  {
88  return Max(Min(v, max), min);
89  }

◆ Lerp()

constexpr double Eule::Math::Lerp ( double  a,
double  b,
double  t 
)
inlinestaticconstexpr

Will return the linear interpolation between a and b by t

Definition at line 91 of file Math.h.

92  {
93  const double it = 1.0 - t;
94  return (a * it) + (b * t);
95  }

◆ Max()

constexpr double Eule::Math::Max ( const double  a,
const double  b 
)
inlinestaticconstexpr

Will return the bigger of two values.

Definition at line 76 of file Math.h.

77  {
78  return (a > b) ? a : b;
79  }

◆ Min()

constexpr double Eule::Math::Min ( const double  a,
const double  b 
)
inlinestaticconstexpr

Will return the smaller of two values.

Definition at line 81 of file Math.h.

82  {
83  return (a < b) ? a : b;
84  }

◆ Mod()

int Math::Mod ( const int  numerator,
const int  denominator 
)
static

Will compute the actual modulo of a fraction.

The % operator returns bs for n<0. May throw divide-by-zero std::logic_error

Definition at line 78 of file Math.cpp.

79 {
80  if (denominator == 0)
81  throw std::logic_error("Divide by zero");
82 
83  // Quick optimizations:
84 
85  // -> 0/n is always 0
86  if (numerator == 0)
87  return 0;
88 
89  // -> operator% works for a > 0 && b > 0
90  if (denominator > 0 && numerator > 0)
91  return numerator % denominator;
92 
93  // Else: generalized formula
94  return (denominator + (numerator % denominator)) % denominator;
95 }

◆ Oscillate()

double Math::Oscillate ( const double  a,
const double  b,
const double  counter,
const double  speed 
)
static

Kind of like \(sin(counter)\), but it oscillates over \([a,b]\) instead of \([-1,1]\), by a given speed.


Given that \(speed = 1\), the result will always be a if counter is even, and b if counter is uneven.
If counter is a rational, the result will oscillate between a and b, like sin() does.
If you increase speed, the oscillation frequency will increase. Meaning \(speed = 2\) would result in \(counter=0.5\) returning b.

Definition at line 68 of file Math.cpp.

69 {
70  return (sin(counter * speed * PI - HALF_PI) * 0.5 + 0.5) * (b-a) + a;
71 }

◆ Random()

double Math::Random ( )
static

Will return a random double between 0 and 1

Definition at line 29 of file Math.cpp.

30 {
32 
33  return (rng() % 694206942069ll) / 694206942069.0;
34 }

◆ RandomChance()

bool Math::RandomChance ( const double  chance)
static

Will 'roll' a dice, returning true \(100 * chance\) percent of the time.

Definition at line 73 of file Math.cpp.

74 {
75  return Random() <= chance;
76 }

◆ RandomInt()

unsigned int Math::RandomInt ( )
static

Will return a random integer.

Definition at line 45 of file Math.cpp.

46 {
48 
49  // Since this is supposed to return a random value anyways,
50  // we can let the random uint overflow without any problems.
51  return (int)rng();
52 }

◆ RandomIntRange()

int Math::RandomIntRange ( const int  max,
const int  min 
)
static

Will return a random integer within a range.

This is faster than (int)RandomRange(x,y)
These bounds are INCLUSIVE!

Definition at line 63 of file Math.cpp.

64 {
65  return (rng() % (max + 1 - min)) + min;
66 }

◆ RandomRange()

double Math::RandomRange ( const double  min,
const double  max 
)
static

Will return a random double within a range
These bounds are INCLUSIVE!

Definition at line 56 of file Math.cpp.

57 {
58  return (Random() * (max - min)) + min;
59 }

◆ RandomUint()

unsigned int Math::RandomUint ( )
static

Will return a random unsigned integer.

Definition at line 37 of file Math.cpp.

38 {
40 
41  return rng();
42 }

◆ Similar()

constexpr bool Eule::Math::Similar ( const double  a,
const double  b,
const double  epsilon = 0.00001 
)
inlinestaticconstexpr

Compares two double values with a given accuracy.

Definition at line 102 of file Math.h.

103  {
104  return Abs(a - b) <= epsilon;
105  }

The documentation for this class was generated from the following files:
MAKE_SURE_RNG_IS_INITIALIZED
#define MAKE_SURE_RNG_IS_INITIALIZED
Definition: Math.cpp:8
PI
static constexpr double PI
Pi up to 50 decimal places.
Definition: Constants.h:6
Eule::Math::Abs
static constexpr double Abs(const double a)
Will return the absolute value of a
Definition: Math.h:97
Eule::Math::Max
static constexpr double Max(const double a, const double b)
Will return the bigger of two values.
Definition: Math.h:76
Eule::Math::Min
static constexpr double Min(const double a, const double b)
Will return the smaller of two values.
Definition: Math.h:81
Eule::Math::Random
static double Random()
Will return a random double between 0 and 1
Definition: Math.cpp:29
HALF_PI
static constexpr double HALF_PI
Pi divided by two.
Definition: Constants.h:9