/ * STRUCT PLACE: How many numbers of numbers are available * / template struct place;
Template struct place {static const Int value = place :: value 1; static const INT plane = value;}
Template <> struct place <0> {static const INT value = 0; static const int place = 1;}; / * struct SQRT: Open square, resulting for value * / template struct sqrt;
/ * STRUCT EXP10: 10 N times power, that is, 1 after adding n zero * / template struct exp10 {static constling long value = EXP10 :: Value * 10;};
Template <> struct exp10 <0> {static constling long value = 1;
/ * Struct SQRTTWOPLACE: Square root of the integer below the number below * / template struct sqrtwoplace {static constling long value = sqrttwoplace :: value;};
#define SQRTTWOPLACEDEF (NN, N) Template <> struct sqrttwoplace / {static constling long value = n;}
SqrtTwoPlaceDef (0, 0); SqrtTwoPlaceDef (1, 1); SqrtTwoPlaceDef (4, 2); SqrtTwoPlaceDef (9, 3); SqrtTwoPlaceDef (16, 4); SqrtTwoPlaceDef (25, 5); SqrtTwoPlaceDef (36, 6); SqrtTwoPlaceDef (49, 7); SQRTTWOPLACEDEF (64, 8); SQRTTWOPLACEDEF (81, 9);
/ * Struct SQRTDIV: Request N-open Square * / Template struct sqrtdiv;
Template struct sqrtdiv {private: static constling long x = (n / x0 x0) / 2; static const bool finish = (x * x <= n ) && ((x 1) * (x 1)> n);
Public: static constling long value = sqrtdiv :: value;};
Template struct sqrtdiv {static constling long value = x;}
/ * Struct SQRT: Open Square * / Template struct sqrt {private: static const int place = place :: value; static const INT ZERO = (Place% 2 == 1)? Place - 1 : Place - 2; Static constling long head = n / expression :: value; static constling long x0 = sqrtwoplace :: value * ev10 :: value;
PUBLIC: Static constling long value = sqrtdiv :: value;};
Template <> struct SQRT <0> {static constling long value = 0;}; / * struct fraction: score, first is a molecule, SECOND is a denominator. Value is Double value * / template struct fraction;
Template struct fract {static constling long first = m; static constling long second = n;
Static const Double value = static_cast (first) / second;
/ * Struct Fractionsqrt: Open the square. Molecule, denominator simultaneous opening * / template struct Fractionsqrt {private: static constling long first1 = fractiontype :: first; static constling long second1 = fractype :: second;
STATIC const line long first = SQRT :: value; static const line long second = SQRT :: value; public: typedef Fraction valueetype;
/ * Struct Fractionsqrt2: Open square, but maintain a fractal constant * / template struct fractionsqrt2 {private: static constling long first1 = fractiontype :: first;
STATIC constrays :: second; static constling long first = sqrt :: value; public: typedef Fraction valueType;
/ * Struct FractionAdd: an adder, the result is ValueType * / template struct FractionAdd {private: static const long long first1 = FractionType1 :: first; static const long long second1 = FractionType1 :: second; static const long long first2 = FractionType2 :: first; static const long long second2 = FractionType2 :: second; static const long long first = first1 * second2 first2 * second1; static const long long second = second1 * second2; public: typedef Fraction ValueType;}; template struct FractionAdd , Fraction > {private: static const long long first = first1 First2; Static constling long second = second; public: typedef Fraction valueType;};
/ * Struct FractionSub: subtraction result ValueType * / template struct FractionSub {private: static const long long first1 = FractionType1 :: first; static const long long second1 = FractionType1 :: second; static const long long first2 = FractionType2 :: first; static const long long second2 = FractionType2 :: second; static const long long first = first1 * second2 - first2 * second1; static const long long second = second1 * second2; public: typedef Fraction VALUETYPE;
template struct FractionSub , Fraction > {private: static const long long first = First1 - First2; static const long long second = Second; public: typedef Fraction ValueType;}; / * struct FractionMul: multiplication, the result ValueType * / template struct FractionMul {private: static const long long first1 = FractionType1 :: first ; static const long long first2 = FractionType2 :: first;; static const long long second1 = :: second FractionType1 static const long long second2 = FractionType2 :: second; static const long long first = first1 * first2; static const long long second = Second1 * second2; public: typef Fraction valueetype;
template struct FractionMul , Fraction > {private: static const long long first = First2; static const long long second = Second1; PUBLIC: TYPEDEF FRACTION valueType;};
template struct FractionMul , Fraction > {private: static const long long first = First2; static const long long second = Second1; PUBLIC: TYPEDEF FRACTION valueType;};
template struct FractionMul , Fraction > {private: static const long long first = 1; static const long long second = 1; public: typedef Fraction ValueType;}; / * struct FractionDiv: division, the result is ValueType * / template struct FractionDiv {private: static const long long first1 = FractionType1 :: first; static const long long second1 = FractionType1 :: second; static const long long first2 = FractionType2 :: first; static const long long second2 = FractionType2 :: second; static const long long first = first1 * second2; static const long long second = first2 * second1; public: Typedef Fraction valuepe;};
template struct FractionDiv , Fraction > {private: static const long long first = First1; static const long long second = First2; PUBLIC: TYPEDEF FRACTION valueType;};
template struct FractionDiv , Fraction > {private: static const long long first = Second2; static const long long second = Second1; PUBLIC: TYPEDEF FRACTION valueType;};
template struct FractionDiv , Fraction > {private: static const long long first = 1; static const long long second = 1; public: typedef Fraction ValueType;}; / * struct FractionAdjust: adjusting the denominator, the denominator is that the size of the SECOND * / template struct FractionAdjust {private: static const long long first1 = FractionType :: STATIC constling long second1 = fracthenpe :: second;
STATIC const line long first2 = (second1
static const long long div = second2 / SECOND; static const long long first = first2 / div; static const long long second = second2 / div; public: typedef Fraction ValueType;}; / * struct PI: is a FRAction, * / STRUCT PI;
Static constling long base = 100000000LL; typedef frame <2, 1> two; // constant 2
/ * Struct PiItem: is a * / template struct piitem {private: typedef typef typeName PiItem :: valueType value1; typefedef Typename PIITEM :: itemtype item1;
typedef typename FractionAdd :: ValueType ItemAddTwo; public: typedef typename FractionSqrt2 :: ValueType ItemType; private: typedef typename FractionDiv :: TwoDivItem ValueType; typedef typename FractionMul : : ValueType ValueResult; public: typedef typename FractionAdjust :: ValueType ValueType;}; template <> struct PIItem <0> {typedef Two ValueType; typedef Fraction <0, BASE> ItemType;};
Struct PI: Public Piitem <50> :: valueetype {};
#include #include using namespace std;
INTMAIN () {cout.Precision (10); cout << Piitem <0> :: itemtype :: value << endl; cout.precision (10); cout << piitem <1> :: itemtype :: value << Endl; cout.Precision (10); cout << Piitem <2> :: itemtype :: value << endl; cout.precision (10); cout << piitem <4> :: itemtype :: value << Cout.Precision (10); COUT << Piitem <8> :: itemtype :: value << endl; cout.precision (10); cout << piitem <16> :: itemtype :: value << endl; cout. Precision (10); cout << Piitem <32> :: itemtype :: value << endl; cout.precision (10); cout << piitem <64> :: itemtype :: value << Endl; cout.precision 10); COUT << PIITEM <128> :: itemtype :: value << endl; cout.precision (10); cout << pi :: value << endl; system ("pause"); return 0;}