这是一堆我自己写完都怀疑人生的代码。

或许我见识太少了吧……

实现一个实数类。这个类也可以看作是分数类。实现了基本的四则运算和比较。另外,与整数或小数的转换还没有实现,无限循环小数转分数也暂时没有实现。但我早晚会写的。。。

咕咕咕。

这里的重头戏是模版。

啊……模版,我边学边卖,才知道模版还可以这么玩。

这次只有一个文件……

来体会一下来自C++的恐惧吧!

BUGFIX:

1. 修复operator=不能对自身影响的BUG;(2018.10.10)

2. 修复小数转分数时,可能造成整数溢出的BUG;(2018.10.10)

Note:

1. 无限循环小数转分数实际上非常简单,就是:

\[RationalResult = \frac{\frac {LoopBodyNum} {(10 ^ {LoopBodyNum.length} - 1)}} {10 ^ {NotLoopBodyNum.length}} + NotLoopBodyNum
\]

(我去你妹的垃圾markdown...对不起,我要平和。对,平和心气。)

    RationalResult  = (LoopBodyNum / (pow(10, LoopBodyNum.length) - 1)) / pow(10, NotLoopBodyNum.length) + NotLoopBodyNum

Lift a chestnut:

  0.52727272727272.... = ?

so easy.

  0.52727272727272.... = 0.5 + 0.02727272727272....

  A/10 = 0.2727272727272.... / 10 = 0.27 * 1 + 0.27 * 0.01 + 0.27 * 0.0001 + .... / 10

上面的不就是等比数列吗?!

\[A = \frac {0.27 * (1 - 0.01^n)} {1 - 0.01}
\]

因为 n 是趋于无穷大,那么:

\[\lim_{n \to +\infty} 0.01^n = 0
\]

所以,

\[A = \frac {3} {11}
\]

(小学没毕业,希望自己没有把最后的结果算错)

Rational.h

