FPEnvironment.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // FPEnvironment.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: FPEnvironment
  7. //
  8. // Definitions of class FPEnvironment.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_FPEnvironment_INCLUDED
  16. #define Foundation_FPEnvironment_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #if defined(POCO_NO_FPENVIRONMENT)
  19. #include "Poco/FPEnvironment_DUMMY.h"
  20. #elif defined(__osf__)
  21. #include "Poco/FPEnvironment_DEC.h"
  22. #elif defined(sun) || defined(__sun)
  23. #include "Poco/FPEnvironment_SUN.h"
  24. #elif defined(__QNX__)
  25. #include "Poco/FPEnvironment_QNX.h"
  26. #elif defined(POCO_OS_FAMILY_UNIX)
  27. #include "Poco/FPEnvironment_C99.h"
  28. #elif defined(POCO_OS_FAMILY_WINDOWS)
  29. #include "Poco/FPEnvironment_WIN32.h"
  30. #else
  31. #include "Poco/FPEnvironment_DUMMY.h"
  32. #endif
  33. namespace Poco {
  34. class Foundation_API FPEnvironment: private FPEnvironmentImpl
  35. /// Instances of this class can be used to save
  36. /// and later restore the current floating
  37. /// point environment (consisting of rounding
  38. /// mode and floating-point flags).
  39. /// The class also provides various static
  40. /// methods to query certain properties
  41. /// of a floating-point number.
  42. {
  43. public:
  44. enum RoundingMode
  45. {
  46. FP_ROUND_DOWNWARD = FP_ROUND_DOWNWARD_IMPL,
  47. FP_ROUND_UPWARD = FP_ROUND_UPWARD_IMPL,
  48. FP_ROUND_TONEAREST = FP_ROUND_TONEAREST_IMPL,
  49. FP_ROUND_TOWARDZERO = FP_ROUND_TOWARDZERO_IMPL
  50. };
  51. enum Flag
  52. {
  53. FP_DIVIDE_BY_ZERO = FP_DIVIDE_BY_ZERO_IMPL,
  54. FP_INEXACT = FP_INEXACT_IMPL,
  55. FP_OVERFLOW = FP_OVERFLOW_IMPL,
  56. FP_UNDERFLOW = FP_UNDERFLOW_IMPL,
  57. FP_INVALID = FP_INVALID_IMPL
  58. };
  59. FPEnvironment();
  60. /// Standard constructor.
  61. /// Remembers the current environment.
  62. FPEnvironment(RoundingMode mode);
  63. /// Remembers the current environment and
  64. /// sets the given rounding mode.
  65. FPEnvironment(const FPEnvironment& env);
  66. /// Copy constructor.
  67. ~FPEnvironment();
  68. /// Restores the previous environment (unless
  69. /// keepCurrent() has been called previously)
  70. FPEnvironment& operator = (const FPEnvironment& env);
  71. /// Assignment operator
  72. void keepCurrent();
  73. /// Keep the current environment even after
  74. /// destroying the FPEnvironment object.
  75. static void clearFlags();
  76. /// Resets all flags.
  77. static bool isFlag(Flag flag);
  78. /// Returns true iff the given flag is set.
  79. static void setRoundingMode(RoundingMode mode);
  80. /// Sets the rounding mode.
  81. static RoundingMode getRoundingMode();
  82. /// Returns the current rounding mode.
  83. static bool isInfinite(float value);
  84. static bool isInfinite(double value);
  85. static bool isInfinite(long double value);
  86. /// Returns true iff the given number is infinite.
  87. static bool isNaN(float value);
  88. static bool isNaN(double value);
  89. static bool isNaN(long double value);
  90. /// Returns true iff the given number is NaN.
  91. static float copySign(float target, float source);
  92. static double copySign(double target, double source);
  93. static long double copySign(long double target, long double source);
  94. /// Copies the sign from source to target.
  95. };
  96. //
  97. // For convenience, we provide a shorter name for
  98. // the FPEnvironment class.
  99. //
  100. typedef FPEnvironment FPE;
  101. //
  102. // inline's
  103. //
  104. inline bool FPEnvironment::isFlag(Flag flag)
  105. {
  106. return isFlagImpl(FlagImpl(flag));
  107. }
  108. inline void FPEnvironment::setRoundingMode(RoundingMode mode)
  109. {
  110. setRoundingModeImpl(RoundingModeImpl(mode));
  111. }
  112. inline FPEnvironment::RoundingMode FPEnvironment::getRoundingMode()
  113. {
  114. return RoundingMode(getRoundingModeImpl());
  115. }
  116. inline bool FPEnvironment::isInfinite(float value)
  117. {
  118. return isInfiniteImpl(value);
  119. }
  120. inline bool FPEnvironment::isInfinite(double value)
  121. {
  122. return isInfiniteImpl(value);
  123. }
  124. inline bool FPEnvironment::isInfinite(long double value)
  125. {
  126. return isInfiniteImpl(value);
  127. }
  128. inline bool FPEnvironment::isNaN(float value)
  129. {
  130. return isNaNImpl(value);
  131. }
  132. inline bool FPEnvironment::isNaN(double value)
  133. {
  134. return isNaNImpl(value);
  135. }
  136. inline bool FPEnvironment::isNaN(long double value)
  137. {
  138. return isNaNImpl(value);
  139. }
  140. inline float FPEnvironment::copySign(float target, float source)
  141. {
  142. return copySignImpl(target, source);
  143. }
  144. inline double FPEnvironment::copySign(double target, double source)
  145. {
  146. return copySignImpl(target, source);
  147. }
  148. inline long double FPEnvironment::copySign(long double target, long double source)
  149. {
  150. return copySignImpl(target, source);
  151. }
  152. } // namespace Poco
  153. #endif // Foundation_FPEnvironment_INCLUDED