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_ShortReal_HeaderFile
16 #define _Standard_ShortReal_HeaderFile
17 
18 #include <cmath>
19 #include <float.h>
20 
21 #include <Standard_values.h>
22 #include <Standard_TypeDef.hxx>
23 
24          //  *********************************** //
25          //       Class methods                  //
26          //                                      //
27          //  Machine-dependent values            //
28          //  Should be taken from include file   //
29          //  *********************************** //
30 
31 //-------------------------------------------------------------------
32 // ShortRealSmall : Returns the smallest positive ShortReal
33 //-------------------------------------------------------------------
ShortRealSmall()34 inline Standard_ShortReal     ShortRealSmall()
35 { return FLT_MIN; }
36 
37 //-------------------------------------------------------------------
38 // Abs : Returns the absolute value of a ShortReal
39 //-------------------------------------------------------------------
Abs(const Standard_ShortReal Value)40 inline Standard_ShortReal     Abs(const Standard_ShortReal Value)
41 #if defined (__alpha) || defined(DECOSF1)
42 { return fabsf(Value); }
43 #else
44 { return float( fabs (Value) ) ; }
45 #endif
46 
47 //-------------------------------------------------------------------
48 // ShortRealDigit : Returns the number of digits of precision in a ShortReal
49 //-------------------------------------------------------------------
ShortRealDigits()50 inline Standard_Integer  ShortRealDigits()
51 { return FLT_DIG; }
52 
53 //-------------------------------------------------------------------
54 // ShortRealEpsilon : Returns the minimum positive ShortReal such that
55 //               1.0 + x is not equal to 1.0
56 //-------------------------------------------------------------------
ShortRealEpsilon()57 inline Standard_ShortReal     ShortRealEpsilon()
58 { return FLT_EPSILON; }
59 
60 //-------------------------------------------------------------------
61 // ShortRealFirst : Returns the minimum negative value of a ShortReal
62 //-------------------------------------------------------------------
ShortRealFirst()63 inline Standard_ShortReal     ShortRealFirst()
64 { Standard_ShortReal MaxFloatTmp = -FLT_MAX;
65   return MaxFloatTmp; }
66 
67 //-------------------------------------------------------------------
68 // ShortRealFirst10Exp : Returns the minimum value of exponent(base 10) of
69 //                  a ShortReal.
70 //-------------------------------------------------------------------
ShortRealFirst10Exp()71 inline Standard_Integer  ShortRealFirst10Exp()
72 { return FLT_MIN_10_EXP; }
73 
74 //-------------------------------------------------------------------
75 // ShortRealLast : Returns the maximum value of a ShortReal
76 //-------------------------------------------------------------------
ShortRealLast()77 inline Standard_ShortReal     ShortRealLast()
78 { return  FLT_MAX; }
79 
80 //-------------------------------------------------------------------
81 // ShortRealLast10Exp : Returns the maximum value of exponent(base 10) of
82 //                 a ShortReal.
83 //-------------------------------------------------------------------
ShortRealLast10Exp()84 inline Standard_Integer  ShortRealLast10Exp()
85 { return  FLT_MAX_10_EXP; }
86 
87 //-------------------------------------------------------------------
88 // ShortRealMantissa : Returns the size in bits of the matissa part of a
89 //                ShortReal.
90 //-------------------------------------------------------------------
ShortRealMantissa()91 inline Standard_Integer  ShortRealMantissa()
92 { return  FLT_MANT_DIG; }
93 
94 //-------------------------------------------------------------------
95 // ShortRealRadix : Returns the radix of exponent representation
96 //-------------------------------------------------------------------
ShortRealRadix()97 inline Standard_Integer  ShortRealRadix()
98 { return  FLT_RADIX; }
99 
100 //-------------------------------------------------------------------
101 // ShortRealSize : Returns the size in bits of an integer
102 //-------------------------------------------------------------------
ShortRealSize()103 inline Standard_Integer  ShortRealSize()
104 { return BITS(Standard_ShortReal); }
105 
106 //-------------------------------------------------------------------
107 // Max : Returns the maximum value of two ShortReals
108 //-------------------------------------------------------------------
Max(const Standard_ShortReal Val1,const Standard_ShortReal Val2)109 inline Standard_ShortReal     Max (const Standard_ShortReal Val1,
110 				   const Standard_ShortReal Val2)
111 {
112   if (Val1 >= Val2) {
113     return Val1;
114   } else {
115     return Val2;
116   }
117 }
118 
119 //-------------------------------------------------------------------
120 // Min : Returns the minimum value of two ShortReals
121 //-------------------------------------------------------------------
Min(const Standard_ShortReal Val1,const Standard_ShortReal Val2)122 inline Standard_ShortReal     Min (const Standard_ShortReal Val1,
123 				   const Standard_ShortReal Val2)
124 {
125   if (Val1 <= Val2) {
126     return Val1;
127   } else {
128     return Val2;
129   }
130 }
131 
132 // ===============================================
133 // Methods from Standard_Entity class which are redefined:
134 //    - Hascode
135 //    - IsEqual
136 // ===============================================
137 
138 // ==================================
139 // Methods implemented in Standard_ShortReal.cxx
140 // ==================================
141 
142 //! Computes a hash code for the given short real, in the range [1, theUpperBound]
143 //! @param theShortReal the short real value which hash code is to be computed
144 //! @param theUpperBound the upper bound of the range a computing hash code must be within
145 //! @return a computed hash code, in the range [1, theUpperBound]
146 Standard_EXPORT Standard_Integer HashCode (Standard_ShortReal theShortReal, Standard_Integer theUpperBound);
147 
148 //-------------------------------------------------------------------
149 // IsEqual : Returns Standard_True if two ShortReals are equal
150 //-------------------------------------------------------------------
IsEqual(const Standard_ShortReal Value1,const Standard_ShortReal Value2)151 inline Standard_Boolean  IsEqual (const Standard_ShortReal Value1,
152 				  const Standard_ShortReal Value2)
153 { return Abs((Value1 - Value2)) < ShortRealSmall(); }
154 
155 #endif
156 
157 
158