00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef FloatH
00033 #define FloatH
00034
00035 #include <zeusbase/System/ZObject.h>
00036 #include <zeusbase/System/ZValueType.hpp>
00037 #include <zeusbase/System/Time.h>
00038 #include <zeusbase/Math/Math.hpp>
00039
00040 BEGIN_NAMESPACE_Zeus
00041
00042
00047
00048 zeusbase_class TFloat : public TZValueType<Float>
00049 {
00050 public:
00051
00054
00055 enum EFloatClass
00056 {
00057 etRegular = 0,
00058 etNaN = 1,
00059 etPosInfinity = 2,
00060 etNegInfinity = 3
00061 };
00062
00063
00066
00067 inline TFloat() : TZValueType<Float>()
00068 {}
00069
00070
00073
00074 inline TFloat(const TFloat& rValue) : TZValueType<Float>(rValue)
00075 {}
00076
00077
00080
00081 inline TFloat(Float fValue) : TZValueType<Float>(fValue)
00082 {}
00083
00084
00087
00088 inline TFloat(const IString& rValue) : TZValueType<Float>()
00089 {
00090 m_tValue = rValue.toFloat(0.0);
00091 }
00092
00093
00096
00097 inline ~TFloat()
00098 {}
00099
00100 bool equals(Float fValue, Float fPrecision) const;
00101
00102
00103 inline Float round() const { return (isNaN() ? TFloat::NaN : TMath::round(m_tValue)); }
00104 inline Float roundEx(Float fPrecision) const { return (isNaN() ? TFloat::NaN : TMath::roundEx(m_tValue, fPrecision)); }
00105 inline Float ceil() const { return (isNaN() ? TFloat::NaN : TMath::ceil(m_tValue)); }
00106 inline Float floor() const { return (isNaN() ? TFloat::NaN : TMath::floor(m_tValue)); }
00107 inline Int roundInt() const { return (isNaN() ? 0 : TMath::roundInt(m_tValue)); }
00108 inline Int ceilInt() const { return (isNaN() ? 0 : TMath::ceilInt(m_tValue)); }
00109 inline Int floorInt() const { return (isNaN() ? 0 : TMath::floorInt(m_tValue)); }
00110 inline Float exp() const { return (isNaN() ? TFloat::NaN : TMath::exp(m_tValue)); }
00111 inline Float abs() const { return (isNaN() ? TFloat::NaN : ::fabs(m_tValue)); }
00112
00113 inline Float pow(const Float& rfExp) const { return (isNaN() ? TFloat::NaN : ::pow(m_tValue, rfExp)); }
00114 inline Float cos() const { return (isNaN() ? TFloat::NaN : ::cos(m_tValue)); }
00115 inline Float sin() const { return (isNaN() ? TFloat::NaN : ::sin(m_tValue)); }
00116 inline Float tan() const { return (isNaN() ? TFloat::NaN : ::tan(m_tValue)); }
00117 inline Float acos() const { return (isNaN() ? TFloat::NaN : ::acos(m_tValue)); }
00118 inline Float asin() const { return (isNaN() ? TFloat::NaN : ::asin(m_tValue)); }
00119 inline Float atan() const { return (isNaN() ? TFloat::NaN : ::atan(m_tValue)); }
00120 inline Float sqrt() const { return (isNaN() ? TFloat::NaN : ::sqrt(m_tValue)); }
00121 inline Float ln() const { return (isNaN() ? TFloat::NaN : ::log(m_tValue)); }
00122 inline Float log(const Float& rfBase) const { return (isNaN() ? TFloat::NaN : ::log(m_tValue) / ::log(rfBase)); }
00123
00124 TString format(Float fRoundPrecision = 0,
00125 Uint uiTailingZeros = 0,
00126 Uint uiLeadingZeros = 1) const;
00127 bool isZero(Float fPrecision = FLOAT_PRECISION) const;
00128 bool isNonZero(Float fPrecision = FLOAT_PRECISION) const;
00129 bool isInfinity() const;
00130 bool isPosInfinity() const;
00131 bool isNegInfinity() const;
00132 bool isNaN() const;
00133 Float getFactorial() const;
00134
00135
00136
00137 bool operator ==(const TFloat& rValue)const;
00138 bool operator ==(const Float fValue)const;
00139 bool operator !=(const TFloat& rValue)const;
00140 bool operator !=(const Float fValue)const;
00141
00142
00143 static bool equalsFloat(Float fValue1, Float fValue2, Float fPrecision = FLOAT_PRECISION);
00144 static bool isZero(const Float& fValue, Float fPrecision = FLOAT_PRECISION);
00145 static bool isNonPositive(const Float& f, Float fPrecision = FLOAT_PRECISION);
00146 static bool isNonNegative(const Float& f, Float fPrecision = FLOAT_PRECISION);
00147 inline static bool isNonZero(const Float& fValue, Float fPrecision = FLOAT_PRECISION) { return !isZero(fValue, fPrecision); }
00148 inline static bool isPositive(const Float& fValue, Float fPrecision = FLOAT_PRECISION) { return !isNonPositive(fValue, fPrecision); }
00149 inline static bool isNegative(const Float& fValue, Float fPrecision = FLOAT_PRECISION) { return !isNonNegative(fValue, fPrecision); }
00150 inline static bool isGreater(const Float& rfValue1, const Float& rfValue2, const Float& rfPrecision = FLOAT_PRECISION) { return isNegative(rfValue2- rfValue1, rfPrecision); }
00151 inline static bool isGreaterEqual(const Float& rfValue1, const Float& rfValue2, const Float& rfPrecision = FLOAT_PRECISION) { return isNonPositive(rfValue2- rfValue1, rfPrecision); }
00152 inline static bool isLower(const Float& rfValue1, const Float& rfValue2, const Float& rfPrecision = FLOAT_PRECISION) { return isPositive(rfValue2- rfValue1, rfPrecision); }
00153 inline static bool isLowerEqual(const Float& rfValue1, const Float& rfValue2, const Float& rfPrecision = FLOAT_PRECISION) { return isNonNegative(rfValue2- rfValue1, rfPrecision); }
00154
00155 static bool isInfinity(Float fValue);
00156 static bool isNaN(Float fValue);
00157 static EFloatClass getClassType(Float fValue);
00158
00159 static Float getFactorial(Float fVal);
00160
00161 static Float randomFloat(Float fFrom, Float fTo);
00162 static Float32 randomFloat32();
00163 static Float64 randomFloat64();
00164
00165
00166 static const Float NaN;
00167 static const Float Infinity;
00168 static const Float NegInfinity;
00169 static const Float MaxValue64bit;
00170 static const Float MinValue64bit;
00171 static const Float MaxValue32bit;
00172 static const Float MinValue32bit;
00173
00174 protected:
00175 static Float m_fStaticPrecision;
00176 };
00177
00178
00179
00184
00185 inline bool TFloat::isInfinity() const
00186 {
00187 return TFloat::isInfinity(m_tValue);
00188 }
00189
00190
00193
00194 inline bool TFloat::isPosInfinity() const
00195 {
00196 return (isInfinity() && m_tValue > 0);
00197 }
00198
00199
00202
00203 inline bool TFloat::isNegInfinity() const
00204 {
00205 return (isInfinity() && m_tValue < 0);
00206 }
00207
00208
00213
00214 inline bool TFloat::isNaN() const
00215 {
00216 return TFloat::isNaN(m_tValue);
00217 }
00218
00219
00223
00224 inline Float TFloat::getFactorial() const
00225 {
00226 return TFloat::getFactorial(m_tValue);
00227 }
00228
00229
00230
00233
00234 inline bool TFloat::operator ==(const TFloat& rValue)const
00235 {
00236 return TFloat::equalsFloat(m_tValue, rValue.m_tValue, m_fStaticPrecision);
00237 }
00238
00239
00242
00243 inline bool TFloat::operator ==(const Float fValue)const
00244 {
00245 return TFloat::equalsFloat(m_tValue, fValue, m_fStaticPrecision);
00246 }
00247
00248
00251
00252 inline bool TFloat::operator !=(const TFloat& rValue)const
00253 {
00254 return !TFloat::equalsFloat(m_tValue, rValue.m_tValue, m_fStaticPrecision);
00255 }
00256
00257
00260
00261 inline bool TFloat::operator !=(const Float fValue)const
00262 {
00263 return !TFloat::equalsFloat(m_tValue, fValue, m_fStaticPrecision);
00264 }
00265
00266
00273
00274 inline bool TFloat::equals(Float fValue, Float fPrecision) const
00275 {
00276 return TFloat::equalsFloat(m_tValue, fValue, fPrecision);
00277 }
00278
00279
00287
00288 inline bool TFloat::isZero(Float fPrecision ) const
00289 {
00290 return TFloat::equalsFloat(m_tValue, 0, fPrecision);
00291 }
00292
00293
00301
00302 inline bool TFloat::isNonZero(Float fPrecision ) const
00303 {
00304 return TFloat::isNonZero(m_tValue, fPrecision);
00305 }
00306
00307
00308
00316
00317 inline bool TFloat::equalsFloat(Float fValue1, Float fValue2, Float fPrecision)
00318 {
00319 EFloatClass eClass1 = TFloat::getClassType(fValue1);
00320 EFloatClass eClass2 = TFloat::getClassType(fValue2);
00321
00322
00323
00324
00325 return (eClass1 == eClass2 &&
00326 (eClass1 != etRegular || ::fabs(fValue1 - fValue2) < fPrecision));
00327 }
00328
00329
00336
00337 inline bool TFloat::isZero(const Float& fValue, Float fPrecision )
00338 {
00339 return (::fabs(fValue) < fPrecision);
00340 }
00341
00342
00351
00352 inline bool TFloat::isNonNegative(const Float& f, Float fPrecision )
00353 {
00354 return (f > -fPrecision);
00355 }
00356
00357
00366
00367 inline bool TFloat::isNonPositive(const Float& f, Float fPrecision )
00368 {
00369 return (f < fPrecision);
00370 }
00371
00372
00378
00379 inline Float TFloat::randomFloat(Float fFrom, Float fTo)
00380 {
00381 return TTime::randomFloat(fFrom, fTo);
00382 }
00383
00384
00387
00388 inline Float32 TFloat::randomFloat32()
00389 {
00390 return (Float32) TTime::randomFloat(TFloat::MinValue32bit, TFloat::MaxValue32bit);
00391 }
00392
00393
00396
00397 inline Float64 TFloat::randomFloat64()
00398 {
00399 return TTime::randomFloat(TFloat::MinValue64bit, TFloat::MaxValue64bit);
00400 }
00401
00402
00403 END_NAMESPACE_Zeus
00404
00405
00406 #endif