Chaste Commit::8b5d759ac2eb95e67ae57699734101efccb0a0a9
MathsCustomFunctions.hpp File Reference
#include <cfloat>
#include <cstddef>
+ Include dependency graph for MathsCustomFunctions.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  CompareDoubles
 

Functions

double SmallPow (double x, unsigned exponent)
 
unsigned SmallPow (unsigned x, unsigned exponent)
 
bool Divides (double smallerNumber, double largerNumber)
 
unsigned CeilDivide (unsigned numerator, unsigned denominator)
 
double Signum (double value)
 
double SafeDivide (double numerator, double divisor)
 
unsigned AdvanceMod (unsigned index, int increment, std::size_t range) noexcept
 
unsigned SmallDifferenceMod (unsigned idxA, unsigned idxB, std::size_t range) noexcept
 

Detailed Description

This file contains some utility functions and a small class for dealing with floating point numbers.

Definition in file MathsCustomFunctions.hpp.

Function Documentation

◆ AdvanceMod()

unsigned AdvanceMod ( unsigned  index,
int  increment,
std::size_t  range 
)
noexcept

Advance index by increment and return the result modulo range taking into account negative decrements. It is assumed that index + range does not overflow the int type, and that 0 < range < INT_MAX.

Note that this function is designed to avoid integer division and is very efficient when (index + increment) is in [-range, 2*range).

Parameters
indexthe current index to increment
incrementthe amount to increment the index by
rangethe top of the range; [0, range)
Returns
(current_location + increment) % range, accounting for negative values

Definition at line 172 of file MathsCustomFunctions.cpp.

◆ CeilDivide()

unsigned CeilDivide ( unsigned  numerator,
unsigned  denominator 
)
Returns
the result of dividing (unsigned) numerator by denominator, rounded up (away from 0), and overflow-safe for large integers up to UINT_MAX.
Parameters
numeratorthe numerator
denominatorthe denominator

Definition at line 132 of file MathsCustomFunctions.cpp.

◆ Divides()

bool Divides ( double  smallerNumber,
double  largerNumber 
)
Returns
true if the smaller number divides the larger (to machine precision) Uses fmod to determine if smallerNumber divides the largerNumber. We expect smallerNumber/largerNumber <= 1 and therefore fmod(largerNumber,smallerNumber) should be close to zero or close to smallerNumber.
Parameters
smallerNumberthe smaller
largerNumberthe larger

Definition at line 109 of file MathsCustomFunctions.cpp.

◆ SafeDivide()

double SafeDivide ( double  numerator,
double  divisor 
)
Returns
the results of dividing two non-negative floating point numbers, avoiding overflow and underflow.
Parameters
numeratorthe number to be divided
divisorthe number to divide by

Definition at line 155 of file MathsCustomFunctions.cpp.

Referenced by CompareDoubles::Difference(), and CompareDoubles::WithinRelativeTolerance().

◆ Signum()

double Signum ( double  value)
Returns
the sign of the argument, i.e. -1 if value<0, 0 if value=0, or +1 if value>0.
Parameters
valuethe argument value

Definition at line 145 of file MathsCustomFunctions.cpp.

◆ SmallDifferenceMod()

unsigned SmallDifferenceMod ( unsigned  idxA,
unsigned  idxB,
std::size_t  range 
)
noexcept

Calculate the smaller of the two differences between two indices, modulo N. I.e. the smallest number of increments from idxA required to get to idxB, modulo range.

Parameters
idxAfirst index, in [0, range)
idxBsecond index, in [0, range)
rangethe top of the range; [0, range)
Returns
the smallest difference between idxA and idxB

Definition at line 188 of file MathsCustomFunctions.cpp.

◆ SmallPow() [1/2]

double SmallPow ( double  x,
unsigned  exponent 
)

Replacement "pow" function.

Parameters
xnumber to be raised to a small power
exponentsmall integer exponent
Returns
x^exponent a.k.a x**exponent.

Definition at line 42 of file MathsCustomFunctions.cpp.

◆ SmallPow() [2/2]

unsigned SmallPow ( unsigned  x,
unsigned  exponent 
)

Replacement "pow" function for unsigned integer values

Parameters
xnumber to be raised to a small power
exponentsmall integer exponent
Returns
x^exponent a.k.a x**exponent.

Definition at line 72 of file MathsCustomFunctions.cpp.