24 #include "srational.h"
33 struct TempTypeTrait<float> {
34 typedef double Result;
38 struct TempTypeTrait<double> {
39 typedef long double Result;
43 template<
typename>
class Complex>
44 struct TempTypeTrait<Complex<T> > {
45 typedef typename TempTypeTrait<T>::Result Result;
48 template<
typename T,
typename A,
49 template<
typename,
typename>
class Complex>
50 struct TempTypeTrait<Complex<T,A> > {
51 typedef typename TempTypeTrait<T>::Result Result;
70 typedef SDecimal<
SBigInt<true,NL11,DefaultDecimalBase>,1,DefaultDecimalBase> TPi1Dec;
72 typedef TYPELIST_3(
SInt<589793238>,SInt<141592653>,SInt<3>) NL21;
73 typedef SDecimal<
SBigInt<true,NL21,DefaultDecimalBase>,2,DefaultDecimalBase> TPi2Dec;
75 typedef TYPELIST_4(SInt<462643383>,SInt<589793238>,SInt<141592653>,SInt<3>) NL31;
76 typedef SDecimal<
SBigInt<true,NL31,DefaultDecimalBase>,3,DefaultDecimalBase> TPi3Dec;
81 template<class Rational, int_t NDigits, base_t DecBase=DefaultDecimalBase>
82 struct RationalToDecimal;
84 template<class Numer, class Denom, int_t NDigits, base_t DecBase>
85 struct RationalToDecimal<SRational<Numer,Denom>,NDigits,DecBase> {
86 typedef typename MF::IPowBig<SInt<DecBase>,NDigits>::Result D;
87 typedef typename Mult<Numer,D>::Result NewNumer;
88 typedef typename Div<NewNumer,Denom>::DivResult AllDecimals;
89 typedef SDecimal<AllDecimals,NDigits,DecBase> Result;
92 template<
bool S1,
class N1,
class Denom,
int_t NDigits, base_t Base>
93 struct RationalToDecimal<SRational<
SBigInt<S1,N1,Base>,Denom>,NDigits,Base> {
94 typedef typename Loki::TL::ShiftRight<N1,NDigits,SInt<0> >::Result NList;
97 typedef typename Div<NewNumer,Denom>::DivResult AllDecimals;
98 typedef SDecimal<AllDecimals,NDigits,Base> Result;
101 template<
int_t N,
int_t NDigits, base_t DecBase>
102 struct RationalToDecimal<
SInt<N>,NDigits,DecBase> {
103 typedef SDecimal<SInt<N>,0,DecBase> Result;
106 template<
class BI,
int_t ND, base_t Base,
int_t NDigits, base_t DecBase>
107 struct RationalToDecimal<SDecimal<BI,ND,Base>,NDigits,DecBase> {
108 typedef BI AllDecimals;
109 typedef SDecimal<BI,ND,Base> Result;
114 template<
class N,
class D,
int Accuracy, base_t Base>
115 struct Reduce<SRational<N,D>,Accuracy,Base> {
116 typedef typename MF::IPowBig<SInt<Base>,Accuracy>::Result Denom;
117 typedef typename RationalToDecimal<SRational<N,D>,Accuracy,Base>::AllDecimals Decimals;
118 typedef typename Simplify<SRational<Decimals,Denom> >::Result Result;
131 template<
class X,
class FuncStep,
132 template<
class,
class>
class Accum,
137 typedef FuncSeries<X,FuncStep,Accum,Accuracy,Count-1> NextIter;
138 typedef typename FuncStep::template Value<Count-1,X,
typename NextIter::ResultAux,Accuracy> FStep;
139 typedef typename FStep::Result Step;
140 typedef typename FStep::ResultAux ResultAux;
142 typedef typename Accum<Step,typename NextIter::Result>::Result Result;
145 template<
class X,
class FuncStep,
146 template<
class,
class>
class Accum,
148 struct FuncSeries<X,FuncStep,Accum,Accuracy,1>
150 typedef typename FuncStep::template Value<0,X,Loki::NullType,Accuracy> FStep;
151 typedef typename FStep::Result Result;
152 typedef typename FStep::ResultAux ResultAux;
155 template<
class X,
class FuncStep,
156 template<
class,
class>
class Accum,
158 struct FuncSeries<X,FuncStep,Accum,Accuracy,0> {};
162 template<
class X,
class FuncStep,
163 template<
class,
class>
class Accum,
165 class Value,
class Dec1,
class Dec2,
class Aux,
167 class FuncAccuracyLoop;
169 template<
class X,
class FuncStep,
170 template<
class,
class>
class Accum,
171 int Accuracy,
int I,
class Value,
class Dec1,
class Dec2,
class Aux>
172 struct FuncAccuracyLoop<X,FuncStep,Accum,Accuracy,I,Value,Dec1,Dec2,Aux,true>
174 typedef Dec2 NextDecimal;
175 typedef Value Result;
178 template<
class X,
class FuncStep,
179 template<
class,
class>
class Accum,
180 int Accuracy,
int I,
class Value,
class Dec1,
class Dec2,
class Aux>
181 struct FuncAccuracyLoop<X,FuncStep,Accum,Accuracy,I,Value,Dec1,Dec2,Aux,false>
183 typedef typename FuncStep::template Value<I,X,Aux,Accuracy> FStep;
184 typedef typename FStep::Result NextStep;
185 typedef typename FStep::ResultAux NextAux;
186 typedef typename Accum<Value,NextStep>::Result NextValue;
188 typedef typename RationalToDecimal<NextValue,Accuracy,DefaultDecimalBase>::AllDecimals NextDecimal;
189 typedef typename FuncAccuracyLoop<X,FuncStep,Accum,Accuracy,I+1,NextValue,Dec2,NextDecimal,NextAux>::Result Result;
194 template<
class X,
class FuncStep,
195 template<
class,
class>
class Accum,
196 int Len,
int I,
class Value1,
class Value2,
class Aux,
197 bool C = (NL::Length<typename Value2::Numer>::value > Len
198 || NL::Length<typename Value2::Denom>::value > Len)>
199 class FuncLengthLoop;
201 template<
class X,
class FuncStep,
202 template<
class,
class>
class Accum,
203 int Len,
int I,
class Value1,
class Value2,
class Aux>
204 struct FuncLengthLoop<X,FuncStep,Accum,Len,I,Value1,Value2,Aux,true>
206 typedef Value1 Result;
209 template<
class X,
class FuncStep,
210 template<
class,
class>
class Accum,
211 int Len,
int I,
class Value1,
class Value2,
class Aux>
212 struct FuncLengthLoop<X,FuncStep,Accum,Len,I,Value1,Value2,Aux,false>
214 typedef typename FuncStep::template Value<I,X,Aux> FStep;
215 typedef typename FStep::Result NextStep;
216 typedef typename FStep::ResultAux NextAux;
217 typedef typename Accum<Value2,NextStep>::Result NextValue;
218 typedef typename FuncLengthLoop<X,FuncStep,Accum,Len,I+1,Value2,NextValue,NextAux>::Result Result;
222 template<
class Value,
int Accuracy, base_t Base>
223 struct GenericAccuracyBasedFuncAdapter;
225 template<
class N,
class D,
int Accuracy, base_t Base>
226 struct GenericAccuracyBasedFuncAdapter<SRational<N,D>,Accuracy,Base>
228 typedef typename RationalToDecimal<SRational<N,D>,Accuracy,DefaultBase>::AllDecimals Result;
231 template<
class BI,
int_t ND,
int Accuracy, base_t Base>
232 struct GenericAccuracyBasedFuncAdapter<SDecimal<BI,ND,Base>,Accuracy,Base>
234 typedef typename SDecimal<BI,ND,Base>::Num Result;
238 template<
class X,
class FuncStep,
239 template<
class,
class>
class Accumulator,
242 struct GenericAccuracyBasedFunc
244 typedef FuncSeries<X,FuncStep,Accumulator,Accuracy,NStartingSteps> Sum;
245 typedef typename Sum::Result StartValue;
246 typedef typename Sum::ResultAux Aux;
247 typedef typename GenericAccuracyBasedFuncAdapter<StartValue,Accuracy,DefaultBase>::Result StartDecimal;
249 typedef typename FuncStep::template Value<NStartingSteps,X,Aux,Accuracy> FStep;
250 typedef typename FStep::Result NextStep;
251 typedef typename FStep::ResultAux NextAux;
252 typedef typename Accumulator<NextStep,StartValue>::Result NextValue;
253 typedef typename GenericAccuracyBasedFuncAdapter<NextValue,Accuracy,DefaultBase>::Result NextDecimal;
255 typedef FuncAccuracyLoop<X,FuncStep,Accumulator,Accuracy,NStartingSteps+1,
256 NextValue,StartDecimal,NextDecimal,NextAux> Loop;
258 typedef typename Loop::Result Result;
263 template<
class X,
class FuncStep,
264 template<
class,
class>
class Accumulator,
267 struct GenericLengthBasedFunc
269 typedef FuncSeries<X,FuncStep,Accumulator,Length,NStartingSteps> Sum;
270 typedef typename Sum::Result StartValue;
271 typedef typename Sum::ResultAux Aux;
273 typedef typename FuncStep::template Value<NStartingSteps,X,Aux,Length> FStep;
274 typedef typename FStep::Result NextStep;
275 typedef typename FStep::ResultAux NextAux;
276 typedef typename Accumulator<NextStep,StartValue>::Result NextValue;
278 typedef typename FuncLengthLoop<X,FuncStep,Accumulator,Length,NStartingSteps+1,StartValue,NextValue,NextAux>::Result Result;
284 template<
class SFrac,
int Accuracy,
class RetType =
long double>
287 template<
class Numer,
class Denom,
int Accuracy,
class RetType>
288 struct Compute<SRational<Numer,Denom>,Accuracy,RetType> {
289 typedef SRational<Numer,Denom> Value;
290 typedef typename RationalToDecimal<Value,Accuracy,DefaultDecimalBase>::Result TDec;
291 typedef typename DoubleBase<typename TDec::Num>::Result BigInt;
293 static RetType value() {
294 return EvaluateToFloat<BigInt,RetType>::value()
295 / DPow<DefaultDecimalBase,Accuracy,RetType>::value();
299 template<
int_t N,
int Accuracy,
class RetType>
300 struct Compute<SInt<N>,Accuracy,RetType> {
302 static RetType value() {
return static_cast<RetType
>(N); }
305 template<
class BI,
int_t ND, base_t Base,
int Accuracy,
class RetType>
306 struct Compute<SDecimal<BI,ND,Base>,Accuracy,RetType> {
307 typedef SDecimal<BI,ND,Base> Value;
308 typedef typename Reduce<Value,Accuracy,Base>::Result TDec;
309 typedef typename DoubleBase<typename TDec::Num>::Result BigInt;
311 static RetType value() {
312 return EvaluateToFloat<BigInt,RetType>::value()
313 / DPow<Base,Accuracy,RetType>::value();
317 template<
int_t N,
int_t ND, base_t Base,
int Accuracy,
class RetType>
318 struct Compute<SDecimal<SInt<N>,ND,Base>,Accuracy,RetType> {
319 typedef SDecimal<SInt<N>,ND,Base> Value;
321 static RetType value() {
322 return static_cast<RetType
>(N)
323 / DPow<Base,ND,RetType>::value();
329 template<
int K,
class C,
class Aux>
332 typedef typename IPowBig<SInt<16>,K>::Result PBig;
337 typedef typename Mult<typename Mult<typename Mult<TK1,TK2>::Result,
338 typename Mult<TK3,TK4>::Result>::Result,PBig>::Result Denom;
339 typedef typename Add<SInt<188>,
typename Mult<SInt<4*K>,
SInt<120*K+151> >::Result>::Result Numer;
341 typedef typename Simplify<SRational<Numer,Denom> >::Result Result;
343 typedef Loki::NullType ResultAux;
346 template<
class C,
class Aux>
347 struct PiRational<0,C,Aux>
349 typedef SRational<SInt<47>,
SInt<15> > Result;
350 typedef Loki::NullType ResultAux;
353 struct PiRationalFunc
355 template<
int K,
class C,
class Aux,
int Accuracy>
356 struct Value :
public PiRational<K,C,Aux> {};
360 template<
int K,
class C,
class Aux,
int Accuracy>
363 typedef typename IPowBig<SInt<16>,K>::Result PBig;
368 typedef typename Mult<typename Mult<typename Mult<TK1,TK2>::Result,
369 typename Mult<TK3,TK4>::Result>::Result,PBig>::Result Denom;
370 typedef typename Add<SInt<188>,
typename Mult<SInt<4*K>,
SInt<120*K+151> >::Result>::Result Numer;
372 typedef SRational<Numer,Denom> F;
373 typedef typename RationalToDecimal<F,Accuracy,DefaultDecimalBase>::Result Result;
374 typedef Loki::NullType ResultAux;
377 template<
class C,
class Aux,
int Accuracy>
378 struct PiDecimal<0,C,Aux,Accuracy>
380 typedef SRational<SInt<47>,
SInt<15> > F;
381 typedef typename RationalToDecimal<F,Accuracy,DefaultDecimalBase>::Result Result;
382 typedef Loki::NullType ResultAux;
387 template<
int K,
class C,
class Aux,
int Accuracy>
388 struct Value :
public PiDecimal<K,C,Aux,Accuracy> {};
392 template<
int Accuracy = 2,
393 int NStartingSteps = 5>
394 struct PiAcc :
public GenericAccuracyBasedFunc<Loki::NullType,PiRationalFunc,Add,Accuracy,NStartingSteps>
397 template<
int NStartingSteps>
398 struct PiAcc<1,NStartingSteps> {
399 static const base_t Base = DefaultDecimalBase;
405 template<
int NStartingSteps>
406 struct PiAcc<2,NStartingSteps> {
407 static const base_t Base = DefaultDecimalBase;
414 template<
int Accuracy = 2,
415 int NStartingSteps = 5>
416 struct PiDecAcc :
public GenericAccuracyBasedFunc<Loki::NullType,PiDecimalFunc,Add,Accuracy,NStartingSteps> {};
419 template<
int Len = 2,
420 int NStartingSteps = 3>
421 struct PiLen :
public GenericLengthBasedFunc<Loki::NullType,PiRationalFunc,Add,Len,NStartingSteps>
426 template<
class X,
class Step,
class Aux = Loki::NullType>
429 typedef Pair<typename Aux::first, Step> Result;
432 template<
class X,
class Step>
433 struct SinCosAux<X,Step,Loki::NullType>
435 typedef typename Mult<X,X>::Result XX;
436 typedef Pair<XX,Step> Result;
440 template<
int K,
class X,
class Aux,
int_t D>
441 struct SinCosRational
443 static const int_t M = 2*(K-1)+D;
445 typedef typename Mult<typename Aux::first,Divider>::Result XX;
446 typedef typename Mult<XX,typename Aux::second>::Result XP;
447 typedef typename Negate<XP>::Result Result;
448 typedef typename SinCosAux<X,Result,Aux>::Result ResultAux;
452 template<
class X,
class Aux,
int_t D>
453 struct SinCosRational<0,X,Aux,D>
455 typedef typename Aux::second Result;
456 typedef typename SinCosAux<X,Result,Aux>::Result ResultAux;
460 template<
int K,
class X,
class Aux, int_t D,
464 static const int_t M = 2*(K-1)+D;
466 typedef typename RationalToDecimal<Divider,Accuracy>::Result DividerDec;
467 typedef typename Mult<typename Aux::first,DividerDec>::Result XX;
468 typedef typename Mult<XX,typename Aux::second>::Result XP;
469 typedef typename Reduce<XP,Accuracy>::Result XPR;
470 typedef typename Negate<XPR>::Result Result;
471 typedef typename SinCosAux<X,Result,Aux>::Result ResultAux;
474 template<
class X,
class Aux, int_t D,
476 struct SinCosDecimal<0,X,Aux,D,Accuracy>
478 typedef typename Aux::second Result;
479 typedef typename SinCosAux<X,Result,Aux>::Result ResultAux;
483 template<
int K,
class X,
class Aux>
484 struct CosRational :
public SinCosRational<K,X,Aux,1> {};
486 template<
int K,
class X>
487 struct CosRational<K,X,Loki::NullType>
488 :
public SinCosRational<K,X,typename SinCosAux<X,SInt<1> >::Result,1> {};
490 struct CosRationalFunc {
491 template<
int K,
class X,
class Aux,
int Accuracy>
492 struct Value :
public CosRational<K,X,Aux> {};
496 template<
int K,
class X,
class Aux,
int Accuracy>
497 struct CosDecimal :
public SinCosDecimal<K,X,Aux,1,Accuracy> {};
499 template<
int K,
class X,
int Accuracy>
500 struct CosDecimal<K,X,Loki::NullType,Accuracy>
501 :
public SinCosDecimal<K,X,typename SinCosAux<X,SInt<1> >::Result,1,Accuracy> {};
503 struct CosDecimalFunc {
504 template<
int K,
class X,
class Aux,
int Accuracy>
505 struct Value :
public CosDecimal<K,X,Aux,Accuracy> {};
509 template<
int K,
class X,
class Aux>
510 struct SinRational :
public SinCosRational<K,X,Aux,2> {};
512 template<
int K,
class X>
513 struct SinRational<K,X,Loki::NullType>
514 :
public SinCosRational<K,X,typename SinCosAux<X,X>::Result,2> {};
516 struct SinRationalFunc {
517 template<
int K,
class X,
class Aux,
int Accuracy>
518 struct Value :
public SinRational<K,X,Aux> {};
522 template<
int K,
class X,
class Aux,
int Accuracy>
523 struct SinDecimal :
public SinCosDecimal<K,X,Aux,2,Accuracy> {};
525 template<
int K,
class X,
int Accuracy>
526 struct SinDecimal<K,X,Loki::NullType,Accuracy>
527 :
public SinCosDecimal<K,X,typename SinCosAux<X,X>::Result,2,Accuracy> {};
530 struct SinDecimalFunc {
531 template<
int K,
class X,
class Aux,
int Accuracy>
532 struct Value :
public SinDecimal<K,X,Aux,Accuracy> {};
538 int NStartingSteps = 5>
539 struct CosAcc :
public GenericAccuracyBasedFunc<X,CosRationalFunc,Add,Accuracy,NStartingSteps> {};
543 int NStartingSteps = 5>
544 struct CosDecAcc :
public GenericAccuracyBasedFunc<X,CosDecimalFunc,Add,Accuracy,NStartingSteps> {};
548 int NStartingSteps = 3>
549 struct CosLen :
public GenericLengthBasedFunc<X,CosRationalFunc,Add,Len,NStartingSteps> {};
554 int NStartingSteps = 5>
555 struct SinAcc :
public GenericAccuracyBasedFunc<X,SinRationalFunc,Add,Accuracy,NStartingSteps> {};
559 int NStartingSteps = 5>
560 struct SinDecAcc :
public GenericAccuracyBasedFunc<X,SinDecimalFunc,Add,Accuracy,NStartingSteps> {};
564 int NStartingSteps = 3>
565 struct SinLen :
public GenericLengthBasedFunc<X,SinRationalFunc,Add,Len,NStartingSteps> {};
568 template<int_t A, int_t B,
570 int NStartingSteps = 5>
572 typedef SRational<SInt<A>,
SInt<B> > F;
573 typedef typename PiAcc<Accuracy,NStartingSteps>::Result TPi;
574 typedef typename Mult<TPi,F>::Result X;
575 typedef typename SinAcc<X,Accuracy>::Result Result;
579 int Accuracy,
int NStartingSteps>
580 struct __SinPiFrac<A,1,Accuracy,NStartingSteps> {
585 int Accuracy,
int NStartingSteps>
586 struct __SinPiFrac<A,2,Accuracy,NStartingSteps> {
587 typedef typename Loki::Select<(A%4 == 1),SInt<1>,SInt<-1> >::Result Result;
590 template<
int_t A,
int NStartingSteps>
591 struct __SinPiFrac<A,3,2,NStartingSteps> {
592 static const int_t R = A%6;
597 typedef SRational<Numer,Denom> Result;
600 template<
int_t A,
int NStartingSteps>
601 struct __SinPiFrac<A,4,2,NStartingSteps> {
602 static const int_t R = A%8;
607 typedef SRational<Numer,Denom> Result;
611 int Accuracy,
int NStartingSteps>
612 struct __SinPiFrac<A,6,Accuracy,NStartingSteps> {
613 static const int_t R = A%12;
614 typedef SRational<SInt<1>,
SInt<2> > V1;
615 typedef SRational<SInt<-1>,
SInt<2> > V2;
616 typedef typename Loki::Select<(R==1 || R==5),V1,V2>::Result Result;
619 template<int_t A, int_t B,
621 int NStartingSteps = 5>
623 typedef SRational<SInt<A>,
SInt<B> > F;
624 typedef typename Simplify<F>::Result SF;
625 typedef typename __SinPiFrac<SF::Numer::value,SF::Denom::value,Accuracy,NStartingSteps>::Result Result;
629 template<int_t A, int_t B,
631 int NStartingSteps = 5>
632 struct __SinPiDecimal {
633 typedef SRational<SInt<A>,
SInt<B> > F;
634 typedef typename RationalToDecimal<F,Accuracy>::Result FDec;
635 typedef typename PiDecAcc<Accuracy,NStartingSteps>::Result TPi;
639 typedef typename Mult<TPi,FDec>::Result X;
640 typedef typename Reduce<X,Accuracy>::Result XR;
641 typedef typename SinDecAcc<XR,Accuracy>::Result Result;
645 int Accuracy,
int NStartingSteps>
646 struct __SinPiDecimal<A,1,Accuracy,NStartingSteps> {
651 int Accuracy,
int NStartingSteps>
652 struct __SinPiDecimal<A,2,Accuracy,NStartingSteps> {
653 typedef typename Loki::Select<(A%4 == 1),SInt<1>,SInt<-1> >::Result Result;
656 template<
int_t A,
int NStartingSteps>
657 struct __SinPiDecimal<A,3,2,NStartingSteps> {
658 static const int_t R = A%6;
661 typedef SDecimal<Numer,2,DefaultDecimalBase> Result;
664 template<
int_t A,
int NStartingSteps>
665 struct __SinPiDecimal<A,4,2,NStartingSteps> {
666 static const int_t R = A%8;
669 typedef SDecimal<Numer,2,DefaultDecimalBase> Result;
673 int Accuracy,
int NStartingSteps>
674 struct __SinPiDecimal<A,6,Accuracy,NStartingSteps> {
675 static const int_t R = A%12;
676 typedef Loki::Typelist<SInt<500000000>,Loki::NullType> NList1;
677 typedef typename Loki::TL::ShiftRight<NList1,Accuracy-1,
SInt<0> >::Result NList;
679 typedef SDecimal<Numer,2,DefaultDecimalBase> Result;
682 template<int_t A, int_t B,
684 int NStartingSteps = 5>
685 struct SinPiDecimal {
686 typedef SRational<SInt<A>,
SInt<B> > F;
687 typedef typename Simplify<F>::Result SF;
688 typedef typename __SinPiDecimal<SF::Numer::value,SF::Denom::value,Accuracy,NStartingSteps>::Result Result;
693 template<int_t A, int_t B,
695 int NStartingSteps = 5>
697 typedef SRational<SInt<A>,
SInt<B> > F;
698 typedef typename PiAcc<Accuracy,NStartingSteps>::Result TPi;
699 typedef typename Mult<TPi,F>::Result X;
700 typedef typename CosAcc<X,Accuracy>::Result Result;
704 int Accuracy,
int NStartingSteps>
705 struct __CosPiFrac<A,1,Accuracy,NStartingSteps> {
706 typedef typename Loki::Select<(A%2==0),SInt<1>,SInt<-1> >::Result Result;
710 int Accuracy,
int NStartingSteps>
711 struct __CosPiFrac<A,2,Accuracy,NStartingSteps> {
715 template<int_t A, int_t B,
717 int NStartingSteps = 5>
719 typedef SRational<SInt<A>,
SInt<B> > F;
720 typedef typename Simplify<F>::Result SF;
721 typedef typename __CosPiFrac<SF::Numer::value,SF::Denom::value,Accuracy,NStartingSteps>::Result Result;
726 template<int_t A, int_t B,
728 int NStartingSteps = 5>
729 struct __CosPiDecimal {
730 typedef SRational<SInt<A>,
SInt<B> > F;
731 typedef typename RationalToDecimal<F,Accuracy>::Result FDec;
732 typedef typename PiDecAcc<Accuracy,NStartingSteps>::Result TPi;
735 typedef typename Mult<TPi,FDec>::Result X;
736 typedef typename Reduce<X,Accuracy>::Result XR;
737 typedef typename CosDecAcc<XR,Accuracy>::Result Result;
741 int Accuracy,
int NStartingSteps>
742 struct __CosPiDecimal<A,1,Accuracy,NStartingSteps> {
743 typedef typename Loki::Select<(A%2==0),SInt<1>,SInt<-1> >::Result Result;
747 int Accuracy,
int NStartingSteps>
748 struct __CosPiDecimal<A,2,Accuracy,NStartingSteps> {
752 template<int_t A, int_t B,
754 int NStartingSteps = 5>
755 struct CosPiDecimal {
756 typedef SRational<SInt<A>,
SInt<B> > F;
757 typedef typename Simplify<F>::Result SF;
758 typedef typename __CosPiDecimal<SF::Numer::value,SF::Denom::value,Accuracy,NStartingSteps>::Result Result;
768 struct Cout<SInt<N> >
770 static void apply(std::ostream& os) {
775 template<
bool S,
class H,
class T, base_t Base>
776 struct Cout<SBigInt<S,Loki::Typelist<H,T>,Base> >
778 static const int_t W = NDigits<Base-1,10>::value;
779 typedef Cout<SBigInt<S,T,Base> > Next;
781 static void apply(std::ostream& os) {
785 os << std::right << H::value <<
" ";
789 template<
bool S,
class H, base_t Base>
790 struct Cout<SBigInt<S,Loki::Typelist<H,Loki::NullType>,Base> > {
791 static void apply(std::ostream& os) {
795 os << H::value <<
" ";
799 template<
class N,
class D>
800 struct Cout<SRational<N,D> >
805 static void apply(std::ostream& os) {
814 template<
bool S,
class H,
class T, base_t Base,
int_t NDecPlaces, base_t DecBase>
815 struct Cout<SDecimal<SBigInt<S,Loki::Typelist<H,T>,Base>,NDecPlaces,DecBase> >
817 static const int_t W = NDigits<Base-1,10>::value;
818 static const int_t DW = NDigits<DecBase-1,10>::value;
819 static const int_t Len = NL::Length<SBigInt<S,Loki::Typelist<H,T>,Base> >::value;
820 static const int_t DP = DW * NDecPlaces;
821 typedef Cout<SDecimal<SBigInt<S,T,Base>,NDecPlaces,DecBase> > Next;
823 static void apply(std::ostream& os,
const int_t len = 0) {
824 Next::apply(os,len+W);
826 if (DP < len+W && DP > len) {
828 for (
int i = 0; i < DP-len; ++i) d *= 10;
830 os << std::right << H::value/d <<
"." << H::value%d;
834 os << std::right << H::value;
841 template<
bool S,
class H, base_t Base,
int_t NDecPlaces, base_t DecBase>
842 struct Cout<SDecimal<SBigInt<S,Loki::Typelist<H,Loki::NullType>,Base>,NDecPlaces,DecBase> >
844 static const int_t HW = NDigits<H::value,10>::value;
845 static const int_t DP = NDigits<DecBase-1,10>::value * NDecPlaces;
847 static void apply(std::ostream& os,
const int_t len = 0)
855 os << std::right << H::value;
857 else if (DP < len+HW && DP > len) {
859 for (
int i = 0; i < DP-len; ++i) d *= 10;
860 os << H::value/d <<
"." << H::value%d;
869 template<
int_t N,
int_t NDecPlaces, base_t DecBase>
870 struct Cout<SDecimal<SInt<N>,NDecPlaces,DecBase> >
872 static const bool S = (N>=0);
873 static const int_t AN = S ? N : -N;
874 static const int_t HW = NDigits<AN,10>::value;
876 static void apply(std::ostream& os,
const int_t len = 0)
880 if (NDecPlaces >= len+HW) {
883 os.width(NDecPlaces-len);
884 os << std::right << AN;
886 else if (NDecPlaces < len+HW && NDecPlaces > len) {
888 for (
int i = 0; i < NDecPlaces-len; ++i) d *= 10;
889 os << AN/d <<
"." << AN%d;