#pragma once
#include "stdinc.h" namespace Lunacia
{
class __Rational {}; template<typename NumT = int>
class Rational final : public __Rational
{
public:
Rational(); template<typename NumT2>
Rational(const Rational<NumT2>& ra); Rational(NumT numer, NumT denom);
Rational(NumT numer, NumT denom, bool isDenomFixed);
~Rational(); public:
static NumT Gcd(NumT a, NumT b);
static NumT Lcm(NumT a, NumT b); static bool IsEven(NumT num);
static NumT GetSymbol(NumT num); static bool IsEqualFloating(long double a, long double b); void SetDenomFixed(bool isFix);
bool GetDenomFixed() const; void GetRationalString(std::string& __out resStr) const;
void GetRationalString(std::wstring& __out resStr) const; public:
///operator+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
template<typename ParamT,
typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator +(const ParamT rRational) const; template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator +(const ParamT& rRational) const; Rational<NumT> operator +(const Rational& rRational) const; ///operator-----------------------------------------------------------------------
template<typename ParamT,
typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator -(const ParamT& rRational) const; template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator -(const ParamT& rRational) const; Rational<NumT> operator -(const Rational& rRational) const; ///operator******************************************************
template<typename ParamT,
typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator *(const ParamT& rRational) const; template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator *(const ParamT& rRational) const; Rational<NumT> operator *(const Rational& rRational) const; ///operator///////////////////////////////////////////////////////
template<typename ParamT,
typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator /(const ParamT& rRational) const; template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator /(const ParamT& rRational) const; Rational<NumT> operator /(const Rational& rRational) const; ///operator=======================================================
template<typename ParamT,
typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator =(const ParamT& rRational); template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
Rational<NumT> operator =(const ParamT& rRational); Rational<NumT> operator =(const Rational& rRational); ///operator== == == == == == == == == == == == == == == == == == == == == == ==
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT
> ::type * = nullptr>
bool operator ==(const ParamT& rRational) const; template<typename ParamT,
typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
bool operator ==(const ParamT& rRational) const; ///operator!= != != != != != != != != != != != != != != != != != != != != != != != != != != != !=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value||
std::is_base_of<__Rational, ParamT>::value,
ParamT
> ::type * = nullptr>
bool operator !=(const ParamT& rRational) const; ///operator>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT
> ::type * = nullptr>
bool operator >(const ParamT& rRational) const; ///operator <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT
> ::type * = nullptr>
bool operator <(const ParamT& rRational) const ; ///operator >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
bool operator >=(const ParamT& rRational); ///operator <= <= <= <= <= <= <= <= <= <= <= <= <= <= <= <= <= <= <= <=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
bool operator <=(const ParamT& rRational); ///operator+= += += += += += += += += += += += += += += += += += +=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
Rational<NumT> operator +=(const ParamT& rRational); ///operator-= -= -= -= -= -= -= -= -= -= -= -= -= -= -= -= -= -= -= -=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
Rational<NumT> operator -=(const ParamT& rRational); ///operator*= *= *= *= *= *= *= *= *= *= *= *= *= *= *= *= *= *=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
Rational<NumT> operator *=(const ParamT& rRational); ///operator/= /= /= /= /= /= /= /= /= /= /= /= /= /= /= /= /= /=
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * = nullptr>
Rational<NumT> operator /=(const ParamT& rRational); ///CHECK IT :
template<typename ParamT,
typename std::enable_if <
std::is_floating_point <ParamT> ::value,
ParamT> ::type * = nullptr>
operator ParamT(); ///CHECK IT :
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value,
ParamT> ::type * = nullptr>
operator ParamT(); ///operator() () () () () () () () () () () () () () () () () () () () () () () ()
void operator ()(const NumT& newNumer, const NumT& newDenmo);
void operator ()(const Rational& newRa); public:
void Reciprocal();
static void Reciprocal(Rational<NumT>& __out res); NumT Reduction(Rational& other); /*Return new denom.*/
static NumT Reduction(Rational& raA, Rational& raB); void DenomFix(bool isForce = false); template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type * = nullptr>
static void FloatingToRational(const ParamT& num, Rational<NumT>& res); public:
NumT _denom, _numer; private:
bool m_isDenomFixed;
}; ////////////////////////////////////////////////////////////////////////////
template<typename NumT>
Rational<NumT>::Rational(NumT numer, NumT denom) :
Rational(numer, denom, false)
{
} template<typename NumT>
Rational<NumT>::Rational(NumT numer, NumT denom, bool isDenomFixed) :
_denom(denom),
_numer(numer),
m_isDenomFixed(isDenomFixed)
{
static_assert(std::is_integral<NumT>::value, "[Error] Error Number Type, Must be an integer.");
DenomFix();
} template<typename NumT>
Rational<NumT>::Rational() :
Rational(0, 1, false)
{
} template<typename NumT>
Rational<NumT>::~Rational()
{
} template<typename NumT>
NumT Rational<NumT>::Gcd(NumT x, NumT y)
{
static_assert(std::is_integral<NumT>::value, "[Error] Error Number Type, Must be an integer."); if (x < 0 || y < 0)
{
return -1;
} if (x < y) return Gcd(y, x); if (y == 0) return x; if (IsEven(x))
{
if (IsEven(y))
return (Gcd(x >> 1, y >> 1) << 1);
else
return Gcd(x >> 1, y);
}
else
{
if (IsEven(y))
return Gcd(x, y >> 1);
else
return Gcd(y, x - y);
}
} template<typename NumT>
NumT Rational<NumT>::Lcm(NumT a, NumT b)
{
static_assert(std::is_integral<NumT>::value, "[Error] Error Number Type, Must be an integer."); NumT max = (a > b) ? a : b;
NumT min = a + b - max;
NumT lcm = -1;
for (NumT i = 1; i <= min; ++i)
{
lcm = max * i;
if (lcm % min == 0)
{
break;
}
}
return lcm;
} template<typename NumT>
inline bool Rational<NumT>::IsEven(NumT num)
{
static_assert(std::is_integral<NumT>::value, "[Error] Error Number Type, Must be an integer.");
return (num & 1) == 0;
} template<typename NumT>
inline NumT Rational<NumT>::GetSymbol(NumT num)
{
if (num == 0) return 0;
return num / std::abs(num);
} template<typename NumT>
inline bool Rational<NumT>::IsEqualFloating(long double a, long double b)
{
return std::abs(a - b) < DoubleESP;
} template<typename NumT>
void Rational<NumT>::SetDenomFixed(bool isFix)
{
m_isDenomFixed = isFix;
DenomFix();
} template<typename NumT>
inline bool Rational<NumT>::GetDenomFixed() const
{
return m_isDenomFixed;
} template<typename NumT>
void Rational<NumT>::GetRationalString(std::string & resStr) const
{
resStr = std::to_string(_numer) + " / " + std::to_string(_denom);
} template<typename NumT>
void Rational<NumT>::GetRationalString(std::wstring & resStr) const
{
resStr = std::to_wstring(_numer) + L" / " + std::to_wstring(_denom);
} template<typename NumT>
inline void Rational<NumT>::Reciprocal()
{
std::swap(_numer, _denom);
} template<typename NumT>
inline static void Rational<NumT>::Reciprocal(Rational<NumT>& res)
{
res(_denom, _numer);
} template<typename NumT>
inline void Rational<NumT>::DenomFix(bool isForce)
{
if (m_isDenomFixed || isForce)
{
NumT gcd = Rational<NumT>::Gcd(_numer, _denom);
if (gcd == -1)
{
//WarningThrow("Return gcd is -1. m_numer or m_denom is 0.");
return;
}
_denom /= gcd;
_numer /= gcd;
} if (_denom < 0)
{
_denom *= -1;
_numer *= -1;
}
} template<typename NumT>
NumT Rational<NumT>::Reduction(Rational& other)
{
NumT lcm = Rational<NumT>::Lcm(_denom, other._denom); _numer *= lcm / _denom;
_denom = lcm; other._numer *= lcm / other._denom;
other._denom = lcm; return lcm;
} template<typename NumT>
inline NumT Rational<NumT>::Reduction(Rational & raA, Rational & raB)
{
raA.Reduction(raB);
return raA._denom;
} template<typename NumT>
template<typename NumT2>
Rational<NumT>::Rational(const Rational<NumT2>& ra)
{
_denom = static_cast<NumT>(ra._denom);
_numer = static_cast<NumT>(ra._numer);
SetDenomFixed(ra.GetDenomFixed());
//(*this)(ra); //Error: Recursive forever.
} ///operator+
template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator+(const ParamT rRational) const
{
Rational<NumT> resRa(_numer + static_cast<NumT>(rRational) * _denom, _denom);
resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator+(const ParamT & rRational) const
{
Rational<int64_t> rightVal;
FloatingToRational(rRational, rightVal); return rightVal + *this;
} template<typename NumT>
inline Rational<NumT> Rational<NumT>::operator+(const Rational & rRational) const
{
Rational rightVal = rRational;
Rational<NumT> resRa(*this);
Reduction(rightVal, resRa); resRa._numer += rightVal._numer;
resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} ///operator-
template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator-(const ParamT & rRational) const
{
Rational<NumT> resRa(_numer - static_cast<NumT>(rRational) * _denom, _denom); resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator-(const ParamT & rRational) const
{
Rational<int64_t> rightVal; FloatingToRational(rRational, rightVal);
Rational<NumT> resRa(*this); return resRa - rightVal;
} template<typename NumT>
inline Rational<NumT> Rational<NumT>::operator-(const Rational & rRational) const
{
Rational rightVal = rRational;
Rational<NumT> resRa(*this); Reduction(rightVal, resRa);
resRa._numer -= rightVal._numer; resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} ///operator*
template<typename NumT>
template<typename ParamT, typename std::enable_if <std::is_integral <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator*(const ParamT & rRational) const
{
Rational<NumT> resRa(_numer * rRational, _denom); resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} template<typename NumT>
template<typename ParamT, typename std::enable_if <std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator*(const ParamT & rRational) const
{
Rational<int64_t> rightVal;
FloatingToRational(rRational, rightVal); return *this * rightVal;
} template<typename NumT>
inline Rational<NumT> Rational<NumT>::operator*(const Rational & rRational) const
{
Rational<NumT> resRa;
resRa(rRational._numer * _numer, rRational._denom * _denom); resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} ///operator /
template<typename NumT>
template<typename ParamT, typename std::enable_if <std::is_integral <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator/(const ParamT & rRational) const
{
Rational<NumT> resRa;
if (_numer % rRational == 0)
{
resRa(_numer / rRational, _denom);
}
else
{
resRa(_numer, _denom * rRational);
} resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} template<typename NumT>
template<typename ParamT, typename std::enable_if <std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator/(const ParamT & rRational) const
{
Rational<int64_t> rightVal;
Rational<NumT> resRa;
FloatingToRational(rRational, rightVal); resRa(*this / rightVal);
return resRa;
} template<typename NumT>
inline Rational<NumT> Rational<NumT>::operator/(const Rational & rRational) const
{
Rational<NumT> resRa;
resRa(_numer * rRational._denom, _denom * rRational._numer); resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} ///operator =
template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_integral <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator=(const ParamT & rRational)
{
Rational<NumT> resRa(_numer = rRational * _denom, _denom); resRa.SetDenomFixed(m_isDenomFixed);
return resRa;
} template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline Rational<NumT> Rational<NumT>::operator=(const ParamT & rRational)
{
Rational<NumT> rightVal;
Rational<NumT> resRa; FloatingToRational(rRational, rightVal); resRa(rightVal);
(*this)(rightVal); return resRa;
} template<typename NumT>
inline Rational<NumT> Rational<NumT>::operator=(const Rational & rRational)
{
Rational<NumT> resRa; resRa(rRational);
(*this)(rRational);
return resRa;
} ///operator ==
template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type *>
inline bool Rational<NumT>::operator==(const ParamT & rRational) const
{
return (*this - rRational)._numer == 0;
} template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline bool Rational<NumT>::operator==(const ParamT & rRational) const
{
return IsEqualFloating(1.0 * _numer / _denom, rRational);
} ///operator ()
//Base Initialization function.
template<typename NumT>
inline void Rational<NumT>::operator()(const NumT& newNumer, const NumT& newDenmo)
{
static_assert(std::is_integral<NumT>::value, "[Error] Error Number Type, Must be an integer."); _denom = newDenmo;
_numer = newNumer;
} //Base Initialization function.
template<typename NumT>
inline void Rational<NumT>::operator()(const Rational & newRa)
{
(*this)(newRa._numer, newRa._denom);
SetDenomFixed(newRa.GetDenomFixed());
} ///
template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type *>
inline bool Rational<NumT>::operator!=(const ParamT & rRational) const
{
return !(*this == rRational);
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type *>
inline bool Rational<NumT>::operator>(const ParamT & rRational) const
{
return (*this - rRational)._numer > 0;
} ////operator < >= <=
template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type *>
inline bool Rational<NumT>::operator<(const ParamT & rRational) const
{
return (*this - rRational)._numer < 0;
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT
> ::type *>
inline bool Rational<NumT>::operator>=(const ParamT & rRational)
{
return !(*this < rRational);
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * >
inline bool Rational<NumT>::operator<=(const ParamT & rRational)
{
return !(*this > rRational);
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * >
inline Rational<NumT> Rational<NumT>::operator+=(const ParamT & rRational)
{
return *(this) = *(this) + rRational;
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * >
inline Rational<NumT> Rational<NumT>::operator-=(const ParamT & rRational)
{
return *(this) = *(this) - rRational;
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * >
inline Rational<NumT> Rational<NumT>::operator*=(const ParamT & rRational)
{
return *(this) = *(this) * rRational;
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value ||
std::is_floating_point <ParamT> ::value ||
std::is_base_of<__Rational, ParamT>::value,
ParamT> ::type * >
inline Rational<NumT> Rational<NumT>::operator/=(const ParamT & rRational)
{
return *(this) = *(this) / rRational;
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_floating_point <ParamT> ::value,
ParamT> ::type *>
inline Rational<NumT>::operator ParamT()
{
return static_cast<ParamT>((_numer * 1.0) / _denom);
} template<typename NumT>
template<typename ParamT,
typename std::enable_if <
std::is_integral <ParamT> ::value,
ParamT> ::type *>
inline Rational<NumT>::operator ParamT()
{
return _numer / _denom;
} template<typename NumT>
template<typename ParamT, typename std::enable_if < std::is_floating_point <ParamT> ::value, ParamT > ::type *>
inline void Rational<NumT>::FloatingToRational(const ParamT & num, Rational<NumT>& res)
{
static const long double BaseDenmo = 1e12;
Rational<int64_t> tempRes;
tempRes.SetDenomFixed(res.GetDenomFixed()); tempRes(static_cast<int64_t>(num * BaseDenmo), static_cast<int64_t>(BaseDenmo));
tempRes.DenomFix(true); res(tempRes);
} //////////////////////////////////////////////////////////////////////////////////
}; //////////////////////////////////////////////////////////////////////////////////
};

main.cpp 测试主函数


#include "Tilee.h"
#include "Location.h"
#include "stdinc.h"
#include "Random.h"
#include "Rational.h" using namespace Lunacia; //struct Item
//{
// uint32_t weight;
// int id;
//}; int main(void)
{ int a = 12;
int b = 9; int64_t lcm = Rational<int64_t>::Lcm(a, b);
std::cout << lcm << std::endl; Rational<int64_t> rnum(2, 4, true);
Rational<int32_t> rnum2(5, 8, false); std::string aaa;
rnum.GetRationalString(aaa);
std::cout << aaa << std::endl; double da = 0.24;
double dda = 0.24*std::pow(10, 12);
//rnum(rnum2); int iA = 2;
long long iB = 3;
rnum(iA, iB); Rational<int64_t> ra1 = rnum + iB;
rnum + 2.6;
rnum + rnum2; Rational<int32_t> ra2 = rnum - 5;
rnum - 0.25;
rnum - rnum2; Rational<int> ra3 = rnum * 3;
rnum * 1.5;
rnum * rnum2; ra3 = rnum / 4;
Rational ra4 = rnum / 2.50;
rnum / rnum2; bool isEqual = rnum == rnum2;
rnum != rnum2;
rnum > rnum2;
rnum >= rnum2;
rnum < rnum2;
rnum <= rnum2; rnum == 5;
rnum != 5;
rnum > 5;
rnum >= 5;
rnum < 5;
rnum <= 5; long double ldaa = 0.5;
rnum == 1.5;
rnum != 3.5;
rnum > 2.5;
rnum >= 4.5f;
rnum < 6.5;
rnum <= ldaa; rnum(rnum2);
system("pause");
return 0;
}

呵呵呵呵呵,好无聊。

C++ 实数类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. Lua简介

    Lua是一种扩展语言,脚本语言,还没有主程序的概念,类似于插件,也即不能直接使用,必须嵌入在牛逼的语言里使用,如Python. Lua由C语言编写,可以在宿主语言里写一段c程序,让Lua的解释器使用, ...

  3. HDU1063 大数 java

    Exponentiation Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. Object Pascal 语言基础

    Delphi 是以Object Pascal 语言为基础的可视化开发工具,所以要学好Delphi,首先要掌握的就是Object Pascal 语言.Object Pascal语言是Pascal之父在1 ...

  5. scikit-learning API

    API参考 这是scikit学习的类和函数参考.有关详细信息,请参阅完整的用户指南,因为类和功能原始规格可能不足以给出其使用的完整指导. sklearn.base:基类和效用函数 所有估计器的基类. ...

  6. Coefficient Computation (大整数、Java解决)

    Coefficient Computation UVALive8265 题意:计算组合数C(n,k)的值并将值按给定的进制输出. 思路:Java大整数类硬上. PS:刚刚学完Java的大整数类,结果却 ...

  7. 3.24 7.13 Python基础汇总

    对象类型 类型名称 示例 简要说明 备注 数字 int,float,complex 1234,3.14,1.3e5,3+4j 数字大小没有限制 十六进制用0x前缀和0-9,a-f表示 字符串 str ...

  8. Shone.Math开源系列1 — 基于.NET 5实现Math<T>泛型数值计算

    Shone.Math开源系列1 — 基于.NET 5实现Math<T>泛型数值计算 作者:Shone .NET 5 preview 4已经可用了,从微软Build2020给出的信息看,.N ...

  9. 浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法

    浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法 众所周知,把[0,1]区间内的n个随机.相互独立的实数变量\(x_i\)之间的大小关系写成一个排列\(\{p_i\}\),使得\(\f ...

随机推荐

  1. 汉诺塔问题-递归实现-JAVA

    public class hanio { /** * @param args */ public static void main(String[] args) { // TODO Auto-gene ...

  2. zookeeper集群的搭建(个人实践可行)

    linux 上面 zookeeper 集群(最少3个节点192.168.204.128.192.168.204.129.192.168.204.130)安装 1.创建文件夹/usr/local/zoo ...

  3. 将Blender3d软件语言改为中文设置(win各版本+Linux)

    Blender作为开源软件,为全世界的媒体工作者和艺术家而设计,可以被用来进行 3D 可视化,同时也可以创作广播和电影级品质的视频,另外内置的实时 3D 游戏引擎,让制作独立回放的 3D 互动内容成为 ...

  4. css清除浮动方式总结

    1.通过父元素overflow:hidden,缺点:超出部分隐藏,不推荐使用 <!DOCTYPE html> <html lang="en"> <he ...

  5. 前端开发【第二篇: css】

    css概述 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS ...

  6. 语言模型(N-Gram)

    问题描述:由于公司业务产品中,需要用户自己填写公司名称,而这个公司名称存在大量的乱填现象,因此需要对其做一些归一化的问题.在这基础上,能延伸出一个预测用户填写的公司名是否有效的模型出来. 目标:问题提 ...

  7. 利用Django实现webUI展示

    1.说明 最近老大想要做一个webUI界面,为了展示我们数据中心工作内容,需要把各自的工作内容用webUI展示出来.目前我负责的做公司名称归一化的问题. 2.Django实现web 具体实现是完全按照 ...

  8. Linux 下使用umount强行卸载设备

    卸载NFS,结果出现无法卸载的情况 [root@localhost /]# umount /udisk/ umount: /udisk: device is busy umount: /udisk: ...

  9. Groovy学习笔记-Java 5新特性支持

    1.枚举enum enum CoffeeSize{ SHORT, SMALL, MEDIUM, LARGE, MUG } def orderCoffee(size){ print "Coff ...

  10. java多态——基础

    多态 定义: 一个接口,多种实现,就是多种状态     价值: 特点:多态存在的特点,就是必须要有继承.覆盖.父类变量调用子类对象 场景: 首先检查父类中是否有该方法,如果没有,则编译错误:如果有,则 ...