WTF
MathExtras.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef WTF_MathExtras_h
00027 #define WTF_MathExtras_h
00028
00029 #include <math.h>
00030
00031 #if PLATFORM(WIN)
00032
00033 #include "kjs/operations.h"
00034 #include "kjs/value.h"
00035 #include <xmath.h>
00036 #include <limits>
00037
00038 #if HAVE(FLOAT_H)
00039 #include <float.h>
00040 #endif
00041
00042 #endif
00043
00044 #ifndef M_PI
00045 const double piDouble = 3.14159265358979323846;
00046 const float piFloat = 3.14159265358979323846f;
00047 #else
00048 const double piDouble = M_PI;
00049 const float piFloat = static_cast<float>(M_PI);
00050 #endif
00051
00052 #ifndef M_PI_4
00053 const double piOverFourDouble = 0.785398163397448309616;
00054 const float piOverFourFloat = 0.785398163397448309616f;
00055 #else
00056 const double piOverFourDouble = M_PI_4;
00057 const float piOverFourFloat = static_cast<float>(M_PI_4);
00058 #endif // !BUILDING_KDE__
00059
00060 #if COMPILER(MSVC)
00061
00062 #ifndef BUILDING_KDE__
00063 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
00064 inline bool isnan(double num) { return _isnan(num); }
00065 inline long lround(double num) { return num > 0 ? num + 0.5 : ceil(num - 0.5); }
00066 inline long lroundf(float num) { return num > 0 ? num + 0.5f : ceilf(num - 0.5f); }
00067 inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
00068 inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
00069 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
00070 #endif
00071
00072 #ifndef BUILDING_KDE__
00073
00074
00075 inline double wtf_atan2(double x, double y)
00076 {
00077 static double posInf = std::numeric_limits<double>::infinity();
00078 static double negInf = -std::numeric_limits<double>::infinity();
00079
00080 double result = KJS::NaN;
00081
00082 if (x == posInf && y == posInf)
00083 result = piOverFourDouble;
00084 else if (x == posInf && y == negInf)
00085 result = 3 * piOverFourDouble;
00086 else if (x == negInf && y == posInf)
00087 result = -piOverFourDouble;
00088 else if (x == negInf && y == negInf)
00089 result = -3 * piOverFourDouble;
00090 else
00091 result = ::atan2(x, y);
00092
00093 return result;
00094 }
00095 #else // !BUILDING_KDE__
00096
00097 #define wtf_atan2(x, y) atan2(x, y)
00098
00099 #endif // !BUILDING_KDE__
00100
00101 #if COMPILER(MSVC)
00102
00103
00104 inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
00105
00106 #define fmod(x, y) wtf_fmod(x, y)
00107
00108 #endif // #if COMPILER(MSVC)
00109
00110 #define atan2(x, y) wtf_atan2(x, y)
00111
00112 #endif // #if PLATFORM(WIN)
00113
00114 inline double deg2rad(double d) { return d * piDouble / 180.0; }
00115 inline double rad2deg(double r) { return r * 180.0 / piDouble; }
00116 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
00117 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
00118 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
00119 inline double grad2rad(double g) { return g * piDouble / 200.0; }
00120
00121 inline float deg2rad(float d) { return d * piFloat / 180.0f; }
00122 inline float rad2deg(float r) { return r * 180.0f / piFloat; }
00123 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
00124 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
00125 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
00126 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
00127
00128 #endif // #ifndef WTF_MathExtras_h