ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Functions
shlublu::Math Namespace Reference

Functions

template<typename T >
constantE ()
 Constant \(e\). More...
 
template<typename T >
constantLog2e ()
 Constant \(log2(e)\). More...
 
template<typename T >
constantLog10e ()
 Constant \(log10(e)\). More...
 
template<typename T >
constantLn2 ()
 Constant \(ln(2)\). More...
 
template<typename T >
constantLn10 ()
 Constant \(ln(10)\). More...
 
template<typename T >
constantPi ()
 Constant \(\pi\). More...
 
template<typename T >
constantPiBy2 ()
 Constant \(\pi / 2\). More...
 
template<typename T >
constantPiBy4 ()
 Constant \(\pi / 4\). More...
 
template<typename T >
constant1ByPi ()
 Constant \(1 / \pi\). More...
 
template<typename T >
constant2ByPi ()
 Constant \(2 / \pi\). More...
 
template<typename T >
constant2BySqrtPi ()
 Constant \(2 / \sqrt{\pi}\). More...
 
template<typename T >
constantSqrt2 ()
 Constant \(\sqrt{2}\). More...
 
template<typename T >
constant1BySqrt2 ()
 Constant \(1 / \sqrt{2}\). More...
 
template<typename T >
constantMaxIncrementable ()
 Maximal value that can be incremented to give a different value. More...
 
template<typename T >
constantMinDecrementable ()
 Minimal value that can be decremented to give a different value. More...
 
template<typename T >
bool sameSign (T x, T y)
 Tells whether x and y are of the same sign. More...
 
template<typename T >
clamp (T value, T minVal, T maxVal)
 Clamps a value to a range. More...
 
template<typename T >
roundX (T number, size_t digits)
 Rounds a number to the specified number of fractional digits. More...
 
template<typename T >
round2 (T number)
 Rounds a number to 2 fractional digits. More...
 
template<typename T >
factorial (T n)
 Returns the factorial of a given number. More...
 
template<typename VALUES_TYPE , typename INCREASE_TYPE = VALUES_TYPE>
INCREASE_TYPE proportionalIncrease (VALUES_TYPE initialValue, VALUES_TYPE finalValue)
 Returns the proportional increase from an initial value to a final value. More...
 
template<typename T >
increaseRate (T overallIncrease, size_t numPeriods)
 Converts an proportional increase over a number of periods to an increase rate per period. More...
 
template<typename VALUES_TYPE , typename INCREASE_TYPE = VALUES_TYPE>
INCREASE_TYPE increaseRate (VALUES_TYPE initialValue, VALUES_TYPE finalValue, size_t numPeriods)
 Returns the increase rate per period giving a final value from an initial value over a given number of periods. More...
 

Detailed Description

Set of helper constants and functions.

Function Documentation

◆ constantE()

template<typename T >
T shlublu::Math::constantE ( )
inline

