[4271] | 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 |
|
---|
[8127] | 29 | #ifndef M_PI_2
|
---|
| 30 | // For Qt <= 4.8.4 M_PI_2 is not known by MinGW-w64
|
---|
| 31 | // when compiling with -std=c++11
|
---|
| 32 | #define M_PI_2 (1.57079632679489661923)
|
---|
[4271] | 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #ifndef LOG_MIN
|
---|
[8127] | 36 | //! Minimum value for logarithmic scales
|
---|
[4271] | 37 | #define LOG_MIN 1.0e-100
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #ifndef LOG_MAX
|
---|
| 41 | //! Maximum value for logarithmic scales
|
---|
| 42 | #define LOG_MAX 1.0e100
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | QWT_EXPORT double qwtGetMin( const double *array, int size );
|
---|
| 46 | QWT_EXPORT double qwtGetMax( const double *array, int size );
|
---|
| 47 |
|
---|
[8127] | 48 | QWT_EXPORT double qwtNormalizeRadians( double radians );
|
---|
| 49 | QWT_EXPORT double qwtNormalizeDegrees( double degrees );
|
---|
| 50 |
|
---|
[4271] | 51 | /*!
|
---|
| 52 | \brief Compare 2 values, relative to an interval
|
---|
| 53 |
|
---|
| 54 | Values are "equal", when :
|
---|
| 55 | \f$\cdot value2 - value1 <= abs(intervalSize * 10e^{-6})\f$
|
---|
| 56 |
|
---|
| 57 | \param value1 First value to compare
|
---|
| 58 | \param value2 Second value to compare
|
---|
| 59 | \param intervalSize interval size
|
---|
| 60 |
|
---|
| 61 | \return 0: if equal, -1: if value2 > value1, 1: if value1 > value2
|
---|
| 62 | */
|
---|
| 63 | inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
|
---|
| 64 | {
|
---|
| 65 | const double eps = qAbs( 1.0e-6 * intervalSize );
|
---|
| 66 |
|
---|
| 67 | if ( value2 - value1 > eps )
|
---|
| 68 | return -1;
|
---|
| 69 |
|
---|
| 70 | if ( value1 - value2 > eps )
|
---|
| 71 | return 1;
|
---|
| 72 |
|
---|
| 73 | return 0;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
|
---|
| 78 | {
|
---|
| 79 | return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
|
---|
| 83 | {
|
---|
| 84 | return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //! Return the sign
|
---|
| 88 | inline int qwtSign( double x )
|
---|
| 89 | {
|
---|
| 90 | if ( x > 0.0 )
|
---|
| 91 | return 1;
|
---|
| 92 | else if ( x < 0.0 )
|
---|
| 93 | return ( -1 );
|
---|
| 94 | else
|
---|
| 95 | return 0;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | //! Return the square of a number
|
---|
| 99 | inline double qwtSqr( double x )
|
---|
| 100 | {
|
---|
| 101 | return x * x;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[8127] | 104 | //! Approximation of arc tangent ( error below 0,005 radians )
|
---|
| 105 | inline double qwtFastAtan( double x )
|
---|
| 106 | {
|
---|
| 107 | if ( x < -1.0 )
|
---|
| 108 | return -M_PI_2 - x / ( x * x + 0.28 );
|
---|
| 109 |
|
---|
| 110 | if ( x > 1.0 )
|
---|
| 111 | return M_PI_2 - x / ( x * x + 0.28 );
|
---|
| 112 |
|
---|
| 113 | return x / ( 1.0 + x * x * 0.28 );
|
---|
[4271] | 114 | }
|
---|
| 115 |
|
---|
[8127] | 116 | //! Approximation of arc tangent ( error below 0,005 radians )
|
---|
| 117 | inline double qwtFastAtan2( double y, double x )
|
---|
| 118 | {
|
---|
| 119 | if ( x > 0 )
|
---|
| 120 | return qwtFastAtan( y / x );
|
---|
| 121 |
|
---|
| 122 | if ( x < 0 )
|
---|
| 123 | {
|
---|
| 124 | const double d = qwtFastAtan( y / x );
|
---|
| 125 | return ( y >= 0 ) ? d + M_PI : d - M_PI;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if ( y < 0.0 )
|
---|
| 129 | return -M_PI_2;
|
---|
| 130 |
|
---|
| 131 | if ( y > 0.0 )
|
---|
| 132 | return M_PI_2;
|
---|
| 133 |
|
---|
| 134 | return 0.0;
|
---|
[4271] | 135 | }
|
---|
| 136 |
|
---|
[8127] | 137 | //! Translate degrees into radians
|
---|
| 138 | inline double qwtRadians( double degrees )
|
---|
| 139 | {
|
---|
| 140 | return degrees * M_PI / 180.0;
|
---|
[4271] | 141 | }
|
---|
| 142 |
|
---|
[8127] | 143 | //! Translate radians into degrees
|
---|
| 144 | inline double qwtDegrees( double degrees )
|
---|
| 145 | {
|
---|
| 146 | return degrees * 180.0 / M_PI;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[4271] | 149 | #endif
|
---|