• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

WTF

  • sources
  • kde-4.12
  • kdelibs
  • kjs
  • wtf
MathExtras.h
Go to the documentation of this file.
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 
31 #if PLATFORM(WIN)
32 
33 #include "kjs/operations.h"
34 #include "kjs/value.h"
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
45 const double piDouble = 3.14159265358979323846;
46 const float piFloat = 3.14159265358979323846f;
47 #else
48 const double piDouble = M_PI;
49 const float piFloat = static_cast<float>(M_PI);
50 #endif
51 
52 #ifndef M_PI_4
53 const double piOverFourDouble = 0.785398163397448309616;
54 const float piOverFourFloat = 0.785398163397448309616f;
55 #else
56 const double piOverFourDouble = M_PI_4;
57 const float piOverFourFloat = static_cast<float>(M_PI_4);
58 #endif // !BUILDING_KDE__
59 
60 #if COMPILER(MSVC)
61 
62 #ifndef BUILDING_KDE__
63  inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
64  inline bool isnan(double num) { return _isnan(num); }
65  inline long lround(double num) { return num > 0 ? num + 0.5 : ceil(num - 0.5); }
66  inline long lroundf(float num) { return num > 0 ? num + 0.5f : ceilf(num - 0.5f); }
67  inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
68  inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
69  inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
70 #endif
71 
72 #ifndef BUILDING_KDE__
73 // FIXME: where to get std::numeric_limits from?
74 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
75 inline double wtf_atan2(double x, double y)
76 {
77  static double posInf = std::numeric_limits<double>::infinity();
78  static double negInf = -std::numeric_limits<double>::infinity();
79 
80  double result = KJS::NaN;
81 
82  if (x == posInf && y == posInf)
83  result = piOverFourDouble;
84  else if (x == posInf && y == negInf)
85  result = 3 * piOverFourDouble;
86  else if (x == negInf && y == posInf)
87  result = -piOverFourDouble;
88  else if (x == negInf && y == negInf)
89  result = -3 * piOverFourDouble;
90  else
91  result = ::atan2(x, y);
92 
93  return result;
94 }
95 #else // !BUILDING_KDE__
96 
97 #define wtf_atan2(x, y) atan2(x, y)
98 
99 #endif // !BUILDING_KDE__
100 
101 #if COMPILER(MSVC)
102 
103 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
104 inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
105 
106 #define fmod(x, y) wtf_fmod(x, y)
107 
108 #endif // #if COMPILER(MSVC)
109 
110 #define atan2(x, y) wtf_atan2(x, y)
111 
112 #endif // #if PLATFORM(WIN)
113 
114 inline double deg2rad(double d) { return d * piDouble / 180.0; }
115 inline double rad2deg(double r) { return r * 180.0 / piDouble; }
116 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
117 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
118 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
119 inline double grad2rad(double g) { return g * piDouble / 200.0; }
120 
121 inline float deg2rad(float d) { return d * piFloat / 180.0f; }
122 inline float rad2deg(float r) { return r * 180.0f / piFloat; }
123 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
124 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
125 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
126 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
127 
128 #endif // #ifndef WTF_MathExtras_h
grad2deg
double grad2deg(double g)
Definition: MathExtras.h:117
piDouble
const double piDouble
Definition: MathExtras.h:45
piOverFourFloat
const float piOverFourFloat
Definition: MathExtras.h:54
grad2rad
double grad2rad(double g)
Definition: MathExtras.h:119
deg2rad
double deg2rad(double d)
Definition: MathExtras.h:114
piOverFourDouble
const double piOverFourDouble
Definition: MathExtras.h:53
rad2deg
double rad2deg(double r)
Definition: MathExtras.h:115
piFloat
const float piFloat
Definition: MathExtras.h:46
deg2grad
double deg2grad(double d)
Definition: MathExtras.h:116
rad2grad
double rad2grad(double r)
Definition: MathExtras.h:118
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal