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 FBaseFloat.cpp
19 * @brief This is the implementation file for Float class.
30 #include <FBaseFloat.h>
31 #include <FBaseResult.h>
32 #include <FBaseSysLog.h>
34 namespace Tizen { namespace Base
40 FloatBitRep(float f = 0.0)
45 FloatBitRep(unsigned int i)
54 Float::Float(float value)
60 Float::Float(const Float& value)
71 Float::operator =(const Float& rhs)
81 Float::Compare(float f1, float f2)
102 Float::CompareTo(const Float& value) const
104 return(Float::Compare(this->value, value.value));
108 Float::Equals(const Object& obj) const
110 const Float* pOther = dynamic_cast <const Float*>(&obj);
117 if (Float::Compare(this->value, (*pOther).value) == 0)
128 Float::GetHashCode(void) const
130 float* pTemp = const_cast<float*> (&value);
131 int* pValue = reinterpret_cast<int*> (pTemp);
136 Float::GetHashCode(float val)
138 int* pValue = reinterpret_cast<int*> (&val);
143 Float::Parse(const String& s, float& ret)
145 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
146 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
149 wchar_t* pEnd = null;
150 float tmpRet = wcstof(s.GetPointer(), &pEnd);
151 SysTryReturn(NID_BASE, (!Float::Compare(pEnd[0], 0)), E_NUM_FORMAT, E_NUM_FORMAT,
152 "[%s] Float parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
153 SysTryReturn(NID_BASE, !((!Float::Compare(tmpRet, HUGE_VAL) || !Float::Compare(tmpRet, -HUGE_VAL)) && (errno != 0)),
154 E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
161 Float::ToChar(void) const
163 return static_cast<char> (value);
167 Float::ToShort(void) const
169 return static_cast<short> (value);
173 Float::ToInt(void) const
175 return static_cast<int> (value);
179 Float::ToLong(void) const
181 return static_cast<long> (value);
185 Float::ToLongLong(void) const
187 return static_cast<long long> (value);
191 Float::ToFloat(void) const
197 Float::ToDouble(void) const
199 return static_cast<double> (value);
203 Float::ToString(void) const
205 return(Float::ToString(value));
209 Float::ToString(float value)
211 const static unsigned int MAX_DIG = 7 + 3;
212 const static unsigned int FLOAT_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
214 if (Float::IsNaN(value))
216 return String(L"NaN");
218 else if (Float::IsInfinity(value))
220 return String(L"Infinity");
224 wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
225 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%g", value);
227 return String(sValue);
232 Float::ToBits(float value)
235 bitRep.value = value;
240 Float::ToFloatFromBits(int value)
248 Float::IsFinite(float value)
250 return((isfinite(value) != 0) ? true : false);
254 Float::IsInfinity(void) const
256 return(Float::IsInfinity(value));
260 Float::IsInfinity(float value)
262 return(!Float::IsFinite(value));
266 Float::IsNaN(void) const
268 return(Float::IsNaN(value));
272 Float::IsNaN(float value)
274 return((isnan(value) != 0) ? true : false);
278 Float::GetMaxValue(void)
284 Float::GetMinValue(void)