Added Math::Mod method
This commit is contained in:
@@ -75,5 +75,24 @@ bool Math::RandomChance(const double chance)
|
||||
return Random() <= chance;
|
||||
}
|
||||
|
||||
int Math::Mod(const int numerator, const int denominator)
|
||||
{
|
||||
if (denominator == 0)
|
||||
throw std::logic_error("Divide by zero");
|
||||
|
||||
// Quick optimizations:
|
||||
|
||||
// -> 0/n is always 0
|
||||
if (numerator == 0)
|
||||
return 0;
|
||||
|
||||
// -> operator% works for a > 0 && b > 0
|
||||
if (denominator > 0 && numerator > 0)
|
||||
return numerator % denominator;
|
||||
|
||||
// Else: generalized formula
|
||||
return (denominator + (numerator % denominator)) % denominator;
|
||||
}
|
||||
|
||||
std::mt19937 Math::rng;
|
||||
bool Math::isRngInitialized = true;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Eule
|
||||
{
|
||||
@@ -26,6 +27,10 @@ namespace Eule
|
||||
//! Compares two double values with a given accuracy
|
||||
[[nodiscard]] static constexpr bool Similar(const double a, const double b, const double epsilon = 0.00001);
|
||||
|
||||
//! Will compute the actual modulo of a fraction. The % operator returns bs for n<0.
|
||||
//! May throw divide-by-zero std::logic_error
|
||||
[[nodiscard]] static int Mod(const int numerator, const int denominator);
|
||||
|
||||
//! Will return a random double between `0` and `1`
|
||||
static double Random();
|
||||
|
||||
|
Reference in New Issue
Block a user