1 | // |
---|
2 | // FPEnvironment.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/FPEnvironment.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: FPEnvironment |
---|
9 | // |
---|
10 | // Definitions of class FPEnvironment. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_FPEnvironment_INCLUDED |
---|
40 | #define Foundation_FPEnvironment_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | #if defined(POCO_NO_FPENVIRONMENT) |
---|
47 | #include "Poco/FPEnvironment_DUMMY.h" |
---|
48 | #elif defined(__osf__) || defined(__VMS) |
---|
49 | #include "Poco/FPEnvironment_DEC.h" |
---|
50 | #elif defined(sun) || defined(__sun) |
---|
51 | #include "Poco/FPEnvironment_SUN.h" |
---|
52 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
53 | #include "Poco/FPEnvironment_C99.h" |
---|
54 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
---|
55 | #include "Poco/FPEnvironment_WIN32.h" |
---|
56 | #else |
---|
57 | #include "Poco/FPEnvironment_DUMMY.h" |
---|
58 | #endif |
---|
59 | |
---|
60 | |
---|
61 | namespace Poco { |
---|
62 | |
---|
63 | |
---|
64 | class Foundation_API FPEnvironment: private FPEnvironmentImpl |
---|
65 | /// Instances of this class can be used to save |
---|
66 | /// and later restore the current floating |
---|
67 | /// point environment (consisting of rounding |
---|
68 | /// mode and floating-point flags). |
---|
69 | /// The class also provides various static |
---|
70 | /// methods to query certain properties |
---|
71 | /// of a floating-point number. |
---|
72 | { |
---|
73 | public: |
---|
74 | enum RoundingMode |
---|
75 | { |
---|
76 | FP_ROUND_DOWNWARD = FP_ROUND_DOWNWARD_IMPL, |
---|
77 | FP_ROUND_UPWARD = FP_ROUND_UPWARD_IMPL, |
---|
78 | FP_ROUND_TONEAREST = FP_ROUND_TONEAREST_IMPL, |
---|
79 | FP_ROUND_TOWARDZERO = FP_ROUND_TOWARDZERO_IMPL |
---|
80 | }; |
---|
81 | |
---|
82 | enum Flag |
---|
83 | { |
---|
84 | FP_DIVIDE_BY_ZERO = FP_DIVIDE_BY_ZERO_IMPL, |
---|
85 | FP_INEXACT = FP_INEXACT_IMPL, |
---|
86 | FP_OVERFLOW = FP_OVERFLOW_IMPL, |
---|
87 | FP_UNDERFLOW = FP_UNDERFLOW_IMPL, |
---|
88 | FP_INVALID = FP_INVALID_IMPL |
---|
89 | }; |
---|
90 | |
---|
91 | FPEnvironment(); |
---|
92 | /// Standard constructor. |
---|
93 | /// Remembers the current environment. |
---|
94 | |
---|
95 | FPEnvironment(RoundingMode mode); |
---|
96 | /// Remembers the current environment and |
---|
97 | /// sets the given rounding mode. |
---|
98 | |
---|
99 | FPEnvironment(const FPEnvironment& env); |
---|
100 | /// Copy constructor. |
---|
101 | |
---|
102 | ~FPEnvironment(); |
---|
103 | /// Restores the previous environment (unless |
---|
104 | /// keepCurrent() has been called previously) |
---|
105 | |
---|
106 | FPEnvironment& operator = (const FPEnvironment& env); |
---|
107 | /// Assignment operator |
---|
108 | |
---|
109 | void keepCurrent(); |
---|
110 | /// Keep the current environment even after |
---|
111 | /// destroying the FPEnvironment object. |
---|
112 | |
---|
113 | static void clearFlags(); |
---|
114 | /// Resets all flags. |
---|
115 | |
---|
116 | static bool isFlag(Flag flag); |
---|
117 | /// Returns true iff the given flag is set. |
---|
118 | |
---|
119 | static void setRoundingMode(RoundingMode mode); |
---|
120 | /// Sets the rounding mode. |
---|
121 | |
---|
122 | static RoundingMode getRoundingMode(); |
---|
123 | /// Returns the current rounding mode. |
---|
124 | |
---|
125 | static bool isInfinite(float value); |
---|
126 | static bool isInfinite(double value); |
---|
127 | static bool isInfinite(long double value); |
---|
128 | /// Returns true iff the given number is infinite. |
---|
129 | |
---|
130 | static bool isNaN(float value); |
---|
131 | static bool isNaN(double value); |
---|
132 | static bool isNaN(long double value); |
---|
133 | /// Returns true iff the given number is NaN. |
---|
134 | |
---|
135 | static float copySign(float target, float source); |
---|
136 | static double copySign(double target, double source); |
---|
137 | static long double copySign(long double target, long double source); |
---|
138 | /// Copies the sign from source to target. |
---|
139 | }; |
---|
140 | |
---|
141 | |
---|
142 | // |
---|
143 | // For convenience, we provide a shorter name for |
---|
144 | // the FPEnvironment class. |
---|
145 | // |
---|
146 | typedef FPEnvironment FPE; |
---|
147 | |
---|
148 | |
---|
149 | // |
---|
150 | // inline's |
---|
151 | // |
---|
152 | inline bool FPEnvironment::isFlag(Flag flag) |
---|
153 | { |
---|
154 | return isFlagImpl(FlagImpl(flag)); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | inline void FPEnvironment::setRoundingMode(RoundingMode mode) |
---|
159 | { |
---|
160 | setRoundingModeImpl(RoundingModeImpl(mode)); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | inline FPEnvironment::RoundingMode FPEnvironment::getRoundingMode() |
---|
165 | { |
---|
166 | return RoundingMode(getRoundingModeImpl()); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | inline bool FPEnvironment::isInfinite(float value) |
---|
171 | { |
---|
172 | return isInfiniteImpl(value); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | inline bool FPEnvironment::isInfinite(double value) |
---|
177 | { |
---|
178 | return isInfiniteImpl(value); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | inline bool FPEnvironment::isInfinite(long double value) |
---|
183 | { |
---|
184 | return isInfiniteImpl(value); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | inline bool FPEnvironment::isNaN(float value) |
---|
189 | { |
---|
190 | return isNaNImpl(value); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | inline bool FPEnvironment::isNaN(double value) |
---|
195 | { |
---|
196 | return isNaNImpl(value); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | inline bool FPEnvironment::isNaN(long double value) |
---|
201 | { |
---|
202 | return isNaNImpl(value); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | inline float FPEnvironment::copySign(float target, float source) |
---|
207 | { |
---|
208 | return copySignImpl(target, source); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | inline double FPEnvironment::copySign(double target, double source) |
---|
213 | { |
---|
214 | return copySignImpl(target, source); |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | inline long double FPEnvironment::copySign(long double target, long double source) |
---|
219 | { |
---|
220 | return copySignImpl(target, source); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | } // namespace Poco |
---|
225 | |
---|
226 | |
---|
227 | #endif // Foundation_FPEnvironment_INCLUDED |
---|