- /* sdec.h by K.Tsuru */
- // file id = 50 BRADIX
- /****************************************************************************
- SDecimal class
- Provides a multi-precision fixed-point decimal fraction arithmetric in binary
- radix, mainly operations between SDecimal object and small number which is less
- than or equal ULONG_MAX/BRADIX.
- Used in BPi(), BE(), etc.
- [Notice for use]
- To obtain a fast speed
- 1.size is fixed in maximum length by constructor and can not change the size in scope
- 2.in subtraction, do not check which operand is greater than another
- 3.does not have exponent, then this class can treat a decimal less than BRADIX
- i.e. integer part has one figure : figure[0] < BRADIX
- *****************************************************************************/
- #ifndef S_DECIMAL_H
- #define S_DECIMAL_H
-
- #define AUTO_DConvDec 1 // automaticaly switch in ConvToDec()
-
- #if AUTO_DConvDec
- const uint dNconvDecMaxFig = 8000u; // DConvToDec() is faster above this figure
- // but out of memory will occure in poor memory system
- #endif
-
-
- class SDecimal : public SDouble, public virtual SCalcInfo {
- /*******************************************************************************
- Unusable functions
- Please use "XXAdd()" etc.
- If use, "not accessible" error will occure.
- SDouble only, cannot use in this class and has no body.
- To make operator+() compiling error, a member function is declared in private part
- as follows.
- SDecimal operator+(const SDecimal& m) const;
- See the reference "Effective C++".
- *********************************************************************************/
- SDecimal& operator=(const SLong& sl);
- SDecimal& operator=(const SFraction& sf);
- SDecimal operator+(const SDecimal& m) const;
- SDecimal& operator+=(const SDecimal& n);
- SDecimal operator-(const SDecimal& m) const;
- SDecimal& operator-=(const SDecimal& n);
- SDecimal operator/(const SDecimal& m) const;
- SDecimal& operator/=(const SDecimal& n);
- SDecimal operator/(double d) const;
- SDecimal& operator/=(double d);
- SDecimal& operator=(const char *s);
- SDecimal(const char *s);
- void SetRdxExp(int e);
- long DExp() const;
- long DFigures() const;
- void StdReform(int id);
- SDecimal DReciprocal(const SDecimal& x);
- void FixedPoint(int exp);
- void PointFree();
- //can not change the size
- void SizeZero();
- protected:
- void SetXDouble(double d); // Set a double, long, etc. value// 501
- public:
- // radix conversion
- SDecimal ConvToBin(const SDouble& m); // DRADIX --> BRADIX 502 changed to member since version 2.20
- // normal radix conversion
- SDouble NConvToDec() const; // BRADIX --> DRADIX 503
- /************************
- Divided radix conversion
- *************************/
- SDouble DConvToDec() const; // BRADIX --> DRADIX 504
- /************************************
- Radix conversion SDecimal to SDouble
- using "square coupling" method see below
- *************************************/
- // SDouble has also SetInt().
- void SetInt(int a){
- if(abs(a) >= Radix()) SetError(OUT_OF_RANGE, "SX SetInt", 51);
- SetZero(); figure[0] = (fType)abs(a); SetSign(a);
- }
- SDouble ConvToDec() const {
- #if AUTO_DConvDec
- if(aHead + 1 > dNconvDecMaxFig) return DConvToDec();
- #endif
- return NConvToDec();
- }
- // constructor
- SDecimal():SDouble(BIN_DEC, SNMaxSize(BIN_DEC)){}
- SDecimal(double d):SDouble(BIN_DEC, SNMaxSize(BIN_DEC)){
- SetXDouble(d);
- }
- // copy constructor
- SDecimal(const SDecimal& a):SDouble(a){}
- // SDouble copy constructor
- // Note that this is not a radix conversion function.
- // "a" must have type BIN_DEC.
- SDecimal(const SDouble& a):SDouble(a){
- if(a.Type() != BIN_DEC) SetError(RADIX_ERR, "SX(SD)", 51);
- }
- // destructor
- ~SDecimal(){}
-
- SDecimal& operator=(double d){ SetXDouble(d); return *this; }
- // for use of SDouble class's multiplication
- // "a" must have type BIN_DEC.
- SDecimal& operator=(const SDouble& a){
- if(a.Type() != BIN_DEC) SetError(RADIX_ERR, "SX = SD", 52);
- SNumber::CopyValue(a, SUBS);
- return *this;
- }
- SDecimal& operator=(const SDecimal& a){
- if(this != &a) SNumber::CopyValue(a, SUBS);
- return *this;
- }
- /******************************************************************
- sign operator
- If this operator is not provided, substitution m = -n is interpreted
- as SDouble's operator,i.e. SDecimal = SDouble, and a compiling error occure.
- ******************************************************************/
- SDecimal operator+() const { return *this; }
- SDecimal operator-() const; // 505
-
- SDecimal& BitShift(long p); // bit shift operator *(2^p). p < 0 Ok 506
- // SDecimal class object never has sign = UNDECIDED, because initialized by zero.
- // To be consistent with SDouble::Sign(int)
- int Sign(int=50) const { return RawSign(); }
- // friend functions
- // to avoid the cost of memory copy, obtain result in a argument "SDecimal& r"
- // then operator+() etc. are not provided
- friend void XXAdd(const SDecimal& m, const SDecimal& n, SDecimal& r);//r=m+n 507
- friend void XXSub(const SDecimal& m, const SDecimal& n, SDecimal& r);//r=m-n(|m|>=|n|) 508
- //operation with small s, s <= ULONG_MAX/BRADIX
- friend void XsMult(const SDecimal& m, ulong s, SDecimal& r); // r=m*s 509
- friend void XsDiv(const SDecimal& m, ulong s, SDecimal& r); // r=m/s 510
- //converting to SDouble and output to present stream
- long Put(long fig=5, long pr = 0, int perLine=0, int mode = CRLF|ROUND|INT_PUT, int delmt=' ') const;
- long Puts(long fig=5,long pr = 0, int perLine=0, int mode = CRLF|ROUND|INT_PUT, int delmt=' ') const;
- };
- // b = a*2^p
- inline void BitShift(const SDecimal&a , long p, SDecimal& b){
- b = a; b.BitShift(p);
- }
- inline long SDecimal::Puts(long fig, long pr, int perLine, int mode, int delmt) const{
- return SDecimal::Put(fig, pr, perLine, mode|END_CR, delmt);
- }
- bool DSeriesIsFast(const SDouble& x);
- // Representation for 1/DRADIX in BRADIX
- SDecimal BRecDRadix(); // 511
- void DrdxFree();
- /************************************
- Radix conversion SDecimal to SDouble
- using "square coupling" method
- not member function
- *************************************/
- SDouble SC_ConvSDecToSDbl(const SDecimal& x); // 512
- /***************
- using binary splitting template class
- ****************/
- SDouble BS_ConvSDecToSDbl(const SDecimal& x);
- #endif // S_DECIMAL_H
sdec.h : last modifiled at 2017/11/06 15:04:18(6,341 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/11/06 15:07:45 (Mon Nov 06 15:07:45 2017).