b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | #ifndef _Standard_Real_HeaderFile |
16 | #define _Standard_Real_HeaderFile |
17 | |
18 | #include <float.h> |
19 | #include <math.h> |
ebc93ae7 |
20 | #include <Standard_values.h> |
21 | #include <Standard_math.hxx> |
7fd59977 |
22 | #include <Standard_TypeDef.hxx> |
7fd59977 |
23 | |
7fd59977 |
24 | // =============================================== |
25 | // Methods from Standard_Entity class which are redefined: |
26 | // - Hascode |
27 | // - IsEqual |
7fd59977 |
28 | // =============================================== |
29 | |
30 | // ================================== |
31 | // Methods implemeted in Standard_Real.cxx |
32 | // ================================== |
7fd59977 |
33 | __Standard_API Standard_Integer HashCode (const Standard_Real, const Standard_Integer); |
7fd59977 |
34 | |
35 | __Standard_API Standard_Real ACos (const Standard_Real ); |
36 | __Standard_API Standard_Real ACosApprox (const Standard_Real ); |
37 | __Standard_API Standard_Real ASin (const Standard_Real ); |
38 | __Standard_API Standard_Real ATan2 (const Standard_Real , const Standard_Real ); |
39 | __Standard_API Standard_Real NextAfter (const Standard_Real , const Standard_Real ); |
40 | __Standard_API Standard_Real Sign (const Standard_Real , const Standard_Real ); |
41 | __Standard_API Standard_Real ATanh (const Standard_Real ); |
42 | __Standard_API Standard_Real ACosh (const Standard_Real ); |
43 | __Standard_API Standard_Real Log (const Standard_Real ); |
44 | __Standard_API Standard_Real Sqrt (const Standard_Real ); |
45 | |
7fd59977 |
46 | //------------------------------------------------------------------- |
47 | // RealSmall : Returns the smallest positive real |
48 | //------------------------------------------------------------------- |
49 | inline Standard_Real RealSmall() |
50 | { return DBL_MIN; } |
51 | |
52 | //------------------------------------------------------------------- |
53 | // Abs : Returns the absolute value of a real |
54 | //------------------------------------------------------------------- |
55 | inline Standard_Real Abs(const Standard_Real Value) |
56 | { return fabs(Value); } |
57 | |
58 | |
59 | //------------------------------------------------------------------- |
60 | // IsEqual : Returns Standard_True if two reals are equal |
61 | //------------------------------------------------------------------- |
62 | inline Standard_Boolean IsEqual (const Standard_Real Value1, |
63 | const Standard_Real Value2) |
64 | { return Abs((Value1 - Value2)) < RealSmall(); } |
65 | |
7fd59977 |
66 | // *********************************** // |
67 | // Class methods // |
68 | // // |
69 | // Machine-dependant values // |
70 | // Should be taken from include file // |
71 | // *********************************** // |
72 | |
73 | |
74 | //------------------------------------------------------------------- |
75 | // RealDigit : Returns the number of digits of precision in a real |
76 | //------------------------------------------------------------------- |
77 | inline Standard_Integer RealDigits() |
78 | { return DBL_DIG; } |
79 | |
80 | //------------------------------------------------------------------- |
81 | // RealEpsilon : Returns the minimum positive real such that |
82 | // 1.0 + x is not equal to 1.0 |
83 | //------------------------------------------------------------------- |
84 | inline Standard_Real RealEpsilon() |
85 | { return DBL_EPSILON; } |
86 | |
87 | //------------------------------------------------------------------- |
88 | // RealFirst : Returns the minimum negative value of a real |
89 | //------------------------------------------------------------------- |
90 | inline Standard_Real RealFirst() |
91 | { return -DBL_MAX; } |
92 | |
93 | //------------------------------------------------------------------- |
94 | // RealFirst10Exp : Returns the minimum value of exponent(base 10) of |
95 | // a real. |
96 | //------------------------------------------------------------------- |
97 | inline Standard_Integer RealFirst10Exp() |
98 | { return DBL_MIN_10_EXP; } |
99 | |
100 | //------------------------------------------------------------------- |
101 | // RealLast : Returns the maximum value of a real |
102 | //------------------------------------------------------------------- |
103 | inline Standard_Real RealLast() |
104 | { return DBL_MAX; } |
105 | |
106 | //------------------------------------------------------------------- |
107 | // RealLast10Exp : Returns the maximum value of exponent(base 10) of |
108 | // a real. |
109 | //------------------------------------------------------------------- |
110 | inline Standard_Integer RealLast10Exp() |
111 | { return DBL_MAX_10_EXP; } |
112 | |
113 | //------------------------------------------------------------------- |
114 | // RealMantissa : Returns the size in bits of the matissa part of a |
115 | // real. |
116 | //------------------------------------------------------------------- |
117 | inline Standard_Integer RealMantissa() |
118 | { return DBL_MANT_DIG; } |
119 | |
120 | //------------------------------------------------------------------- |
121 | // RealRadix : Returns the radix of exponent representation |
122 | //------------------------------------------------------------------- |
123 | inline Standard_Integer RealRadix() |
124 | { return FLT_RADIX; } |
125 | |
126 | //------------------------------------------------------------------- |
127 | // RealSize : Returns the size in bits of an integer |
128 | //------------------------------------------------------------------- |
129 | inline Standard_Integer RealSize() |
130 | { return BITS(Standard_Real); } |
131 | |
132 | |
133 | |
134 | //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// |
135 | // End of machine-dependant values // |
136 | //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// |
137 | |
138 | |
139 | //------------------------------------------------------------------- |
140 | // IntToReal : Converts an integer in a real |
141 | //------------------------------------------------------------------- |
142 | inline Standard_Real IntToReal(const Standard_Integer Value) |
143 | { return Value; } |
144 | |
145 | //------------------------------------------------------------------- |
146 | // ATan : Returns the value of the arc tangent of a real |
147 | //------------------------------------------------------------------- |
148 | inline Standard_Real ATan(const Standard_Real Value) |
149 | { return atan(Value); } |
150 | |
151 | |
152 | //------------------------------------------------------------------- |
153 | // Ceiling : Returns the smallest integer not less than a real |
154 | //------------------------------------------------------------------- |
155 | inline Standard_Real Ceiling (const Standard_Real Value) |
156 | { return ceil(Value); } |
157 | |
158 | //------------------------------------------------------------------- |
159 | // Cos : Returns the cosine of a real |
160 | //------------------------------------------------------------------- |
161 | inline Standard_Real Cos (const Standard_Real Value) |
162 | { return cos(Value); } |
163 | |
164 | //------------------------------------------------------------------- |
165 | // Cosh : Returns the hyperbolic cosine of a real |
166 | //------------------------------------------------------------------- |
167 | inline Standard_Real Cosh (const Standard_Real Value) |
168 | { return cosh(Value); } |
169 | |
170 | |
171 | //------------------------------------------------------------------- |
be53be81 |
172 | // Epsilon : The function returns absolute value of difference |
173 | // between 'Value' and other nearest value of |
174 | // Standard_Real type. |
175 | // Nearest value is choseen in direction of infinity |
176 | // the same sign as 'Value'. |
177 | // If 'Value' is 0 then returns minimal positive value |
178 | // of Standard_Real type. |
7fd59977 |
179 | //------------------------------------------------------------------- |
180 | inline Standard_Real Epsilon (const Standard_Real Value) |
181 | { |
182 | Standard_Real aEpsilon; |
183 | |
184 | if (Value>=0.0){ |
185 | aEpsilon = NextAfter(Value, RealLast()) - Value; |
186 | } else { |
187 | aEpsilon = Value - NextAfter(Value, RealFirst()); |
188 | } |
189 | return aEpsilon; |
190 | } |
191 | |
192 | //------------------------------------------------------------------- |
193 | // Exp : Returns the exponential function of a real |
194 | //------------------------------------------------------------------- |
195 | inline Standard_Real Exp (const Standard_Real Value) |
196 | { return exp(Value); } |
197 | |
198 | //------------------------------------------------------------------- |
199 | // Floor : Return the largest integer not greater than a real |
200 | //------------------------------------------------------------------- |
201 | inline Standard_Real Floor (const Standard_Real Value) |
202 | { return floor(Value); } |
203 | |
204 | //------------------------------------------------------------------- |
205 | // IntegerPart : Returns the integer part of a real |
206 | //------------------------------------------------------------------- |
207 | inline Standard_Real IntegerPart (const Standard_Real Value) |
208 | { return ( (Value>0) ? floor(Value) : ceil(Value) ); } |
209 | |
210 | |
211 | //------------------------------------------------------------------- |
212 | // Log10 : Returns the base-10 logarithm of a real |
213 | //------------------------------------------------------------------- |
214 | inline Standard_Real Log10 (const Standard_Real Value) |
215 | { return log10(Value); } |
216 | |
217 | //------------------------------------------------------------------- |
218 | // Max : Returns the maximum value of two reals |
219 | //------------------------------------------------------------------- |
220 | inline Standard_Real Max (const Standard_Real Val1, |
221 | const Standard_Real Val2) |
222 | { |
223 | return Val1 >= Val2 ? Val1 : Val2; |
224 | } |
225 | |
226 | //------------------------------------------------------------------- |
227 | // Min : Returns the minimum value of two reals |
228 | //------------------------------------------------------------------- |
229 | inline Standard_Real Min (const Standard_Real Val1, |
230 | const Standard_Real Val2) |
231 | { |
232 | return Val1 <= Val2 ? Val1 : Val2; |
233 | } |
234 | |
235 | //------------------------------------------------------------------- |
236 | // Pow : Returns a real to a given power |
237 | //------------------------------------------------------------------- |
238 | inline Standard_Real Pow (const Standard_Real Value, const Standard_Real P) |
239 | { return pow(Value,P); } |
240 | |
241 | //------------------------------------------------------------------- |
242 | // RealPart : Returns the fractional part of a real. |
243 | //------------------------------------------------------------------- |
244 | inline Standard_Real RealPart (const Standard_Real Value) |
245 | { return fabs(IntegerPart(Value) - Value); } |
246 | |
247 | //------------------------------------------------------------------- |
248 | // RealToInt : Returns the real converted to nearest valid integer. |
249 | // If input value is out of valid range for integers, |
250 | // minimal or maximal possible integer is returned. |
251 | //------------------------------------------------------------------- |
252 | inline Standard_Integer RealToInt (const Standard_Real Value) |
253 | { |
254 | // Note that on WNT under MS VC++ 8.0 conversion of double value less |
255 | // than INT_MIN or greater than INT_MAX to integer will cause signal |
256 | // "Floating point multiple trap" (OCC17861) |
257 | return Value < INT_MIN ? INT_MIN |
258 | : Value > INT_MAX ? INT_MAX |
259 | : (Standard_Integer)Value; |
260 | } |
261 | |
b7cd4ba7 |
262 | // ======================================================================= |
263 | // function : RealToShortReal |
264 | // purpose : Converts Standard_Real value to the nearest valid |
265 | // Standard_ShortReal. If input value is out of valid range |
266 | // for Standard_ShortReal, minimal or maximal |
267 | // Standard_ShortReal is returned. |
268 | // ======================================================================= |
269 | inline Standard_ShortReal RealToShortReal (const Standard_Real theVal) |
270 | { |
271 | return theVal < -FLT_MAX ? -FLT_MAX |
272 | : theVal > FLT_MAX ? FLT_MAX |
273 | : (Standard_ShortReal)theVal; |
274 | } |
275 | |
7fd59977 |
276 | //------------------------------------------------------------------- |
277 | // Round : Returns the nearest integer of a real |
278 | //------------------------------------------------------------------- |
279 | inline Standard_Real Round (const Standard_Real Value) |
280 | { return IntegerPart(Value + (Value > 0 ? 0.5 : -0.5)); } |
281 | |
282 | //------------------------------------------------------------------- |
283 | // Sin : Returns the sine of a real |
284 | //------------------------------------------------------------------- |
285 | inline Standard_Real Sin (const Standard_Real Value) |
286 | { return sin(Value); } |
287 | |
288 | //------------------------------------------------------------------- |
289 | // Sinh : Returns the hyperbolic sine of a real |
290 | //------------------------------------------------------------------- |
291 | inline Standard_Real Sinh(const Standard_Real Value) |
292 | { return sinh(Value); } |
293 | |
294 | //------------------------------------------------------------------- |
295 | // ASinh : Returns the hyperbolic arc sine of a real |
296 | //------------------------------------------------------------------- |
297 | inline Standard_Real ASinh(const Standard_Real Value) |
298 | { return asinh(Value); } |
299 | |
300 | //------------------------------------------------------------------- |
301 | // Square : Returns a real to the power 2 |
302 | //------------------------------------------------------------------- |
303 | inline Standard_Real Square(const Standard_Real Value) |
304 | { return Value * Value; } |
305 | |
306 | //------------------------------------------------------------------- |
307 | // Tan : Returns the tangent of a real |
308 | //------------------------------------------------------------------- |
309 | inline Standard_Real Tan (const Standard_Real Value) |
310 | { return tan(Value); } |
311 | |
312 | //------------------------------------------------------------------- |
313 | // Tanh : Returns the hyperbolic tangent of a real |
314 | //------------------------------------------------------------------- |
315 | inline Standard_Real Tanh (const Standard_Real Value) |
316 | { return tanh(Value); } |
317 | |
318 | #endif |