WTF
MathExtras.h
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 1. Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * 2. Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * 00013 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 00014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00015 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00016 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 00017 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00018 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00019 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00020 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00021 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00023 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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 // FIXME: where to get std::numeric_limits from? 00074 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values. 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 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x. 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
KDE 4.6 API Reference