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 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 8 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 92 of file Math.h.

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

## 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 81 of file Math.h.

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

## 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 86 of file Math.h.

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

## Max()

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

inlinestaticconstexpr

Will return the bigger of two values.

Definition at line 71 of file Math.h.

{
return (a > b) ? a : b;
}

## Min()

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

inlinestaticconstexpr

Will return the smaller of two values.

Definition at line 76 of file Math.h.

{
return (a < b) ? a : b;
}

## 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.

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

## Random()

double Math::Random | ( |  | ) |

static

Will return a random double between `0` and `1`

Definition at line 29 of file Math.cpp.

{
MAKE_SURE_RNG_IS_INITIALIZED;
return (rng() % 694206942069ll) / 694206942069.0;
}

## 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.

{
return Random() <= chance;
}

## RandomInt()

unsigned int Math::RandomInt | ( |  | ) |

static

Will return a random integer.

Definition at line 45 of file Math.cpp.

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

## 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.

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

## 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.

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

## RandomUint()

unsigned int Math::RandomUint | ( |  | ) |

static

Will return a random unsigned integer.

Definition at line 37 of file Math.cpp.

{
MAKE_SURE_RNG_IS_INITIALIZED;
return rng();
}

## Similar()

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

staticconstexpr

Compares two double values with a given accuracy.

The documentation for this class was generated from the following files:

- Eule/Math.h
- Eule/Math.cpp

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::Max
static constexpr double Max(const double a, const double b)
Will return the bigger of two values.
Definition: Math.h:71
Eule::Math::Min
static constexpr double Min(const double a, const double b)
Will return the smaller of two values.
Definition: Math.h:76
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