| 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 | |
| 16 | |
| 17 | #ifndef Foundation_FPEnvironment_INCLUDED |
| 18 | #define Foundation_FPEnvironment_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | |
| 23 | |
| 24 | #if defined(POCO_NO_FPENVIRONMENT) |
| 25 | #include "Poco/FPEnvironment_DUMMY.h" |
| 26 | #elif defined(__osf__) || defined(__VMS) |
| 27 | #include "Poco/FPEnvironment_DEC.h" |
| 28 | #elif defined(sun) || defined(__sun) |
| 29 | #include "Poco/FPEnvironment_SUN.h" |
| 30 | #elif defined(__QNX__) |
| 31 | #include "Poco/FPEnvironment_QNX.h" |
| 32 | #elif defined(POCO_OS_FAMILY_UNIX) |
| 33 | #include "Poco/FPEnvironment_C99.h" |
| 34 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
| 35 | #include "Poco/FPEnvironment_WIN32.h" |
| 36 | #else |
| 37 | #include "Poco/FPEnvironment_DUMMY.h" |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | namespace Poco { |
| 42 | |
| 43 | |
| 44 | class Foundation_API FPEnvironment: private FPEnvironmentImpl |
| 45 | /// Instances of this class can be used to save |
| 46 | /// and later restore the current floating |
| 47 | /// point environment (consisting of rounding |
| 48 | /// mode and floating-point flags). |
| 49 | /// The class also provides various static |
| 50 | /// methods to query certain properties |
| 51 | /// of a floating-point number. |
| 52 | { |
| 53 | public: |
| 54 | enum RoundingMode |
| 55 | { |
| 56 | FP_ROUND_DOWNWARD = FP_ROUND_DOWNWARD_IMPL, |
| 57 | FP_ROUND_UPWARD = FP_ROUND_UPWARD_IMPL, |
| 58 | FP_ROUND_TONEAREST = FP_ROUND_TONEAREST_IMPL, |
| 59 | FP_ROUND_TOWARDZERO = FP_ROUND_TOWARDZERO_IMPL |
| 60 | }; |
| 61 | |
| 62 | enum Flag |
| 63 | { |
| 64 | FP_DIVIDE_BY_ZERO = FP_DIVIDE_BY_ZERO_IMPL, |
| 65 | FP_INEXACT = FP_INEXACT_IMPL, |
| 66 | FP_OVERFLOW = FP_OVERFLOW_IMPL, |
| 67 | FP_UNDERFLOW = FP_UNDERFLOW_IMPL, |
| 68 | FP_INVALID = FP_INVALID_IMPL |
| 69 | }; |
| 70 | |
| 71 | FPEnvironment(); |
| 72 | /// Standard constructor. |
| 73 | /// Remembers the current environment. |
| 74 | |
| 75 | FPEnvironment(RoundingMode mode); |
| 76 | /// Remembers the current environment and |
| 77 | /// sets the given rounding mode. |
| 78 | |
| 79 | FPEnvironment(const FPEnvironment& env); |
| 80 | /// Copy constructor. |
| 81 | |
| 82 | ~FPEnvironment(); |
| 83 | /// Restores the previous environment (unless |
| 84 | /// keepCurrent() has been called previously) |
| 85 | |
| 86 | FPEnvironment& operator = (const FPEnvironment& env); |
| 87 | /// Assignment operator |
| 88 | |
| 89 | void keepCurrent(); |
| 90 | /// Keep the current environment even after |
| 91 | /// destroying the FPEnvironment object. |
| 92 | |
| 93 | static void clearFlags(); |
| 94 | /// Resets all flags. |
| 95 | |
| 96 | static bool isFlag(Flag flag); |
| 97 | /// Returns true iff the given flag is set. |
| 98 | |
| 99 | static void setRoundingMode(RoundingMode mode); |
| 100 | /// Sets the rounding mode. |
| 101 | |
| 102 | static RoundingMode getRoundingMode(); |
| 103 | /// Returns the current rounding mode. |
| 104 | |
| 105 | static bool isInfinite(float value); |
| 106 | static bool isInfinite(double value); |
| 107 | static bool isInfinite(long double value); |
| 108 | /// Returns true iff the given number is infinite. |
| 109 | |
| 110 | static bool isNaN(float value); |
| 111 | static bool isNaN(double value); |
| 112 | static bool isNaN(long double value); |
| 113 | /// Returns true iff the given number is NaN. |
| 114 | |
| 115 | static float copySign(float target, float source); |
| 116 | static double copySign(double target, double source); |
| 117 | static long double copySign(long double target, long double source); |
| 118 | /// Copies the sign from source to target. |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | // |
| 123 | // For convenience, we provide a shorter name for |
| 124 | // the FPEnvironment class. |
| 125 | // |
| 126 | typedef FPEnvironment FPE; |
| 127 | |
| 128 | |
| 129 | // |
| 130 | // inline's |
| 131 | // |
| 132 | inline bool FPEnvironment::isFlag(Flag flag) |
| 133 | { |
| 134 | return isFlagImpl(FlagImpl(flag)); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | inline void FPEnvironment::setRoundingMode(RoundingMode mode) |
| 139 | { |
| 140 | setRoundingModeImpl(RoundingModeImpl(mode)); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | inline FPEnvironment::RoundingMode FPEnvironment::getRoundingMode() |
| 145 | { |
| 146 | return RoundingMode(getRoundingModeImpl()); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | inline bool FPEnvironment::isInfinite(float value) |
| 151 | { |
| 152 | return isInfiniteImpl(value); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | inline bool FPEnvironment::isInfinite(double value) |
| 157 | { |
| 158 | return isInfiniteImpl(value); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | inline bool FPEnvironment::isInfinite(long double value) |
| 163 | { |
| 164 | return isInfiniteImpl(value); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | inline bool FPEnvironment::isNaN(float value) |
| 169 | { |
| 170 | return isNaNImpl(value); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | inline bool FPEnvironment::isNaN(double value) |
| 175 | { |
| 176 | return isNaNImpl(value); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | inline bool FPEnvironment::isNaN(long double value) |
| 181 | { |
| 182 | return isNaNImpl(value); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | inline float FPEnvironment::copySign(float target, float source) |
| 187 | { |
| 188 | return copySignImpl(target, source); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | inline double FPEnvironment::copySign(double target, double source) |
| 193 | { |
| 194 | return copySignImpl(target, source); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | inline long double FPEnvironment::copySign(long double target, long double source) |
| 199 | { |
| 200 | return copySignImpl(target, source); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | } // namespace Poco |
| 205 | |
| 206 | |
| 207 | #endif // Foundation_FPEnvironment_INCLUDED |
| 208 | |