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