Merge "[DCM-1855] Fix to get the system tz." into tizen_2.1
[platform/framework/native/appfw.git] / src / base / FBaseDouble.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseDouble.cpp
19  * @brief               This is the implementation file for Double class.
20  * @see         Number
21  */
22
23 #include <cfloat>
24 #include <math.h>
25 #include <stdio.h>
26 #include <wchar.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <FBaseDouble.h>
30 #include <FBaseResult.h>
31 #include <FBaseSysLog.h>
32 #include "FBase_LocalizedNumParser.h"
33
34
35 namespace Tizen { namespace Base
36 {
37
38 union DoubleBitRep
39 {
40         explicit
41         DoubleBitRep(double d = 0.0)
42                 : value(d)
43         {
44         };
45
46         DoubleBitRep(long long l)
47                 : rep(l)
48         {
49         };
50
51         double value;
52         long long rep;
53 };
54
55 Double::Double(double value)
56         : value(value)
57         , __pDoubleImpl(null)
58 {
59 }
60
61 Double::Double(const Double& value)
62         : value(value.value)
63         , __pDoubleImpl(null)
64 {
65 }
66
67 Double::~Double(void)
68 {
69 }
70
71 Double&
72 Double::operator =(const Double& rhs)
73 {
74         if (&rhs != this)
75         {
76                 value = rhs.value;
77         }
78         return *this;
79 }
80
81 int
82 Double::Compare(double d1, double d2)
83 {
84         int ret = 0;
85         if (d1 > d2)
86         {
87                 ret = 1;
88         }
89         else if (d1 < d2)
90         {
91                 ret = -1;
92         }
93         else
94         {
95                 ret = 0;
96         }
97         return ret;
98 }
99
100 int
101 Double::CompareTo(double value) const
102 {
103         return(Double::Compare(this->value, value));
104 }
105
106 int
107 Double::CompareTo(const Double& value) const
108 {
109         return(Double::Compare(this->value, value.value));
110 }
111
112 bool
113 Double::Equals(const Object& obj) const
114 {
115         const Double* pOther = dynamic_cast< const Double* >(&obj);
116
117         if (pOther == null)
118         {
119                 return false;
120         }
121
122         if (Double::Compare(this->value, (*pOther).value) == 0)
123         {
124                 return true;
125         }
126         else
127         {
128                 return false;
129         }
130 }
131
132 int
133 Double::GetHashCode(void) const
134 {
135         double* pTemp = const_cast< double* >(&value);
136         int* pValueLow = reinterpret_cast< int* >(pTemp);
137         int* pValueHigh = pValueLow + 1;
138         return *pValueLow + *pValueHigh;
139 }
140
141 int
142 Double::GetHashCode(double val)
143 {
144         int* pValueLow = reinterpret_cast< int* >(&val);
145         int* pValueHigh = pValueLow + 1;
146         return *pValueLow + *pValueHigh;
147 }
148
149 result
150 Double::Parse(const String& s, double& ret)
151 {
152         double tmpRet = _LocalizedNumParser::ToDouble(s, "");
153
154         if (IsNaN(tmpRet))
155         {
156                 result r = GetLastResult();
157                 SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
158                 return r;
159         }
160
161         ret = tmpRet;
162         return E_SUCCESS;
163 }
164
165 char
166 Double::ToChar(void) const
167 {
168         return static_cast< char >(value);
169 }
170
171 short
172 Double::ToShort(void) const
173 {
174         return static_cast< short >(value);
175 }
176
177 int
178 Double::ToInt(void) const
179 {
180         return static_cast< int >(value);
181 }
182
183 long
184 Double::ToLong(void) const
185 {
186         return static_cast< long >(value);
187 }
188
189 long long
190 Double::ToLongLong(void) const
191 {
192         return static_cast< long long >(value);
193 }
194
195 float
196 Double::ToFloat(void) const
197 {
198         return static_cast< float >(value);
199 }
200
201 double
202 Double::ToDouble(void) const
203 {
204         return value;
205 }
206
207 String
208 Double::ToString(void) const
209 {
210         return Double::ToString(value);
211 }
212
213 String
214 Double::ToString(double value)
215 {
216         return _LocalizedNumParser::ToString(value, "");
217 }
218
219 long long
220 Double::ToBits(double value)
221 {
222         DoubleBitRep bitRep;
223         bitRep.value = value;
224         return (long long) bitRep.rep;
225 }
226
227 double
228 Double::ToDoubleFromBits(long long value)
229 {
230         DoubleBitRep bitRep;
231         bitRep.rep = value;
232         return bitRep.value;
233 }
234
235 bool
236 Double::IsFinite(double d)
237 {
238         return isfinite(d);
239 }
240
241 bool
242 Double::IsInfinity(void) const
243 {
244         return Double::IsInfinity(value);
245 }
246
247 bool
248 Double::IsInfinity(double value)
249 {
250         return !Double::IsFinite(value);
251 }
252
253 bool
254 Double::IsNaN(void) const
255 {
256         return Double::IsNaN(value);
257 }
258
259 bool
260 Double::IsNaN(double value)
261 {
262         return isnan(value);
263 }
264
265 double
266 Double::GetMaxValue(void)
267 {
268         return DBL_MAX;
269 }
270
271 double
272 Double::GetMinValue(void)
273 {
274         return DBL_MIN;
275 }
276
277 }} // Tizen::Base