Source

neglect / neglect / cpplib / include / neglect / math.hpp

Full commit
#ifndef _INC_NEGLECT_MATH_HPP_
#define _INC_NEGLECT_MATH_HPP_

#include <neglect/boot.hpp>

namespace neglect {

    /* check if two numeric values are almost equal */
    inline bool almost_equal(int a, int b)
    {
        return a == b;
    }

    inline bool almost_equal(size_t a, size_t b)
    {
        return a == b;
    }

    inline bool almost_equal(float a, float b)
    {
        return (a - 1e-08 < b && a + 1e-08 > b);
    }

    inline bool almost_equal(double a, double b)
    {
        return (a - 1e-10 < b && a + 1e-10 > b);
    }

    // currently these are just aliases to the C/C++ math functions
    // but this gives the possibility to replace them later with more
    // optimized functions
    namespace math {
        static const double e  = 2.7182818284590452354;
        static const double pi = 3.14159265358979323846;
        static const double sqrt2 = 1.41421356237309504880;

        template <typename T> T sqrt(T x) { return ::std::sqrt(x); }
        template <typename T> T cos(T x) { return ::std::cos(x); }
        template <typename T> T cosh(T x) { return ::std::cosh(x); }
        template <typename T> T sin(T x) { return ::std::sin(x); }
        template <typename T> T sinh(T x) { return ::std::sinh(x); }
        template <typename T> T tan(T x) { return ::std::tan(x); }
        template <typename T> T tanh(T x) { return ::std::tanh(x); }
        template <typename T> T exp(T x) { return ::std::exp(x); }
        template <typename T> T log(T x) { return ::std::log(x); }
        template <typename T> T log10(T x) { return ::std::log10(x); }
        template <typename T> T pow(T x, T y) { return ::std::pow(x, y); }

        template <typename T>
        T deg_to_rad(T degrees)
        {
            return degrees * pi / 180.0;
        }

        template <typename T>
        T rad_to_deg(T radians)
        {
            return radians * 180.0 / pi;
        }
    }
}

#endif