Qwt User's Guide  6.1-rc3
 All Classes Functions Variables Typedefs Enumerations Enumerator Pages
qwt_math.h
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #ifndef QWT_MATH_H
11 #define QWT_MATH_H
12 
13 #include "qwt_global.h"
14 
15 #if defined(_MSC_VER)
16 /*
17  Microsoft says:
18 
19  Define _USE_MATH_DEFINES before including math.h to expose these macro
20  definitions for common math constants. These are placed under an #ifdef
21  since these commonly-defined names are not part of the C/C++ standards.
22 */
23 #define _USE_MATH_DEFINES 1
24 #endif
25 
26 #include <qmath.h>
27 #include "qwt_global.h"
28 
29 #ifndef LOG_MIN
30 
31 #define LOG_MIN 1.0e-100
32 #endif
33 
34 #ifndef LOG_MAX
35 
36 #define LOG_MAX 1.0e100
37 #endif
38 
39 QWT_EXPORT double qwtGetMin( const double *array, int size );
40 QWT_EXPORT double qwtGetMax( const double *array, int size );
41 
42 QWT_EXPORT double qwtNormalizeRadians( double radians );
43 QWT_EXPORT double qwtNormalizeDegrees( double degrees );
44 
57 inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
58 {
59  const double eps = qAbs( 1.0e-6 * intervalSize );
60 
61  if ( value2 - value1 > eps )
62  return -1;
63 
64  if ( value1 - value2 > eps )
65  return 1;
66 
67  return 0;
68 }
69 
70 
71 inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
72 {
73  return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
74 }
75 
76 inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
77 {
78  return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
79 }
80 
82 inline int qwtSign( double x )
83 {
84  if ( x > 0.0 )
85  return 1;
86  else if ( x < 0.0 )
87  return ( -1 );
88  else
89  return 0;
90 }
91 
93 inline double qwtSqr( double x )
94 {
95  return x * x;
96 }
97 
99 inline double qwtFastAtan( double x )
100 {
101  if ( x < -1.0 )
102  return -M_PI_2 - x / ( x * x + 0.28 );
103 
104  if ( x > 1.0 )
105  return M_PI_2 - x / ( x * x + 0.28 );
106 
107  return x / ( 1.0 + x * x * 0.28 );
108 }
109 
111 inline double qwtFastAtan2( double y, double x )
112 {
113  if ( x > 0 )
114  return qwtFastAtan( y / x );
115 
116  if ( x < 0 )
117  {
118  const double d = qwtFastAtan( y / x );
119  return ( y >= 0 ) ? d + M_PI : d - M_PI;
120  }
121 
122  if ( y < 0.0 )
123  return -M_PI_2;
124 
125  if ( y > 0.0 )
126  return M_PI_2;
127 
128  return 0.0;
129 }
130 
131 // Translate degrees into radians
132 inline double qwtRadians( double degrees )
133 {
134  return degrees * M_PI / 180.0;
135 }
136 
137 // Translate radians into degrees
138 inline double qwtDegrees( double degrees )
139 {
140  return degrees * 180.0 / M_PI;
141 }
142 
143 #endif