Constant \(e\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(e\)

◆ constantLog2e()

template<typename T >
T shlublu::Math::constantLog2e ( )
inline

Constant \(log2(e)\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(log2(e)\)

◆ constantLog10e()

template<typename T >
T shlublu::Math::constantLog10e ( )
inline

Constant \(log10(e)\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(log10(e)\)

◆ constantLn2()

template<typename T >
T shlublu::Math::constantLn2 ( )
inline

Constant \(ln(2)\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(ln(2)\)

◆ constantLn10()

template<typename T >
T shlublu::Math::constantLn10 ( )
inline

Constant \(ln(10)\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(ln(10)\)

◆ constantPi()

template<typename T >
T shlublu::Math::constantPi ( )
inline

Constant \(\pi\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(\pi\)

◆ constantPiBy2()

template<typename T >
T shlublu::Math::constantPiBy2 ( )
inline

Constant \(\pi / 2\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(\pi / 2\)

◆ constantPiBy4()

template<typename T >
T shlublu::Math::constantPiBy4 ( )
inline

Constant \(\pi / 4\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(\pi / 4\)

◆ constant1ByPi()

template<typename T >
T shlublu::Math::constant1ByPi ( )
inline

Constant \(1 / \pi\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(1 / \pi\)

◆ constant2ByPi()

template<typename T >
T shlublu::Math::constant2ByPi ( )
inline

Constant \(2 / \pi\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(2 / \pi\)

◆ constant2BySqrtPi()

template<typename T >
T shlublu::Math::constant2BySqrtPi ( )
inline

Constant \(2 / \sqrt{\pi}\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(2 / \sqrt{\pi}\)

◆ constantSqrt2()

template<typename T >
T shlublu::Math::constantSqrt2 ( )
inline

Constant \(\sqrt{2}\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(\sqrt{2}\)

◆ constant1BySqrt2()

template<typename T >
T shlublu::Math::constant1BySqrt2 ( )
inline

Constant \(1 / \sqrt{2}\).

As per the legacy <math.h> C header.

Template Parameters
Tthe type of the returned value. This type should be floating point.
Returns
\(1 / \sqrt{2}\)

◆ constantMaxIncrementable()

template<typename T >
T shlublu::Math::constantMaxIncrementable ( )
inline

Maximal value that can be incremented to give a different value.

Determined as \(f + 1 \neq f\).

Template Parameters
Tthe type to apply this function to. This type should be floating point.
Returns
the value applicable to T
Note
Linux long double is currently handled as double though it would support higher values.

◆ constantMinDecrementable()

template<typename T >
T shlublu::Math::constantMinDecrementable ( )
inline

Minimal value that can be decremented to give a different value.

Determined as \(f - 1 \neq f\).

Template Parameters
Tthe type to apply this function to. This type should be floating point.
Returns
the value applicable to T
Note
Linux long double is currently handled as double though it would support lower values.

◆ sameSign()

template<typename T >
bool shlublu::Math::sameSign ( x,
y 
)

Tells whether x and y are of the same sign.

Template Parameters
thetype of x and y. This type should be arithmetic.
Parameters
xa value
yanother value
Returns
true if x and y are of the same sign, false otherwise

◆ clamp()

template<typename T >
T shlublu::Math::clamp ( value,
minVal,
maxVal 
)

Clamps a value to a range.

Template Parameters
TThe type of the value and the range. This type should be non-boolean arithmetic.
Parameters
valuethe value to clamp
minValthe lower inclusive bound of the range
maxValthe upper inclusive bound of the range
Returns
value if \(value \in [minVal, maxVal]\), minVal if \(value < minVal\), and maxVal if \(value > maxVal\)
Exceptions
std::invalid_argumentif minVal > maxVal

◆ roundX()

template<typename T >
T shlublu::Math::roundX ( number,
size_t  digits 
)

Rounds a number to the specified number of fractional digits.

Template Parameters
Tthe type of the number to round and of the returned value. This type should be floating point.
Parameters
numberthe number to round
digitsthe number of fractional digits to round number to
Returns
the rounded value of number

◆ round2()

template<typename T >
T shlublu::Math::round2 ( number)

Rounds a number to 2 fractional digits.

Template Parameters
Tthe type of the number to round and of the returned value. This type should be floating point.
Parameters
numberthe number to round
Returns
the rounded value of number
See also
roundX()

◆ factorial()

template<typename T >
T shlublu::Math::factorial ( n)

Returns the factorial of a given number.

This function is only defined for \(n \in \mathbb{N+}\)

Template Parameters
Tthe type of n and of the returned value
Parameters
nthe number whose factorial is to be calculated
Returns
the factorial of n
Exceptions
std::domain_errorif \(n \notin \mathbb{N+}\)

◆ proportionalIncrease()

template<typename VALUES_TYPE , typename INCREASE_TYPE = VALUES_TYPE>
INCREASE_TYPE shlublu::Math::proportionalIncrease ( VALUES_TYPE  initialValue,
VALUES_TYPE  finalValue 
)
inline

Returns the proportional increase from an initial value to a final value.

The increase I is defined as \(Vfinal = Vinitial + (Vinitial \times I)\), leading to \(I = (Vfinal / Vinitial) - 1\).

If I be negative, this is a decrease.

Template Parameters
VALUES_TYPEthe type of the initial and final values. This type should be arithmetic.
INCREASE_TYPEthe type of the resulting increase. This type should be floating point.
Parameters
initialValuethe initial value
finalValuethe final value
Returns
The proportional increase \((Vfinal / Vinitial) - 1\). initialValue is converted to INCREASE_TYPE to ensure this is a floating point division.
Exceptions
std::invalid_argumentif initialValue is zero

◆ increaseRate() [1/2]

template<typename T >
T shlublu::Math::increaseRate ( overallIncrease,
size_t  numPeriods 
)
inline

Converts an proportional increase over a number of periods to an increase rate per period.

This is the increase rate that produces the overall increase when applied to each period.
This rate R is defined as \((R + 1) ^ N = O + 1\), where N is the number of periods and O the overall increase. This leads to \(R = \sqrt[N]{O + 1} - 1\).

This function only applies to \(O > -1\) as -1 or less would correspond to a decrease of 100% or more, leading the result R not to be a real number.

Template Parameters
Tthe type of the overall increase and the returned rate. This type should be floating point.
Parameters
overallIncreasethe proportional increase over all the periods of times
numPeriodsthe number of periods of time that are covered
Returns
the increase rate \(\sqrt[N]{O + 1} - 1\)
Exceptions
std::invalid_argumentif numPeriods is zero
std::domain_errorif overallIncrease <= -1

◆ increaseRate() [2/2]

template<typename VALUES_TYPE , typename INCREASE_TYPE = VALUES_TYPE>
INCREASE_TYPE shlublu::Math::increaseRate ( VALUES_TYPE  initialValue,
VALUES_TYPE  finalValue,
size_t  numPeriods 
)
inline

Returns the increase rate per period giving a final value from an initial value over a given number of periods.

This rate R is defined as \(Vfinal = Vinitial \times (R + 1)^N\) where N is the number of periods. This leads to \(R = \sqrt[N]{Vfinal / Vinitial} - 1\).

This function only applies to:

  • \(Vinitial > 0\) and
  • \(Vfinal / Vinitial > 0\) as zero or less would correspond to a decrease of 100% or more, leading the result R not to be a real number.
Template Parameters
VALUES_TYPEthe type of the initial and final values. This type should be arithmetic.
INCREASE_TYPEthe type of the resulting increase. This type should be floating point.
Parameters
initialValuethe initial value
finalValuethe final value
numPeriodsthe number of periods of time that are covered
Returns
the increase rate \(\sqrt[N]{Vfinal / Vinitial} - 1\)
Exceptions
std::invalid_argumentif initialValue is zero or if numPeriods is zero
std::domain_errorif initialValue is zero, or if finalValue / initialValue <= 0