math.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Name: wx/math.h
  3. * Purpose: Declarations/definitions of common math functions
  4. * Author: John Labenski and others
  5. * Modified by:
  6. * Created: 02/02/03
  7. * Copyright: (c) John Labenski
  8. * Licence: wxWindows licence
  9. */
  10. /* THIS IS A C FILE, DON'T USE C++ FEATURES (IN PARTICULAR COMMENTS) IN IT */
  11. #ifndef _WX_MATH_H_
  12. #define _WX_MATH_H_
  13. #include "wx/defs.h"
  14. #include <math.h>
  15. #ifndef M_PI
  16. #define M_PI 3.1415926535897932384626433832795
  17. #endif
  18. /* Scaling factors for various unit conversions: 1 inch = 2.54 cm */
  19. #ifndef METRIC_CONVERSION_CONSTANT
  20. #define METRIC_CONVERSION_CONSTANT (1/25.4)
  21. #endif
  22. #ifndef mm2inches
  23. #define mm2inches (METRIC_CONVERSION_CONSTANT)
  24. #endif
  25. #ifndef inches2mm
  26. #define inches2mm (1/(mm2inches))
  27. #endif
  28. #ifndef mm2twips
  29. #define mm2twips (METRIC_CONVERSION_CONSTANT*1440)
  30. #endif
  31. #ifndef twips2mm
  32. #define twips2mm (1/(mm2twips))
  33. #endif
  34. #ifndef mm2pt
  35. #define mm2pt (METRIC_CONVERSION_CONSTANT*72)
  36. #endif
  37. #ifndef pt2mm
  38. #define pt2mm (1/(mm2pt))
  39. #endif
  40. #ifdef __cplusplus
  41. /* Any C++11 compiler should provide isfinite() */
  42. #if __cplusplus >= 201103
  43. #include <cmath>
  44. #define wxFinite(x) std::isfinite(x)
  45. #elif defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__)
  46. #include <float.h>
  47. #define wxFinite(x) _finite(x)
  48. #elif defined(__MINGW64_TOOLCHAIN__) || defined(__clang__)
  49. /*
  50. add more compilers with C99 support here: using C99 isfinite() is
  51. preferable to using BSD-ish finite()
  52. */
  53. #if defined(_GLIBCXX_CMATH) || defined(_LIBCPP_CMATH)
  54. // these <cmath> headers #undef isfinite
  55. #define wxFinite(x) std::isfinite(x)
  56. #else
  57. #define wxFinite(x) isfinite(x)
  58. #endif
  59. #elif ( defined(__GNUG__)||defined(__GNUWIN32__)||defined(__DJGPP__)|| \
  60. defined(__SGI_CC__)||defined(__SUNCC__)||defined(__XLC__)|| \
  61. defined(__HPUX__) ) && ( !defined(wxOSX_USE_IPHONE) || wxOSX_USE_IPHONE == 0 )
  62. #ifdef __SOLARIS__
  63. #include <ieeefp.h>
  64. #endif
  65. #define wxFinite(x) finite(x)
  66. #else
  67. #define wxFinite(x) ((x) == (x))
  68. #endif
  69. #if defined(__VISUALC__)||defined(__BORLAND__)
  70. #define wxIsNaN(x) _isnan(x)
  71. #elif defined(__GNUG__)||defined(__GNUWIN32__)||defined(__DJGPP__)|| \
  72. defined(__SGI_CC__)||defined(__SUNCC__)||defined(__XLC__)|| \
  73. defined(__HPUX__)
  74. #define wxIsNaN(x) isnan(x)
  75. #else
  76. #define wxIsNaN(x) ((x) != (x))
  77. #endif
  78. #ifdef __INTELC__
  79. inline bool wxIsSameDouble(double x, double y)
  80. {
  81. // VZ: this warning, given for operators==() and !=() is not wrong, as ==
  82. // shouldn't be used with doubles, but we get too many of them and
  83. // removing these operators is probably not a good idea
  84. //
  85. // Maybe we should always compare doubles up to some "epsilon" precision
  86. #pragma warning(push)
  87. // floating-point equality and inequality comparisons are unreliable
  88. #pragma warning(disable: 1572)
  89. return x == y;
  90. #pragma warning(pop)
  91. }
  92. #else /* !__INTELC__ */
  93. wxGCC_WARNING_SUPPRESS(float-equal)
  94. inline bool wxIsSameDouble(double x, double y) { return x == y; }
  95. wxGCC_WARNING_RESTORE(float-equal)
  96. #endif /* __INTELC__/!__INTELC__ */
  97. inline bool wxIsNullDouble(double x) { return wxIsSameDouble(x, 0.); }
  98. inline int wxRound(double x)
  99. {
  100. wxASSERT_MSG( x > INT_MIN - 0.5 && x < INT_MAX + 0.5,
  101. wxT("argument out of supported range") );
  102. #if defined(HAVE_ROUND)
  103. return int(round(x));
  104. #else
  105. return (int)(x < 0 ? x - 0.5 : x + 0.5);
  106. #endif
  107. }
  108. #endif /* __cplusplus */
  109. #if defined(__WINDOWS__) && !defined(__WXWINCE__)
  110. #define wxMulDivInt32( a , b , c ) ::MulDiv( a , b , c )
  111. #else
  112. #define wxMulDivInt32( a , b , c ) (wxRound((a)*(((wxDouble)b)/((wxDouble)c))))
  113. #endif
  114. #if wxUSE_APPLE_IEEE
  115. #ifdef __cplusplus
  116. extern "C" {
  117. #endif
  118. /* functions from common/extended.c */
  119. WXDLLIMPEXP_BASE wxFloat64 wxConvertFromIeeeExtended(const wxInt8 *bytes);
  120. WXDLLIMPEXP_BASE void wxConvertToIeeeExtended(wxFloat64 num, wxInt8 *bytes);
  121. /* use wxConvertFromIeeeExtended() and wxConvertToIeeeExtended() instead */
  122. #if WXWIN_COMPATIBILITY_2_8
  123. wxDEPRECATED( WXDLLIMPEXP_BASE wxFloat64 ConvertFromIeeeExtended(const wxInt8 *bytes) );
  124. wxDEPRECATED( WXDLLIMPEXP_BASE void ConvertToIeeeExtended(wxFloat64 num, wxInt8 *bytes) );
  125. #endif
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif /* wxUSE_APPLE_IEEE */
  130. #endif /* _WX_MATH_H_ */