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);
165 double SafeDivide(
double numerator,
double divisor);
167 double SafeDivide(
double numerator,
double divisor)
170 if (divisor < 1.0 && numerator > divisor*DBL_MAX)
176 if (numerator == 0.0 || (divisor > 1.0 && numerator < divisor*DBL_MIN))
181 return numerator/divisor;
187 double difference = fabs(number1 - number2);
188 double d1 = SafeDivide(difference, fabs(number1));
189 double d2 = SafeDivide(difference, fabs(number2));
191 return d1 <= tolerance && d2 <= tolerance;
196 return fabs(number1 - number2) <= tolerance;
202 if (printError && !ok)
204 std::cout <<
"CompareDoubles::WithinAnyTolerance: " << number1 <<
" and " << number2
205 <<
" differ by more than relative tolerance of " << relTol
206 <<
" and absolute tolerance of " << absTol << std::endl;
214 if (toleranceIsAbsolute)
224 std::cout <<
"CompareDoubles::WithinTolerance: " << number1 <<
" and " << number2
225 <<
" differ by more than " << (toleranceIsAbsolute ?
"absolute" :
"relative")
226 <<
" tolerance of " << tolerance << std::endl;
233 if (toleranceIsAbsolute)
235 return fabs(number1 - number2);
239 double difference = fabs(number1 - number2);
240 double d1 = SafeDivide(difference, fabs(number1));
241 double d2 = SafeDivide(difference, fabs(number2));
242 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)
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)