Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_math.h
1/******************************************************************************
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/*
16 Microsoft says:
17
18 Define _USE_MATH_DEFINES before including math.h to expose these macro
19 definitions for common math constants. These are placed under an #ifdef
20 since these commonly-defined names are not part of the C/C++ standards.
21 */
22
23#ifndef _USE_MATH_DEFINES
24#define _USE_MATH_DEFINES
25#define undef_USE_MATH_DEFINES
26#endif
27
28#include <cmath>
29
30#ifdef undef_USE_MATH_DEFINES
31#undef _USE_MATH_DEFINES
32#undef undef_USE_MATH_DEFINES
33#endif
34
35#ifndef M_E
36#define M_E ( 2.7182818284590452354 )
37#endif
38
39#ifndef M_LOG2E
40#define M_LOG2E ( 1.4426950408889634074 )
41#endif
42
43#ifndef M_LOG10E
44#define M_LOG10E ( 0.43429448190325182765 )
45#endif
46
47#ifndef M_LN2
48#define M_LN2 ( 0.69314718055994530942 )
49#endif
50
51#ifndef M_LN10
52#define M_LN10 ( 2.30258509299404568402 )
53#endif
54
55#ifndef M_PI
56#define M_PI ( 3.14159265358979323846 )
57#endif
58
59#ifndef M_PI_2
60#define M_PI_2 ( 1.57079632679489661923 )
61#endif
62
63#ifndef M_PI_4
64#define M_PI_4 ( 0.78539816339744830962 )
65#endif
66
67#ifndef M_1_PI
68#define M_1_PI ( 0.31830988618379067154 )
69#endif
70
71#ifndef M_2_PI
72#define M_2_PI ( 0.63661977236758134308 )
73#endif
74
75#ifndef M_2_SQRTPI
76#define M_2_SQRTPI ( 1.12837916709551257390 )
77#endif
78
79#ifndef M_SQRT2
80#define M_SQRT2 ( 1.41421356237309504880 )
81#endif
82
83#ifndef M_SQRT1_2
84#define M_SQRT1_2 ( 0.70710678118654752440 )
85#endif
86
87#if defined( QT_WARNING_PUSH )
88 /*
89 early Qt versions not having QT_WARNING_PUSH is full of warnings
90 so that we do not care of suppressing those from below
91 */
92 QT_WARNING_PUSH
93 QT_WARNING_DISABLE_CLANG("-Wdouble-promotion")
94 QT_WARNING_DISABLE_GCC("-Wdouble-promotion")
95#endif
96
97/*
98 On systems, where qreal is a float you often run into
99 compiler issues with qMin/qMax.
100 */
101
103QWT_CONSTEXPR inline float qwtMinF( float a, float b )
104{
105 return ( a < b ) ? a : b;
106}
107
109QWT_CONSTEXPR inline double qwtMinF( double a, double b )
110{
111 return ( a < b ) ? a : b;
112}
113
115QWT_CONSTEXPR inline qreal qwtMinF( float a, double b )
116{
117 return ( a < b ) ? a : b;
118}
119
121QWT_CONSTEXPR inline qreal qwtMinF( double a, float b )
122{
123 return ( a < b ) ? a : b;
124}
125
127QWT_CONSTEXPR inline float qwtMaxF( float a, float b )
128{
129 return ( a < b ) ? b : a;
130}
131
133QWT_CONSTEXPR inline double qwtMaxF( double a, double b )
134{
135 return ( a < b ) ? b : a;
136}
137
139QWT_CONSTEXPR inline qreal qwtMaxF( float a, double b )
140{
141 return ( a < b ) ? b : a;
142}
143
145QWT_CONSTEXPR inline qreal qwtMaxF( double a, float b )
146{
147 return ( a < b ) ? b : a;
148}
149
150#if defined( QT_WARNING_POP )
151 QT_WARNING_POP
152#endif
153
154QWT_EXPORT double qwtNormalizeRadians( double radians );
155QWT_EXPORT double qwtNormalizeDegrees( double degrees );
156QWT_EXPORT quint32 qwtRand();
157
170inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
171{
172 const double eps = qAbs( 1.0e-6 * intervalSize );
173
174 if ( value2 - value1 > eps )
175 return -1;
176
177 if ( value1 - value2 > eps )
178 return 1;
179
180 return 0;
181}
182
184inline int qwtSign( double x )
185{
186 if ( x > 0.0 )
187 return 1;
188 else if ( x < 0.0 )
189 return ( -1 );
190 else
191 return 0;
192}
193
195inline double qwtSqr( double x )
196{
197 return x * x;
198}
199
201inline double qwtFastAtan( double x )
202{
203 if ( x < -1.0 )
204 return -M_PI_2 - x / ( x * x + 0.28 );
205
206 if ( x > 1.0 )
207 return M_PI_2 - x / ( x * x + 0.28 );
208
209 return x / ( 1.0 + x * x * 0.28 );
210}
211
213inline double qwtFastAtan2( double y, double x )
214{
215 if ( x > 0 )
216 return qwtFastAtan( y / x );
217
218 if ( x < 0 )
219 {
220 const double d = qwtFastAtan( y / x );
221 return ( y >= 0 ) ? d + M_PI : d - M_PI;
222 }
223
224 if ( y < 0.0 )
225 return -M_PI_2;
226
227 if ( y > 0.0 )
228 return M_PI_2;
229
230 return 0.0;
231}
232
233/* !
234 \brief Calculate a value of a cubic polynomial
235
236 \param x Value
237 \param a Cubic coefficient
238 \param b Quadratic coefficient
239 \param c Linear coefficient
240 \param d Constant offset
241
242 \return Value of the polyonom for x
243 */
244inline double qwtCubicPolynomial( double x,
245 double a, double b, double c, double d )
246{
247 return ( ( ( a * x ) + b ) * x + c ) * x + d;
248}
249
251inline double qwtRadians( double degrees )
252{
253 return degrees * M_PI / 180.0;
254}
255
257inline double qwtDegrees( double degrees )
258{
259 return degrees * 180.0 / M_PI;
260}
261
266inline int qwtCeil( qreal value )
267{
268 using std::ceil;
269 return int( ceil( value ) );
270}
275inline int qwtFloor( qreal value )
276{
277 using std::floor;
278 return int( floor( value ) );
279}
280
281#endif