1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _Standard_Real_HeaderFile
16 #define _Standard_Real_HeaderFile
20 #include <Standard_values.h>
21 #include <Standard_math.hxx>
22 #include <Standard_TypeDef.hxx>
24 // ===============================================
25 // Methods from Standard_Entity class which are redefined:
28 // ===============================================
30 // ==================================
31 // Methods implemeted in Standard_Real.cxx
32 // ==================================
33 __Standard_API Standard_Integer HashCode (const Standard_Real, const Standard_Integer);
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 );
46 //-------------------------------------------------------------------
47 // RealSmall : Returns the smallest positive real
48 //-------------------------------------------------------------------
49 inline Standard_Real RealSmall()
52 //-------------------------------------------------------------------
53 // Abs : Returns the absolute value of a real
54 //-------------------------------------------------------------------
55 inline Standard_Real Abs(const Standard_Real Value)
56 { return fabs(Value); }
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(); }
66 // *********************************** //
69 // Machine-dependant values //
70 // Should be taken from include file //
71 // *********************************** //
74 //-------------------------------------------------------------------
75 // RealDigit : Returns the number of digits of precision in a real
76 //-------------------------------------------------------------------
77 inline Standard_Integer RealDigits()
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; }
87 //-------------------------------------------------------------------
88 // RealFirst : Returns the minimum negative value of a real
89 //-------------------------------------------------------------------
90 inline Standard_Real RealFirst()
93 //-------------------------------------------------------------------
94 // RealFirst10Exp : Returns the minimum value of exponent(base 10) of
96 //-------------------------------------------------------------------
97 inline Standard_Integer RealFirst10Exp()
98 { return DBL_MIN_10_EXP; }
100 //-------------------------------------------------------------------
101 // RealLast : Returns the maximum value of a real
102 //-------------------------------------------------------------------
103 inline Standard_Real RealLast()
106 //-------------------------------------------------------------------
107 // RealLast10Exp : Returns the maximum value of exponent(base 10) of
109 //-------------------------------------------------------------------
110 inline Standard_Integer RealLast10Exp()
111 { return DBL_MAX_10_EXP; }
113 //-------------------------------------------------------------------
114 // RealMantissa : Returns the size in bits of the matissa part of a
116 //-------------------------------------------------------------------
117 inline Standard_Integer RealMantissa()
118 { return DBL_MANT_DIG; }
120 //-------------------------------------------------------------------
121 // RealRadix : Returns the radix of exponent representation
122 //-------------------------------------------------------------------
123 inline Standard_Integer RealRadix()
124 { return FLT_RADIX; }
126 //-------------------------------------------------------------------
127 // RealSize : Returns the size in bits of an integer
128 //-------------------------------------------------------------------
129 inline Standard_Integer RealSize()
130 { return BITS(Standard_Real); }
134 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=//
135 // End of machine-dependant values //
136 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=//
139 //-------------------------------------------------------------------
140 // IntToReal : Converts an integer in a real
141 //-------------------------------------------------------------------
142 inline Standard_Real IntToReal(const Standard_Integer Value)
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); }
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); }
158 //-------------------------------------------------------------------
159 // Cos : Returns the cosine of a real
160 //-------------------------------------------------------------------
161 inline Standard_Real Cos (const Standard_Real Value)
162 { return cos(Value); }
164 //-------------------------------------------------------------------
165 // Cosh : Returns the hyperbolic cosine of a real
166 //-------------------------------------------------------------------
167 inline Standard_Real Cosh (const Standard_Real Value)
168 { return cosh(Value); }
171 //-------------------------------------------------------------------
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.
179 //-------------------------------------------------------------------
180 inline Standard_Real Epsilon (const Standard_Real Value)
182 Standard_Real aEpsilon;
185 aEpsilon = NextAfter(Value, RealLast()) - Value;
187 aEpsilon = Value - NextAfter(Value, RealFirst());
192 //-------------------------------------------------------------------
193 // Exp : Returns the exponential function of a real
194 //-------------------------------------------------------------------
195 inline Standard_Real Exp (const Standard_Real Value)
196 { return exp(Value); }
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); }
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) ); }
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); }
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)
223 return Val1 >= Val2 ? Val1 : Val2;
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)
232 return Val1 <= Val2 ? Val1 : Val2;
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); }
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); }
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)
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;
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)
271 return theVal < -FLT_MAX ? -FLT_MAX
272 : theVal > FLT_MAX ? FLT_MAX
273 : (Standard_ShortReal)theVal;
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)); }
282 //-------------------------------------------------------------------
283 // Sin : Returns the sine of a real
284 //-------------------------------------------------------------------
285 inline Standard_Real Sin (const Standard_Real Value)
286 { return sin(Value); }
288 //-------------------------------------------------------------------
289 // Sinh : Returns the hyperbolic sine of a real
290 //-------------------------------------------------------------------
291 inline Standard_Real Sinh(const Standard_Real Value)
292 { return sinh(Value); }
294 //-------------------------------------------------------------------
295 // ASinh : Returns the hyperbolic arc sine of a real
296 //-------------------------------------------------------------------
297 inline Standard_Real ASinh(const Standard_Real Value)
299 { return std::asinh(Value); }
301 { return asinh(Value); }
304 //-------------------------------------------------------------------
305 // Square : Returns a real to the power 2
306 //-------------------------------------------------------------------
307 inline Standard_Real Square(const Standard_Real Value)
308 { return Value * Value; }
310 //-------------------------------------------------------------------
311 // Tan : Returns the tangent of a real
312 //-------------------------------------------------------------------
313 inline Standard_Real Tan (const Standard_Real Value)
314 { return tan(Value); }
316 //-------------------------------------------------------------------
317 // Tanh : Returns the hyperbolic tangent of a real
318 //-------------------------------------------------------------------
319 inline Standard_Real Tanh (const Standard_Real Value)
320 { return tanh(Value); }