c++ functions — Working with numbers

Introduction

I’m collecting all the common numeric function I have used over the years in a small file, keeping track of their dependencies. In this post, I will present to you all the functions I use, what they’re used for, and their dependencies.  I will be updating this post as I present more functions. Feel free to pick and choose what you want. They are saved in a zip file which you can download.

isNaN

To check if a number is NaN is real simple, as it turns out that comparing a NaN value to itself always returns false. So here it is:

//-------------------------------------------------------------------
// FUNCTION:            isNaN
// ARGUMENTS:           Number
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             Check whether a number is NaN
// DEPENDENCIES:        None
//-------------------------------------------------------------------
template<typename blType>
inline bool isNaN(const blType& Number)
{
    return (Number != Number);
}
//-------------------------------------------------------------------

isInf

To check if a number is infinite, any type of infinite, whether negative or positive infinity, we check if it lies within the min and max possible values for that type:

//-------------------------------------------------------------------
// FUNCTION:            isInf
// ARGUMENTS:           Number
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             Check whether a number is infinite
//                      (pos inf or neg inf both work)
// DEPENDENCIES:        std::numeric_limits<T>::min()
//                      std::numeric_limits<T>::max()
//-------------------------------------------------------------------
template<typename blType>
inline bool isInf(const blType& value)
{
    return (value >= std::numeric_limits<blType>::min() &&
            value <= std::numeric_limits<blType>::max());
}
//-------------------------------------------------------------------

sign

This function checks the sign of a number and returns -1,0,1 depending on the sign

//-------------------------------------------------------------------
// FUNCTION:            sign
// ARGUMENTS:           Number
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             If a number is negative, return -1,
//                      otherwise if a number is zero return 0,
//                      otherwise return 1
// DEPENDENCIES:        None
//-------------------------------------------------------------------
template<typename blType>
inline blType sign(const blType& Number)
{
    if(Number > blType(0))
        return blType(1);
    else if (Number < blType(0))
        return blType(-1);
    else
        return blType(0);
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
// FUNCTION:            sign
// ARGUMENTS:           Number
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             If the real part of a comples number is
//                      negative, return -1 otherwise return 1
// DEPENDENCIES:        std::real()
//                      std::complex<T>
//-------------------------------------------------------------------
template<typename blType>
inline complex<blType> sign(const complex<blType>& Number)
{
    if(std::real(Number) > 0)
        return complex<blType>(1);
    else if(std::real(Number) < 0)
        return complex<blType>(-1);
    else
        return complex<blType>(0);
}
//-------------------------------------------------------------------

RoundOff

This function rounds off a number to the number of decimal places specified by the precision parameter

//-------------------------------------------------------------------
// FUNCTION:            RoundOff
// ARGUMENTS:           Number,Precision
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             RoundOff a number usign the number
//                      of decimal places specified by Precision
// DEPENDENCIES:        std::pow
//                      std::abs
//-------------------------------------------------------------------
template<typename blType>
inline blType RoundOff(const blType& Number,const int& Precision)
{
    blType Multiplier = std::pow(int(10),std::abs(Precision));
    return (blType(int(Multiplier*Number + blType(0.5)))) / Multiplier;
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
// FUNCTION:            RoundOff
// ARGUMENTS:           Number,Precision
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             RoundOff a complex number usign the number
//                      of decimal places specified by Precision
// DEPENDENCIES:        RoundOff
//-------------------------------------------------------------------
template<typename blType>
inline complex<blType> RoundOff(const complex<blType>& Value,
                                const int& Precision)
{
    // For a complex number, we need to round off both the
    // real and imaginary parts
    return complex<blType>(RoundOff(std::real(Value),Precision),
                           RoundOff(std::imag(Value),Precision));
}
//-------------------------------------------------------------------

isZero

This function checks whether a number is zero by comparing it to the machine epsilon multiplied by some value specified by the passed argument

//-------------------------------------------------------------------
// FUNCTION:            isZero
// ARGUMENTS:           Number,HowManyEpsilons
// TEMPLATE ARGUMENTS:  blType
// PURPOSE:             Check if a number is zero by comparing it
//                      to the machine epsilon multiplied by the
//                      HowManyEpsilons argument
// DEPENDENCIES:        None
//-------------------------------------------------------------------
template<typename blType>
inline bool isZero(const blType& Number,
                   const blType HowManyEpsilons = 10)
{
    if(std::abs(Number) <= HowManyEpsilons * numeric_limits<blType>::epsilon())
        return true;
    else
        return false;
}
//-------------------------------------------------------------------

Factorial

This function calculates the factorial of an integer by using a simple while loop

//-------------------------------------------------------------------
// FUNCTION:            Factorial
// ARGUMENTS:           Number
// TEMPLATE ARGUMENTS:  None
// PURPOSE:             Simple factorial function using a loop
// DEPENDENCIES:        None
//-------------------------------------------------------------------
inline int Factorial(const int& Number)
{
    if(Number == 0)
        return 1;

    int Result = Number;

    for(int i = (Number - 1); i > 0; --i)
        Result *= i;

    return Result;
}
//-------------------------------------------------------------------

Downloads

Here you can download the individual file containing all the functions, or you can download the blMathAPI library which also has other tools I have created.
[download#3] [download#2]

Updates

I will be updating this post as I add more functions. Just re-download the file to get all the updated functions.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

Leave a Reply

Your email address will not be published. Required fields are marked *