2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseDouble.cpp
19 * @brief This is the implementation file for Double class.
29 #include <FBaseDouble.h>
30 #include <FBaseResult.h>
31 #include "FBase_NativeError.h"
32 #include <FBaseSysLog.h>
35 namespace Tizen { namespace Base
41 DoubleBitRep(double d = 0.0)
46 DoubleBitRep(long long l)
55 Double::Double(double value)
61 Double::Double(const Double& value)
72 Double::operator =(const Double& rhs)
82 Double::Compare(double d1, double d2)
101 Double::CompareTo(double value) const
103 return(Double::Compare(this->value, value));
107 Double::CompareTo(const Double& value) const
109 return(Double::Compare(this->value, value.value));
113 Double::Equals(const Object& obj) const
115 const Double* pOther = dynamic_cast <const Double*>(&obj);
122 if (Double::Compare(this->value, (*pOther).value) == 0)
133 Double::GetHashCode(void) const
135 double* pTemp = const_cast<double*> (&value);
136 int* pValueLow = reinterpret_cast<int*> (pTemp);
137 int* pValueHigh = pValueLow + 1;
138 return *pValueLow + *pValueHigh;
142 Double::GetHashCode(double val)
144 int* pValueLow = reinterpret_cast<int*> (&val);
145 int* pValueHigh = pValueLow + 1;
146 return *pValueLow + *pValueHigh;
150 Double::Parse(const String& s, double& ret)
152 wchar_t* pEnd = null;
154 double tmpRet = wcstod(s.GetPointer(), &pEnd);
155 SysTryReturnResult(NID_BASE, ((!(!Double::Compare(tmpRet, 0) && errno == EINVAL)) && (pEnd[0] == 0)), E_NUM_FORMAT,
156 "Double parse failed with reason (%s). Scan stopped at (%ls)", __ConvertNativeErrorToMessage(errno), pEnd);
157 SysTryReturnResult(NID_BASE, !(errno != 0 && (!Double::Compare(tmpRet, HUGE_VAL)|| !Double::Compare(tmpRet, -HUGE_VAL))),
158 E_NUM_FORMAT, "Parsed value cannot fit into a Double.");
165 Double::ToChar(void) const
167 return static_cast<char> (value);
171 Double::ToShort(void) const
173 return static_cast<short> (value);
177 Double::ToInt(void) const
179 return static_cast<int> (value);
183 Double::ToLong(void) const
185 return static_cast<long> (value);
189 Double::ToLongLong(void) const
191 return static_cast<long long> (value);
195 Double::ToFloat(void) const
197 return static_cast<float> (value);
201 Double::ToDouble(void) const
207 Double::ToString(void) const
209 return(Double::ToString(value));
213 Double::ToString(double value)
215 const static unsigned int MAX_DIG = 17 + 3;
216 const static unsigned int DOUBLE_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
218 if (Double::IsNaN(value))
220 return String(L"NaN");
222 else if (Double::IsInfinity(value))
224 return String(L"Infinity");
228 wchar_t sValue[DOUBLE_LENGTH_MAX + 1] = {0, };
229 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%#lg", value);
230 return String(sValue);
235 Double::ToBits(double value)
238 bitRep.value = value;
239 return (long long) bitRep.rep;
243 Double::ToDoubleFromBits(long long value)
251 Double::IsFinite(double d)
253 return((isfinite(d) != 0) ? true : false);
257 Double::IsInfinity(void) const
259 return(Double::IsInfinity(value));
263 Double::IsInfinity(double value)
265 return(!Double::IsFinite(value));
269 Double::IsNaN(void) const
271 return(Double::IsNaN(value));
275 Double::IsNaN(double value)
277 return((isnan(value) != 0) ? true : false);
281 Double::GetMaxValue(void)
287 Double::GetMinValue(void)