source: webkit/trunk/JavaScriptCore/wtf/MathExtras.h@ 27885

Last change on this file since 27885 was 27614, checked in by Adam Roben, 18 years ago

Stop using KJS inside of MathExtras.h

Reviewed by Darin.

  • wtf/MathExtras.h: Removed an unused header, and a now-unused forward-declaration. (wtf_atan2): Use std::numeric_limits intead of KJS.
File size: 5.6 KB
Line 
1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WTF_MathExtras_h
27#define WTF_MathExtras_h
28
29#include <math.h>
30#include <stdlib.h>
31#include <time.h>
32
33#if COMPILER(MSVC)
34
35#include <xmath.h>
36#include <limits>
37
38#if HAVE(FLOAT_H)
39#include <float.h>
40#endif
41
42#endif
43
44#ifndef M_PI
45const double piDouble = 3.14159265358979323846;
46const float piFloat = 3.14159265358979323846f;
47#else
48const double piDouble = M_PI;
49const float piFloat = static_cast<float>(M_PI);
50#endif
51
52#ifndef M_PI_4
53const double piOverFourDouble = 0.785398163397448309616;
54const float piOverFourFloat = 0.785398163397448309616f;
55#else
56const double piOverFourDouble = M_PI_4;
57const float piOverFourFloat = static_cast<float>(M_PI_4);
58#endif
59
60#if COMPILER(MSVC)
61
62inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
63inline bool isnan(double num) { return !!_isnan(num); }
64inline long lround(double num) { return static_cast<long>(num > 0 ? num + 0.5 : ceil(num - 0.5)); }
65inline long lroundf(float num) { return static_cast<long>(num > 0 ? num + 0.5f : ceilf(num - 0.5f)); }
66inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
67inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
68inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
69inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
70
71inline double nextafter(double x, double y) { return _nextafter(x, y); }
72inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
73
74inline double copysign(double x, double y) { return _copysign(x, y); }
75inline int isfinite(double x) { return _finite(x); }
76
77// Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
78inline double wtf_atan2(double x, double y)
79{
80 static double posInf = std::numeric_limits<double>::infinity();
81 static double negInf = -std::numeric_limits<double>::infinity();
82 static double nan = std::numeric_limits<double>::quiet_NaN();
83
84 double result = nan;
85
86 if (x == posInf && y == posInf)
87 result = piOverFourDouble;
88 else if (x == posInf && y == negInf)
89 result = 3 * piOverFourDouble;
90 else if (x == negInf && y == posInf)
91 result = -piOverFourDouble;
92 else if (x == negInf && y == negInf)
93 result = -3 * piOverFourDouble;
94 else
95 result = ::atan2(x, y);
96
97 return result;
98}
99
100// Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
101inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
102
103// Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
104inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
105
106#define atan2(x, y) wtf_atan2(x, y)
107#define fmod(x, y) wtf_fmod(x, y)
108#define pow(x, y) wtf_pow(x, y)
109
110#if defined(_CRT_RAND_S)
111// Initializes the random number generator.
112inline void wtf_random_init()
113{
114 // No need to initialize for rand_s.
115}
116
117// Returns a pseudo-random number in the range [0, 1).
118inline double wtf_random()
119{
120 unsigned u;
121 rand_s(&u);
122
123 return static_cast<double>(u) / (static_cast<double>(UINT_MAX) + 1.0);
124}
125#endif // _CRT_RAND_S
126
127#else
128
129// Initializes the random number generator.
130inline void wtf_random_init()
131{
132 srand(static_cast<unsigned>(time(0)));
133}
134
135// Returns a pseudo-random number in the range [0, 1).
136inline double wtf_random()
137{
138 return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
139}
140
141#endif // #if COMPILER(MSVC)
142
143inline double deg2rad(double d) { return d * piDouble / 180.0; }
144inline double rad2deg(double r) { return r * 180.0 / piDouble; }
145inline double deg2grad(double d) { return d * 400.0 / 360.0; }
146inline double grad2deg(double g) { return g * 360.0 / 400.0; }
147inline double rad2grad(double r) { return r * 200.0 / piDouble; }
148inline double grad2rad(double g) { return g * piDouble / 200.0; }
149
150inline float deg2rad(float d) { return d * piFloat / 180.0f; }
151inline float rad2deg(float r) { return r * 180.0f / piFloat; }
152inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
153inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
154inline float rad2grad(float r) { return r * 200.0f / piFloat; }
155inline float grad2rad(float g) { return g * piFloat / 200.0f; }
156
157#endif // #ifndef WTF_MathExtras_h
Note: See TracBrowser for help on using the repository browser.