2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FBaseDouble.cpp
20 * @brief This is the implementation file for Double class.
30 #include <FBaseDouble.h>
31 #include <FBaseResult.h>
32 #include "FBase_NativeError.h"
33 #include <FBaseSysLog.h>
36 namespace Tizen { namespace Base
42 DoubleBitRep(double d = 0.0)
47 DoubleBitRep(long long l)
56 Double::Double(double value)
62 Double::Double(const Double& value)
73 Double::operator =(const Double& rhs)
83 Double::Compare(double d1, double d2)
102 Double::CompareTo(double value) const
104 return(Double::Compare(this->value, value));
108 Double::CompareTo(const Double& value) const
110 return(Double::Compare(this->value, value.value));
114 Double::Equals(const Object& obj) const
116 const Double* pOther = dynamic_cast <const Double*>(&obj);
123 if (Double::Compare(this->value, (*pOther).value) == 0)
134 Double::GetHashCode(void) const
136 double* pTemp = const_cast<double*> (&value);
137 int* pValueLow = reinterpret_cast<int*> (pTemp);
138 int* pValueHigh = pValueLow + 1;
139 return *pValueLow + *pValueHigh;
143 Double::GetHashCode(double val)
145 int* pValueLow = reinterpret_cast<int*> (&val);
146 int* pValueHigh = pValueLow + 1;
147 return *pValueLow + *pValueHigh;
151 Double::Parse(const String& s, double& ret)
153 wchar_t* pEnd = null;
156 ret = wcstod(s.GetPointer(), &pEnd);
157 SysTryReturnResult(NID_BASE, ((!(!Double::Compare(ret, 0) && errno == EINVAL)) && (pEnd[0] == 0)), E_NUM_FORMAT,
158 "Double parse failed with reason (%s). Scan stopped at (%ls)", __ConvertNativeErrorToMessage(errno), pEnd);
159 SysTryReturnResult(NID_BASE, !(errno != 0 && (!Double::Compare(ret, HUGE_VAL)|| !Double::Compare(ret, -HUGE_VAL))),
160 E_NUM_FORMAT, "Parsed value cannot fit into a Double.");
166 Double::ToChar(void) const
168 return static_cast<char> (value);
172 Double::ToShort(void) const
174 return static_cast<short> (value);
178 Double::ToInt(void) const
180 return static_cast<int> (value);
184 Double::ToLong(void) const
186 return static_cast<long> (value);
190 Double::ToLongLong(void) const
192 return static_cast<long long> (value);
196 Double::ToFloat(void) const
198 return static_cast<float> (value);
202 Double::ToDouble(void) const
208 Double::ToString(void) const
210 return(Double::ToString(value));
214 Double::ToString(double value)
216 const static unsigned int MAX_DIG = 17 + 3;
217 const static unsigned int DOUBLE_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
219 if (Double::IsNaN(value))
221 return String(L"NaN");
223 else if (Double::IsInfinity(value))
225 return String(L"Infinity");
229 wchar_t sValue[DOUBLE_LENGTH_MAX + 1] = {0, };
230 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%#lg", value);
231 return String(sValue);
236 Double::ToBits(double value)
239 bitRep.value = value;
240 return (long long) bitRep.rep;
244 Double::ToDoubleFromBits(long long value)
252 Double::IsFinite(double d)
254 return((isfinite(d) != 0) ? true : false);
258 Double::IsInfinity(void) const
260 return(Double::IsInfinity(value));
264 Double::IsInfinity(double value)
266 return(!Double::IsFinite(value));
270 Double::IsNaN(void) const
272 return(Double::IsNaN(value));
276 Double::IsNaN(double value)
278 return((isnan(value) != 0) ? true : false);
282 Double::GetMaxValue(void)
288 Double::GetMinValue(void)