qwt_math.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #ifndef QWT_MATH_H
  10. #define QWT_MATH_H
  11. #include "qwt_global.h"
  12. #if defined(_MSC_VER)
  13. /*
  14. Microsoft says:
  15. Define _USE_MATH_DEFINES before including math.h to expose these macro
  16. definitions for common math constants. These are placed under an #ifdef
  17. since these commonly-defined names are not part of the C/C++ standards.
  18. */
  19. #define _USE_MATH_DEFINES 1
  20. #endif
  21. #include <qmath.h>
  22. #include "qwt_global.h"
  23. #ifndef M_PI_2
  24. // For Qt <= 4.8.4 M_PI_2 is not known by MinGW-w64
  25. // when compiling with -std=c++11
  26. #define M_PI_2 (1.57079632679489661923)
  27. #endif
  28. #ifndef LOG_MIN
  29. //! Minimum value for logarithmic scales
  30. #define LOG_MIN 1.0e-100
  31. #endif
  32. #ifndef LOG_MAX
  33. //! Maximum value for logarithmic scales
  34. #define LOG_MAX 1.0e100
  35. #endif
  36. QWT_EXPORT double qwtGetMin( const double *array, int size );
  37. QWT_EXPORT double qwtGetMax( const double *array, int size );
  38. QWT_EXPORT double qwtNormalizeRadians( double radians );
  39. QWT_EXPORT double qwtNormalizeDegrees( double degrees );
  40. /*!
  41. \brief Compare 2 values, relative to an interval
  42. Values are "equal", when :
  43. \f$\cdot value2 - value1 <= abs(intervalSize * 10e^{-6})\f$
  44. \param value1 First value to compare
  45. \param value2 Second value to compare
  46. \param intervalSize interval size
  47. \return 0: if equal, -1: if value2 > value1, 1: if value1 > value2
  48. */
  49. inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
  50. {
  51. const double eps = qAbs( 1.0e-6 * intervalSize );
  52. if ( value2 - value1 > eps )
  53. return -1;
  54. if ( value1 - value2 > eps )
  55. return 1;
  56. return 0;
  57. }
  58. inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
  59. {
  60. return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
  61. }
  62. inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
  63. {
  64. return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
  65. }
  66. //! Return the sign
  67. inline int qwtSign( double x )
  68. {
  69. if ( x > 0.0 )
  70. return 1;
  71. else if ( x < 0.0 )
  72. return ( -1 );
  73. else
  74. return 0;
  75. }
  76. //! Return the square of a number
  77. inline double qwtSqr( double x )
  78. {
  79. return x * x;
  80. }
  81. //! Approximation of arc tangent ( error below 0,005 radians )
  82. inline double qwtFastAtan( double x )
  83. {
  84. if ( x < -1.0 )
  85. return -M_PI_2 - x / ( x * x + 0.28 );
  86. if ( x > 1.0 )
  87. return M_PI_2 - x / ( x * x + 0.28 );
  88. return x / ( 1.0 + x * x * 0.28 );
  89. }
  90. //! Approximation of arc tangent ( error below 0,005 radians )
  91. inline double qwtFastAtan2( double y, double x )
  92. {
  93. if ( x > 0 )
  94. return qwtFastAtan( y / x );
  95. if ( x < 0 )
  96. {
  97. const double d = qwtFastAtan( y / x );
  98. return ( y >= 0 ) ? d + M_PI : d - M_PI;
  99. }
  100. if ( y < 0.0 )
  101. return -M_PI_2;
  102. if ( y > 0.0 )
  103. return M_PI_2;
  104. return 0.0;
  105. }
  106. //! Translate degrees into radians
  107. inline double qwtRadians( double degrees )
  108. {
  109. return degrees * M_PI / 180.0;
  110. }
  111. //! Translate radians into degrees
  112. inline double qwtDegrees( double degrees )
  113. {
  114. return degrees * 180.0 / M_PI;
  115. }
  116. #endif