42double SmallPow(
double x,
unsigned exponent)
68 return std::pow(x,
static_cast<double>(exponent));
72unsigned SmallPow(
unsigned x,
unsigned exponent)
94 if (exponent % 2 == 0)
97 unsigned partial_answer = SmallPow(x, exponent/2);
98 return partial_answer*partial_answer;
103 return SmallPow(x, exponent-1)*x;
109bool Divides(
double smallerNumber,
double largerNumber)
111 double remainder = fmod(largerNumber, smallerNumber);
116 if (remainder < DBL_EPSILON*largerNumber)
124 if (fabs(remainder-smallerNumber) < DBL_EPSILON*largerNumber)
132unsigned CeilDivide(
unsigned numerator,
unsigned denominator)
141 return ((numerator - 1u) / denominator) + 1u;
145double Signum(
double value)
147 return (0.0 < value) - (value < 0.0);
152 return fabs(number) <= fabs(tolerance);
155double SafeDivide(
double numerator,
double divisor)
158 if (divisor < 1.0 && numerator > divisor*DBL_MAX)
164 if (numerator == 0.0 || (divisor > 1.0 && numerator < divisor*DBL_MIN))
169 return numerator/divisor;
172unsigned AdvanceMod(
const unsigned currentLocation,
const int increment,
const std::size_t range)
noexcept
174 int new_pos = currentLocation + increment;
180 while (new_pos >=
static_cast<int>(range))
185 return static_cast<unsigned>(new_pos);
188unsigned SmallDifferenceMod(
const unsigned idxA,
const unsigned idxB,
const std::size_t range)
noexcept
190 assert(idxA < range);
191 assert(idxB < range);
193 const unsigned min = idxA < idxB ? idxA : idxB;
194 const unsigned max = idxA < idxB ? idxB : idxA;
196 return std::min<unsigned>(max - min, (range + min) - max);
201 double difference = fabs(number1 - number2);
202 double d1 =
SafeDivide(difference, fabs(number1));
203 double d2 =
SafeDivide(difference, fabs(number2));
205 return d1 <= tolerance && d2 <= tolerance;
210 return fabs(number1 - number2) <= tolerance;
216 if (printError && !ok)
218 std::cout <<
"CompareDoubles::WithinAnyTolerance: " << number1 <<
" and " << number2
219 <<
" differ by more than relative tolerance of " << relTol
220 <<
" and absolute tolerance of " << absTol << std::endl;
228 if (toleranceIsAbsolute)
238 std::cout <<
"CompareDoubles::WithinTolerance: " << number1 <<
" and " << number2
239 <<
" differ by more than " << (toleranceIsAbsolute ?
"absolute" :
"relative")
240 <<
" tolerance of " << tolerance << std::endl;
247 if (toleranceIsAbsolute)
249 return fabs(number1 - number2);
253 double difference = fabs(number1 - number2);
254 double d1 =
SafeDivide(difference, fabs(number1));
255 double d2 =
SafeDivide(difference, fabs(number2));
256 return d1 > d2 ? d1 : d2;
double SafeDivide(double numerator, double divisor)
static bool IsNearZero(double number, double tolerance)
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
static bool WithinTolerance(double number1, double number2, double tolerance, bool toleranceIsAbsolute)
static bool WithinRelativeTolerance(double number1, double number2, double tolerance)
static double Difference(double number1, double number2, bool toleranceIsAbsolute)
static bool WithinAbsoluteTolerance(double number1, double number2, double tolerance)