1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHLIMITS_H
38 #define INCLUDED_IMATHLIMITS_H
40 //----------------------------------------------------------------
42 // Limitations of the basic C++ numerical data types
44 //----------------------------------------------------------------
49 //------------------------------------------
50 // In Windows, min and max are macros. Yay.
51 //------------------------------------------
53 #if defined _WIN32 || defined _WIN64
65 //-----------------------------------------------------------------
67 // Template class limits<T> returns information about the limits
68 // of numerical data type T:
70 // min() largest possible negative value of type T
72 // max() largest possible positive value of type T
74 // smallest() smallest possible positive value of type T
76 // epsilon() smallest possible e of type T, for which
79 // isIntegral() returns true if T is an integral type
81 // isSigned() returns true if T is signed
83 // Class limits<T> is useful to implement template classes or
84 // functions which depend on the limits of a numerical type
85 // which is not known in advance; for example:
87 // template <class T> max (T x[], int n)
89 // T m = limits<T>::min();
91 // for (int i = 0; i < n; i++)
98 // Class limits<T> has been implemented for the following types:
100 // char, signed char, unsigned char
101 // short, unsigned short
103 // long, unsigned long
108 // Class limits<T> has only static member functions, all of which
109 // are implemented as inlines. No objects of type limits<T> are
112 //-----------------------------------------------------------------
115 template <class T> struct limits
121 static bool isIntegral();
122 static bool isSigned();
133 static char min() {return CHAR_MIN;}
134 static char max() {return CHAR_MAX;}
135 static char smallest() {return 1;}
136 static char epsilon() {return 1;}
137 static bool isIntegral() {return true;}
138 static bool isSigned() {return (char) ~0 < 0;}
142 struct limits <signed char>
144 static signed char min() {return SCHAR_MIN;}
145 static signed char max() {return SCHAR_MAX;}
146 static signed char smallest() {return 1;}
147 static signed char epsilon() {return 1;}
148 static bool isIntegral() {return true;}
149 static bool isSigned() {return true;}
153 struct limits <unsigned char>
155 static unsigned char min() {return 0;}
156 static unsigned char max() {return UCHAR_MAX;}
157 static unsigned char smallest() {return 1;}
158 static unsigned char epsilon() {return 1;}
159 static bool isIntegral() {return true;}
160 static bool isSigned() {return false;}
164 struct limits <short>
166 static short min() {return SHRT_MIN;}
167 static short max() {return SHRT_MAX;}
168 static short smallest() {return 1;}
169 static short epsilon() {return 1;}
170 static bool isIntegral() {return true;}
171 static bool isSigned() {return true;}
175 struct limits <unsigned short>
177 static unsigned short min() {return 0;}
178 static unsigned short max() {return USHRT_MAX;}
179 static unsigned short smallest() {return 1;}
180 static unsigned short epsilon() {return 1;}
181 static bool isIntegral() {return true;}
182 static bool isSigned() {return false;}
188 static int min() {return INT_MIN;}
189 static int max() {return INT_MAX;}
190 static int smallest() {return 1;}
191 static int epsilon() {return 1;}
192 static bool isIntegral() {return true;}
193 static bool isSigned() {return true;}
197 struct limits <unsigned int>
199 static unsigned int min() {return 0;}
200 static unsigned int max() {return UINT_MAX;}
201 static unsigned int smallest() {return 1;}
202 static unsigned int epsilon() {return 1;}
203 static bool isIntegral() {return true;}
204 static bool isSigned() {return false;}
210 static long min() {return LONG_MIN;}
211 static long max() {return LONG_MAX;}
212 static long smallest() {return 1;}
213 static long epsilon() {return 1;}
214 static bool isIntegral() {return true;}
215 static bool isSigned() {return true;}
219 struct limits <unsigned long>
221 static unsigned long min() {return 0;}
222 static unsigned long max() {return ULONG_MAX;}
223 static unsigned long smallest() {return 1;}
224 static unsigned long epsilon() {return 1;}
225 static bool isIntegral() {return true;}
226 static bool isSigned() {return false;}
230 struct limits <float>
232 static float min() {return -FLT_MAX;}
233 static float max() {return FLT_MAX;}
234 static float smallest() {return FLT_MIN;}
235 static float epsilon() {return FLT_EPSILON;}
236 static bool isIntegral() {return false;}
237 static bool isSigned() {return true;}
241 struct limits <double>
243 static double min() {return -DBL_MAX;}
244 static double max() {return DBL_MAX;}
245 static double smallest() {return DBL_MIN;}
246 static double epsilon() {return DBL_EPSILON;}
247 static bool isIntegral() {return false;}
248 static bool isSigned() {return true;}
252 struct limits <long double>
254 static long double min() {return -LDBL_MAX;}
255 static long double max() {return LDBL_MAX;}
256 static long double smallest() {return LDBL_MIN;}
257 static long double epsilon() {return LDBL_EPSILON;}
258 static bool isIntegral() {return false;}
259 static bool isSigned() {return true;}