7e2b38c074e5aaba185deba3a9aec76b9ef2cdab
[platform/framework/native/appfw.git] / inc / FBaseFloat.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FBaseFloat.h
20  * @brief               This is the header file for the %Float class.
21  *
22  * @see                 Tizen::Base::Number
23  *
24  * This header file contains the declarations of the %Float class.
25  */
26 #ifndef _FBASE_FLOAT_H_
27 #define _FBASE_FLOAT_H_
28
29 #include <FBaseNumber.h>
30
31
32 namespace Tizen { namespace Base
33 {
34 /**
35  *      @class  Float
36  *      @brief  This class is the wrapper class for the @c signed @c float built-in type.
37  *
38  *      @since  2.0
39  *
40  *      The %Float class represents a single-precision 32-bit floating number. The %Float class wraps a @c float type value. This enables
41  *      passing a @c float value to a method that only accepts an instance of Object class. This class provides methods to compare instances
42  *      of this type, convert the value of an instance to its string representation, and convert the string representation
43  *      of a number to an instance of this type.
44  *
45  * The following example demonstrates how to use the %Float class.
46  *
47  *      @code
48  *
49  *      #include <FBase.h>
50  *
51  *      using namespace Tizen::Base;
52  *
53  *      // This method checks whether the given string object contains the string
54  *      // representation of the pre-defined minimum float value.
55  *      result
56  *      MyClass::Verify(String& string, bool& out)
57  *      {
58  *              // Creates and initializes an instance of Double
59  *              static const Float MINIMUM(1.23L);
60  *
61  *              result r = E_SUCCESS;
62  *
63  *              float f;
64  *
65  *              // Parses the string representation of the numeric value
66  *              // Returns f (value as signed float)
67  *              r = Float::Parse(string, f);
68  *
69  *              // Error Handling
70  *              if (IsFailed(r))
71  *              {
72  *                      goto CATCH;
73  *              }
74  *
75  *              out = (MINIMUM.CompareTo(f) == 0) ? true: false;
76  *              return r;
77  *      CATCH:
78  *              return r;
79  *      }
80  *      @endcode
81  */
82 class _OSP_EXPORT_ Float
83         : public Number
84 {
85 public:
86         /**
87          *      Initializes this instance of %Float with the specified value.
88          *
89          *      @since                  2.0
90          *
91          *      @param[in]      value   A @c float value
92          */
93         Float(float value = 0.0);
94
95
96         /**
97          *      Copying of objects using this copy constructor is allowed.
98          *
99          *      @since                  2.0
100          *
101          *      @param[in]      value   An instance of %Float
102          */
103         Float(const Float& value);
104
105         /**
106          *      This destructor overrides Tizen::Base::Object::~Object().
107          *
108          *      @since  2.0
109          */
110         virtual ~Float(void);
111
112         /**
113          *      Copying of objects using this copy assignment operator is allowed.
114          *
115          *      @since                  2.0
116          *
117          *      @param[in]      rhs     An instance of %Float
118          */
119         Float& operator =(const Float& rhs);
120
121         /**
122          *      Compares two @c float values.
123          *
124          *      @since          2.0
125          *
126          *      @return         A 32-bit @c signed integer value
127          *      @code
128          *      <  0  if the value of @c f1 is less than the value of @c f2
129          *      == 0  if the value of @c f1 is equal to the value of @c f2
130          *      >  0  if the value of @c f1 is greater than the value of @c f2
131          *      @endcode
132          *      @param[in]      f1      The first @c float value to compare
133          *      @param[in]      f2      The second @c float value to compare
134          */
135         static int Compare(float f1, float f2);
136
137         /**
138          *      Compares the value of the current instance with the value of the specified instance of the %Float class.
139          *
140          *      @since          2.0
141          *
142          *      @return         A 32-bit @c signed integer value
143          *      @code
144          *      @li <  0    if the value of the current instance is less than the value of the specified instance
145          *      @li == 0    if the value of the current instance is equal to the value of the specified instance
146          *      @li >  0    if the value of the current instance is greater than the value of the specified instance
147          *      @endcode
148          *      @param[in]      value   An instance of the %Float class to compare
149          */
150         int CompareTo(const Float& value) const;
151
152         /**
153          *      Checks whether the value of the specified instance of Object is equal to the value of the current instance of %Float.
154          *
155          *      @since          2.0
156          *
157          *      @return         @c true if the value of the specified instance of Object is equal to the value of the current instance of %Float, @n
158          *                              else @c false
159          *      @param[in]      obj             An instance of Object to compare
160          *      @see            Tizen::Base::Object::Equals()
161          */
162         virtual bool Equals(const Object& obj) const;
163
164
165         /**
166          *      Gets the hash value of the current instance of %Float.
167          *
168          *      @since          2.0
169          *
170          *      @return         An integer value indicating the hash value of the current instance of %Float
171          *      @remarks        Two equal instances must return the same hash value. For better performance,
172          *                              the used hash function must generate a random distribution for all inputs. @n
173          *                              The default implementation of this method returns the value of the current instance.
174          */
175         virtual int GetHashCode(void) const;
176
177         /**
178         *        Gets the hash value of the specified @c float value.
179         *
180         *        @since         2.0
181         *
182         *        @return        An integer value indicating the hash value of the specified @c float value
183         *        @param[in]   val   A @c float value to get the hash value
184         */
185         static int GetHashCode(float val);
186
187         /**
188          * Parses the specified string representing a numeric value and returns the value as @c signed @c float (as out parameter).
189          *
190          *      @since                  2.0
191          *
192          *      @return                 An error code
193          *      @param[in]      s                       A unicode string representing a @c signed @c float value
194          *      @param[out]     ret                     The numeric representation of the string
195          *      @exception      E_SUCCESS        The method is successful.
196          *      @exception      E_NUM_FORMAT The specified string does not contain a number that can be parsed.
197          *      @remarks        This method guarantees that the original value of out-parameter is not changed when the method returns error.
198          *      @remarks        The behavior of this method is dependent on the system default locale setting.
199          *      @see            Tizen::Base::Double::Parse()
200          */
201         static result Parse(const String& s, float& ret);
202
203         /**
204          *      Gets the @c signed @c char equivalent of the current instance of the %Float class.
205          *
206          *      @since          2.0
207          *
208          *      @return         A @c signed @c char equivalent of the current instance
209          */
210         virtual char ToChar(void) const;
211
212         /**
213          *      Gets the @c signed @c short equivalent of the current instance of the %Float class.
214          *
215          *      @since          2.0
216          *
217          *      @return         A @c signed @c short equivalent of the current instance
218          */
219         virtual short ToShort(void) const;
220
221         /**
222          *      Gets the @c signed @c int equivalent of the current instance of the %Float class.
223          *
224          *      @since          2.0
225          *
226          *      @return         A @c signed @c int equivalent of the current instance
227          */
228         virtual int ToInt(void) const;
229
230         /**
231          *      Gets the @c signed @c long equivalent of the current instance of the %Float class.
232          *
233          *      @since          2.0
234          *
235          *      @return         A @c signed @c long equivalent of the current instance
236          */
237         virtual long ToLong(void) const;
238
239         /**
240          * Gets the @c signed @c long @c long equivalent of the current instance of the %Float class.
241          *
242          * @since               2.0
243          *
244          * @return      A @c signed @c long @c long equivalent of the current instance
245          */
246         virtual long long ToLongLong(void) const;
247
248         /**
249          *      Gets the @c signed @c float equivalent of the current instance of the %Float class.
250          *
251          *      @since          2.0
252          *
253          *      @return         A @c signed @c float equivalent of the current instance
254          */
255         virtual float ToFloat(void) const;
256
257         /**
258          *      Gets the @c signed @c double equivalent of the current instance of the %Float class.
259          *
260          *      @since          2.0
261          *
262          *      @return         A @c signed @c double equivalent of the current instance
263          */
264         virtual double ToDouble(void) const;
265
266         /**
267          * Gets the string representing the value of the current instance of %Float.
268          *
269          * @since                       2.0
270          *
271          * @return              A string containing a Unicode representation of the value of the current instance
272          * @remarks             If the value is Not-a-Number (NaN), the result is the string "NaN". Furthermore, infinity
273          *                              produces the result "Infinity". @n
274          *                              6 digits are given for the precision of this method. Use String::Format() to set the specific precision.
275          * @remarks             The behavior of this method is dependent on the system default locale setting.
276          */
277         virtual String ToString(void) const;
278
279         /**
280          *      Gets the string representing the specified @c float value.
281          *
282          *      @since          2.0
283          *
284          *      @return         A string containing a Unicode representation of the specified @c float value
285          *      @param[in]      value   A @c float value to convert
286          *  @remarks    If the value is Not-a-Number (NaN), the result is the string "NaN". Furthermore, infinity
287          *                              produces the result "Infinity". @n
288          *                              6 digits are given for the precision of this method. Use String::Format() to set the specific precision.
289          * @remarks             The behavior of this method is dependent on the system default locale setting.
290          */
291         static String ToString(float value);
292
293         /**
294          *      Gets the IEEE 754 floating-point "single format" bit layout representation of the specified @c float value.
295          *
296          *      @since          2.0
297          *
298          *      @return         The bits that represent the floating-point number in the IEEE 754 floating-point "single format" bit layout
299          *      @param[in]      value   A @c float value to convert
300          *      @see            Tizen::Base::Double::Parse()
301          */
302         static int ToBits(float value);
303
304         /**
305          *      Gets the @c float value corresponding to the specified floating-point value in the IEEE 754 floating-point "single format" bit layout.
306          *
307          *      @since          2.0
308          *
309          *      @return         The @c float floating-point value with the same bit pattern
310          *      @param[in]      value   A floating-point value in the IEEE 754 floating-point "single format" bit layout
311          *      @see            Tizen::Base::Double::Parse()
312          */
313         static float ToFloatFromBits(int value);
314
315         /**
316          *      Checks whether the current value of %Float is equal to negative or positive infinity.
317          *
318          *      @since          2.0
319          *
320          *      @return         @c true if the current value equals negative or positive infinity, @n
321          *                              else @c false
322          */
323         bool IsInfinity(void) const;
324
325         /**
326          *      Checks whether the specified @c float value is equal to negative or positive infinity.
327          *
328          *      @since          2.0
329          *
330          *      @return         @c true if the specified value equals negative or positive infinity, @n
331          *                              else @c false
332          *      @param[in]      value   A @c float value to check
333          */
334         static bool IsInfinity(float value);
335
336         /**
337          *      Checks whether the current value is Not-a-Number.
338          *
339          *      @since          2.0
340          *
341          *      @return         @c true if the current value is Not-a-Number, @n
342          *                              else @c false
343          */
344         bool IsNaN(void) const;
345
346         /**
347          *      Checks whether the specified value is Not-a-Number.
348          *
349          *      @since          2.0
350          *
351          *      @return         @c true if the specified value is Not-a-Number, @n
352          *                              else @c false
353          *      @param[in]      value   A @c float value to check
354          */
355         static bool IsNaN(float value);
356
357         /**
358          *      Gets a constant holding the largest positive finite value of type @c float. @n
359          *      This is equal to the value defined in Limit.h of the C library.
360          *
361          *      @since          2.0
362          *
363          *      @return         A constant holding the largest positive finite value of type @c float
364          */
365         static float GetMaxValue(void);
366
367         /**
368          *      Gets a constant holding the smallest positive non-zero value of type @c float. @n
369          *      This is equal to the value defined in Limit.h of the C library.
370          *
371          *      @since          2.0
372          *
373          *      @return         A constant holding the smallest possible non-zero value of type @c float
374          */
375         static float GetMinValue(void);
376
377         /**
378          * A @c float value of this instance.
379          *
380          * @since       2.0
381          */
382         float value;
383
384
385 private:
386         /**
387          *      Checks if the specified @c float value is finite.
388          *
389          *      @since          2.0
390          *
391          *      @return         @c true if the specified value is finite, @n
392          *                              else @c false
393          *      @param[in]      value   A @c float value to check
394          */
395         static bool IsFinite(float value);
396
397         static const int __DBL_MAX_10_EXP = 308;
398
399         friend class _FloatImpl;
400         class _FloatImpl * __pFloatImpl;
401
402 }; // Float
403
404 }} // Tizen::Base
405
406 #endif //_FBASE_FLOAT_H_