Newer
Older
Import / research / 3d-octrees / jMath.h
/*
 * =====================================================================================
 *
 *       Filename:  jMath.h
 *
 *    Description:  Various math related functions
 *
 *        Version:  1.0
 *        Created:  04/06/2011 07:42:30
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  John Ryland (jryland), jryland@xiaofrog.com
 *        Company:  InvertedLogic
 *
 * =====================================================================================
 */

#ifndef __J_MATH_H__
#define __J_MATH_H__


#include <jTypes.h>    // jFloat32 and jInt32
#include <jLimits.h>   // INT_MAX etc
#include <math.h>      // sqrt()
#include <float.h>     // FLT_MAX


// Math class
template <typename T>
class jMath
{
public:
    static T zero();
    static T identity();
    static T maxValue();
    static T minValue();
    static T squareRoot(const T& value);
};


template <>
class jMath<jInt32>
{
public:
    static jInt32 zero()            { return 0; }
    static jInt32 identity()        { return 1; }
    static jInt32 maxValue()        { return J_INT32_MAX; }
    static jInt32 minValue()        { return J_INT32_MIN; }
    static jInt32 squareRoot(const jInt32& value);
};


template <>
inline jInt32 jMath<jFloat32>::squareRoot(const jInt32& value)
{
    // XXX Could do much better here
    return jInt32(sqrt(jFloat32(value)));
    // Newton method
    //T valueA = maxValue<T>();
    //T valueB = minValue<T>();
}


template <>
class jMath<jFloat32>
{
public:
    static jFloat32 zero()          { return 0.0f; }
    static jFloat32 identity()      { return 1.0f; }
    static jFloat32 maxValue()      { return J_FLOAT32_MAX; }
    static jFloat32 minValue()      { return J_FLOAT32_MIN; }
    static jFloat32 squareRoot(const jFloat32& value);
};


template <>
inline jFloat32 jMath<jFloat32>::squareRoot(const jFloat32& value)
{
    return sqrt(value);
}


#endif // __J_MATH_H__