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