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 // ==================================
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);
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 );
46 //! Returns |a| if b >= 0; -|a| if b < 0.
47 Standard_EXPORT Standard_Real Sign(const Standard_Real a, const Standard_Real b);
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 );
56 //-------------------------------------------------------------------
57 // RealSmall : Returns the smallest positive real
58 //-------------------------------------------------------------------
59 inline Standard_Real RealSmall()
62 //-------------------------------------------------------------------
63 // Abs : Returns the absolute value of a real
64 //-------------------------------------------------------------------
65 inline Standard_Real Abs(const Standard_Real Value)
66 { return fabs(Value); }
69 //-------------------------------------------------------------------
70 // IsEqual : Returns Standard_True if two reals are equal
71 //-------------------------------------------------------------------
72 inline Standard_Boolean IsEqual (const Standard_Real Value1,
73 const Standard_Real Value2)
74 { return Abs((Value1 - Value2)) < RealSmall(); }
76 // *********************************** //
79 // Machine-dependant values //
80 // Should be taken from include file //
81 // *********************************** //
84 //-------------------------------------------------------------------
85 // RealDigit : Returns the number of digits of precision in a real
86 //-------------------------------------------------------------------
87 inline Standard_Integer RealDigits()
90 //-------------------------------------------------------------------
91 // RealEpsilon : Returns the minimum positive real such that
92 // 1.0 + x is not equal to 1.0
93 //-------------------------------------------------------------------
94 inline Standard_Real RealEpsilon()
95 { return DBL_EPSILON; }
97 //-------------------------------------------------------------------
98 // RealFirst : Returns the minimum negative value of a real
99 //-------------------------------------------------------------------
100 inline Standard_Real RealFirst()
103 //-------------------------------------------------------------------
104 // RealFirst10Exp : Returns the minimum value of exponent(base 10) of
106 //-------------------------------------------------------------------
107 inline Standard_Integer RealFirst10Exp()
108 { return DBL_MIN_10_EXP; }
110 //-------------------------------------------------------------------
111 // RealLast : Returns the maximum value of a real
112 //-------------------------------------------------------------------
113 inline Standard_Real RealLast()
116 //-------------------------------------------------------------------
117 // RealLast10Exp : Returns the maximum value of exponent(base 10) of
119 //-------------------------------------------------------------------
120 inline Standard_Integer RealLast10Exp()
121 { return DBL_MAX_10_EXP; }
123 //-------------------------------------------------------------------
124 // RealMantissa : Returns the size in bits of the matissa part of a
126 //-------------------------------------------------------------------
127 inline Standard_Integer RealMantissa()
128 { return DBL_MANT_DIG; }
130 //-------------------------------------------------------------------
131 // RealRadix : Returns the radix of exponent representation
132 //-------------------------------------------------------------------
133 inline Standard_Integer RealRadix()
134 { return FLT_RADIX; }
136 //-------------------------------------------------------------------
137 // RealSize : Returns the size in bits of an integer
138 //-------------------------------------------------------------------
139 inline Standard_Integer RealSize()
140 { return BITS(Standard_Real); }
144 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=//
145 // End of machine-dependant values //
146 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=//
149 //-------------------------------------------------------------------
150 // IntToReal : Converts an integer in a real
151 //-------------------------------------------------------------------
152 inline Standard_Real IntToReal(const Standard_Integer Value)
155 //-------------------------------------------------------------------
156 // ATan : Returns the value of the arc tangent of a real
157 //-------------------------------------------------------------------
158 inline Standard_Real ATan(const Standard_Real Value)
159 { return atan(Value); }
162 //-------------------------------------------------------------------
163 // Ceiling : Returns the smallest integer not less than a real
164 //-------------------------------------------------------------------
165 inline Standard_Real Ceiling (const Standard_Real Value)
166 { return ceil(Value); }
168 //-------------------------------------------------------------------
169 // Cos : Returns the cosine of a real
170 //-------------------------------------------------------------------
171 inline Standard_Real Cos (const Standard_Real Value)
172 { return cos(Value); }
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 choseen 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 //-------------------------------------------------------------------
184 inline Standard_Real Epsilon (const Standard_Real Value)
186 Standard_Real aEpsilon;
189 aEpsilon = NextAfter(Value, RealLast()) - Value;
191 aEpsilon = Value - NextAfter(Value, RealFirst());
196 //-------------------------------------------------------------------
197 // Exp : Returns the exponential function of a real
198 //-------------------------------------------------------------------
199 inline Standard_Real Exp (const Standard_Real Value)
200 { return exp(Value); }
202 //-------------------------------------------------------------------
203 // Floor : Return the largest integer not greater than a real
204 //-------------------------------------------------------------------
205 inline Standard_Real Floor (const Standard_Real Value)
206 { return floor(Value); }
208 //-------------------------------------------------------------------
209 // IntegerPart : Returns the integer part of a real
210 //-------------------------------------------------------------------
211 inline Standard_Real IntegerPart (const Standard_Real Value)
212 { return ( (Value>0) ? floor(Value) : ceil(Value) ); }
215 //-------------------------------------------------------------------
216 // Log10 : Returns the base-10 logarithm of a real
217 //-------------------------------------------------------------------
218 inline Standard_Real Log10 (const Standard_Real Value)
219 { return log10(Value); }
221 //-------------------------------------------------------------------
222 // Max : Returns the maximum value of two reals
223 //-------------------------------------------------------------------
224 inline Standard_Real Max (const Standard_Real Val1,
225 const Standard_Real Val2)
227 return Val1 >= Val2 ? Val1 : Val2;
230 //-------------------------------------------------------------------
231 // Min : Returns the minimum value of two reals
232 //-------------------------------------------------------------------
233 inline Standard_Real Min (const Standard_Real Val1,
234 const Standard_Real Val2)
236 return Val1 <= Val2 ? Val1 : Val2;
239 //-------------------------------------------------------------------
240 // Pow : Returns a real to a given power
241 //-------------------------------------------------------------------
242 inline Standard_Real Pow (const Standard_Real Value, const Standard_Real P)
243 { return pow(Value,P); }
245 //-------------------------------------------------------------------
246 // RealPart : Returns the fractional part of a real.
247 //-------------------------------------------------------------------
248 inline Standard_Real RealPart (const Standard_Real Value)
249 { return fabs(IntegerPart(Value) - Value); }
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 //-------------------------------------------------------------------
256 inline Standard_Integer RealToInt (const Standard_Real Value)
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;
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 // =======================================================================
273 inline Standard_ShortReal RealToShortReal (const Standard_Real theVal)
275 return theVal < -FLT_MAX ? -FLT_MAX
276 : theVal > FLT_MAX ? FLT_MAX
277 : (Standard_ShortReal)theVal;
280 //-------------------------------------------------------------------
281 // Round : Returns the nearest integer of a real
282 //-------------------------------------------------------------------
283 inline Standard_Real Round (const Standard_Real Value)
284 { return IntegerPart(Value + (Value > 0 ? 0.5 : -0.5)); }
286 //-------------------------------------------------------------------
287 // Sin : Returns the sine of a real
288 //-------------------------------------------------------------------
289 inline Standard_Real Sin (const Standard_Real Value)
290 { return sin(Value); }
293 //-------------------------------------------------------------------
294 // ASinh : Returns the hyperbolic arc sine of a real
295 //-------------------------------------------------------------------
296 inline Standard_Real ASinh(const Standard_Real Value)
298 { return std::asinh(Value); }
300 { return asinh(Value); }
303 //-------------------------------------------------------------------
304 // Square : Returns a real to the power 2
305 //-------------------------------------------------------------------
306 inline Standard_Real Square(const Standard_Real Value)
307 { return Value * Value; }
309 //-------------------------------------------------------------------
310 // Tan : Returns the tangent of a real
311 //-------------------------------------------------------------------
312 inline Standard_Real Tan (const Standard_Real Value)
313 { return tan(Value); }
315 //-------------------------------------------------------------------
316 // Tanh : Returns the hyperbolic tangent of a real
317 //-------------------------------------------------------------------
318 inline Standard_Real Tanh (const Standard_Real Value)
319 { return tanh(Value); }