63 if (exponent % 2 == 0)
66 double partial_answer =
SmallPow(x, exponent/2);
67 return partial_answer*partial_answer;
77 unsigned SmallPow(
unsigned x,
unsigned exponent)
99 if (exponent % 2 == 0)
102 unsigned partial_answer =
SmallPow(x, exponent/2);
103 return partial_answer*partial_answer;
114 bool Divides(
double smallerNumber,
double largerNumber)
116 double remainder = fmod(largerNumber, smallerNumber);
121 if (remainder < DBL_EPSILON*largerNumber)
129 if (fabs(remainder-smallerNumber) < DBL_EPSILON*largerNumber)
137 unsigned CeilDivide(
unsigned numerator,
unsigned denominator)
146 return ((numerator - 1u) / denominator) + 1u;
152 return (0.0 < value) - (value < 0.0);
157 return fabs(number) <= fabs(tolerance);
163 if (divisor < 1.0 && numerator > divisor*DBL_MAX)
169 if (numerator == 0.0 || (divisor > 1.0 && numerator < divisor*DBL_MIN))
174 return numerator/divisor;
179 double difference = fabs(number1 - number2);
180 double d1 =
SafeDivide(difference, fabs(number1));
181 double d2 =
SafeDivide(difference, fabs(number2));
183 return d1 <= tolerance && d2 <= tolerance;
188 return fabs(number1 - number2) <= tolerance;
194 if (printError && !ok)
196 std::cout <<
"CompareDoubles::WithinAnyTolerance: " << number1 <<
" and " << number2
197 <<
" differ by more than relative tolerance of " << relTol
198 <<
" and absolute tolerance of " << absTol << std::endl;
206 if (toleranceIsAbsolute)
216 std::cout <<
"CompareDoubles::WithinTolerance: " << number1 <<
" and " << number2
217 <<
" differ by more than " << (toleranceIsAbsolute ?
"absolute" :
"relative")
218 <<
" tolerance of " << tolerance << std::endl;
225 if (toleranceIsAbsolute)
227 return fabs(number1 - number2);
231 double difference = fabs(number1 - number2);
232 double d1 =
SafeDivide(difference, fabs(number1));
233 double d2 =
SafeDivide(difference, fabs(number2));
234 return d1 > d2 ? d1 : d2;
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
double SmallPow(double x, unsigned exponent)
unsigned CeilDivide(unsigned numerator, unsigned denominator)
double SafeDivide(double numerator, double divisor)
bool Divides(double smallerNumber, double largerNumber)
double Signum(double value)
static double Difference(double number1, double number2, bool toleranceIsAbsolute)
static bool WithinRelativeTolerance(double number1, double number2, double tolerance)
static bool WithinAbsoluteTolerance(double number1, double number2, double tolerance)
static bool IsNearZero(double number, double tolerance)
static bool WithinTolerance(double number1, double number2, double tolerance, bool toleranceIsAbsolute)