FMath.hpp 6.08 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11 12
#ifndef FMATH_HPP
#define FMATH_HPP
13

14

15
#include <cmath>
16
#include <limits>
17
#include <limits>
18

19 20 21 22
#include "FGlobal.hpp"

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
berenger-bramas's avatar
berenger-bramas committed
23
* @class
24 25 26 27 28
* Please read the license
*
* Propose basic math functions or indirections to std math.
*/
struct FMath{
29 30 31
    static const FReal FPi;        //< Pi constant
    static const FReal FPiDiv2;    //< Pi/2 constant
    static const FReal Epsilon;    //< Epsilon
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    /** To get absolute value */
    template <class NumType>
    static NumType Abs(const NumType inV){
        return (inV < 0 ? -inV : inV);
    }

    /** To get max between 2 values */
    template <class NumType>
    static NumType Max(const NumType inV1, const NumType inV2){
        return (inV1 > inV2 ? inV1 : inV2);
    }

    /** To get min between 2 values */
    template <class NumType>
    static NumType Min(const NumType inV1, const NumType inV2){
        return (inV1 < inV2 ? inV1 : inV2);
    }

    /** To know if 2 values seems to be equal */
    template <class NumType>
    static bool LookEqual(const NumType inV1, const NumType inV2){
54
        return (Abs(inV1-inV2) < std::numeric_limits<NumType>::epsilon());
55 56 57
        //const FReal relTol = FReal(0.00001);
        //const FReal absTol = FReal(0.00001);
        //return (Abs(inV1 - inV2) <= Max(absTol, relTol * Max(Abs(inV1), Abs(inV2))));
58 59
    }

60 61 62 63 64 65
    /** To know if 2 values seems to be equal */
    template <class NumType>
    static FReal RelatifDiff(const NumType inV1, const NumType inV2){
        return Abs(inV1 - inV2)*Abs(inV1 - inV2)/Max(Abs(inV1*inV1), Abs(inV2*inV2));
    }

66
    /** To get floor of a FReal */
67 68 69 70
    static float dfloor(const float inValue){
        return floorf(inValue);
    }
    static double dfloor(const double inValue){
71 72 73
        return floor(inValue);
    }

berenger-bramas's avatar
berenger-bramas committed
74
    /** To get ceil of a FReal */
75 76 77 78
    static float Ceil(const float inValue){
        return ceilf(inValue);
    }
    static double Ceil(const double inValue){
berenger-bramas's avatar
berenger-bramas committed
79 80 81
        return ceil(inValue);
    }

82 83
    /** To get pow */
    template <class NumType>
84
    static NumType pow(const NumType inValue, int power){
85 86 87 88 89
        NumType result = 1;
        while(power-- > 0) result *= inValue;
        return result;
    }

90 91 92 93 94
    /** To get pow of 2 */
    static int pow2(const int power){
        return (1 << power);
    }

95 96 97 98 99 100 101
    /** To know if a value is between two others */
    template <class NumType>
    static bool Between(const NumType inValue, const NumType inMin, const NumType inMax){
        return ( inMin <= inValue && inValue < inMax );
    }

    /** To get sqrt of a FReal */
102 103 104 105
    static float Sqrt(const float inValue){
        return sqrtf(inValue);
    }
    static double Sqrt(const double inValue){
106 107 108
        return sqrt(inValue);
    }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /** To get Log of a FReal */
    static float Log(const float inValue){
        return logf(inValue);
    }
    static double Log(const double inValue){
        return log(inValue);
    }

    /** To get Log2 of a FReal */
    static float Log2(const float inValue){
        return log2f(inValue);
    }
    static double Log2(const double inValue){
        return log2(inValue);
    }

125
    /** To get atan2 of a 2 FReal */
126 127 128 129
    static float Atan2(const float inValue1,const float inValue2){
        return atan2f(inValue1,inValue2);
    }
    static double Atan2(const double inValue1,const double inValue2){
130 131 132
        return atan2(inValue1,inValue2);
    }

COULAUD Olivier's avatar
COULAUD Olivier committed
133
    /** To get sin of a FReal */
134 135 136 137
    static float Sin(const float inValue){
        return sinf(inValue);
    }
    static double Sin(const double inValue){
138 139 140 141
        return sin(inValue);
    }

    /** To get cos of a FReal */
142 143 144 145
    static float Cos(const float inValue){
        return cosf(inValue);
    }
    static double Cos(const double inValue){
146 147 148 149
        return cos(inValue);
    }

    /** To get acos of a FReal */
150 151 152 153
    static float ACos(const float inValue){
        return acosf(inValue);
    }
    static double ACos(const double inValue){
154 155 156 157
        return acos(inValue);
    }

    /** To get atan2 of a 2 FReal */
158 159 160 161
    static float Fmod(const float inValue1,const float inValue2){
        return fmodf(inValue1,inValue2);
    }
    static double Fmod(const double inValue1,const double inValue2){
162 163
        return fmod(inValue1,inValue2);
    }
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    /** To know if a variable is nan, based on the C++0x */
    template <class TestClass>
    static bool IsNan(const TestClass& value){
        //volatile const TestClass* const pvalue = &value;
        //return (*pvalue) != value;
        return std::isnan(value);
    }

    /** To know if a variable is not inf, based on the C++0x */
    template <class TestClass>
    static bool IsFinite(const TestClass& value){
        // return !(value <= std::numeric_limits<T>::min()) && !(std::numeric_limits<T>::max() <= value);
        return std::isfinite(value);
    }
berenger-bramas's avatar
berenger-bramas committed
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    /** A class to compute accuracy */
    class FAccurater {
        FReal l2Dot;
        FReal l2Diff;
        FReal max;
        FReal maxDiff;
    public:
        FAccurater() : l2Dot(0), l2Diff(0), max(0), maxDiff(0) {
        }
        /** Add value to the current list */
        void add(const FReal inGood, const FReal inBad){
            l2Diff += (inBad - inGood) * (inBad - inGood);
            l2Dot  += inGood * inGood;

            max = Max(max , Abs(inGood));
            maxDiff = Max(maxDiff, Abs(inGood-inBad));
        }
        /** Get the L2 norm */
        FReal getL2Norm() const{
            return Sqrt(l2Diff / l2Dot);
        }
        /** Get the inf norm */
        FReal getInfNorm() const{
            return maxDiff / max;
        }
    };
207 208 209 210
};


#endif //FMATH_HPP