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