- /* snum.h by K.Tsuru */
- // error ID = 1x
- /*----------------
- SNumber class
- ------------------*/
- #ifndef S_NUMBER_H
- #define S_NUMBER_H
-
- typedef NCBlock<fType> FigBlock; // type of figure[]
-
- /****************** sub-functions ************************/
- /*---------------------------------------------------------------------
- FigBlock shift to upper(n>0)/lower(n<0)
- Do not enlarge the size, i.e. cut off an oversize area.
- If abs(n) >= a.size(), "a" is initialized by zero.
- -----------------------------------------------------------------------*/
- int FigBlockShift(FigBlock& a, int n);
- /*-------------------------------------------------------------
- Convert x into a number "r" in radix "DRADIX", i.e. in a form
- x = r[0].r[1].... x DRADIX^e, r[0] = 0
- Return the exponent "e".
- --------------------------------------------------------------*/
- int doubleToArray(double x, FigBlock& r, uint *size);
- int ldoubleToArray(ldouble x, FigBlock& r, uint *size);
- /*-----------------------------------------------------------------------------
- Try to convert double "d" using long "L" into a form
- d = L * 10^e ( 0 =< |L| <= mt).
- When it is successed, return true. The operation between SDouble and double can
- be reduced into one between SDouble and short integer.
- e.g. x/1.3 ---> (x/13000)*DRADIX.
- Usually take mt = (double)d.SlOpMaxValue() (= ULONG_MAX/radix);
- -------------------------------------------------------------------------------*/
- bool doubleTolongExp(double d, long* L, int* e, double mt);
- /******************** end of sub-functions ***********************/
-
- /********************
- The list of classes
- D(DRADIX), B(BRADIX)
- *********************/
- // error/function ID
- class SNumber; // 1xx
- class SLong; // integer(D) 2xx
- class SDouble; // floating point number(D) 3xx
- class SInteger; // integer(B) 4xx
- class SDecimal; // fixed point number(B) 5xx
- class StringToNumber; // string -->integer/real 6xx
- class SFraction; // Fraction(D) 7xx
- class SRational; // Rational(B) 8xx
- class SComplex; // complex(D) 9xxx ver.2.18
- class SLComplex; // complex(D) 9xxx ver.2.182
- // template class 1xxx
- struct Ldiv_t; // structure 27x
-
- /****************
- for preprocessor ver 2.18
- ****************/
- #define SNUMBER 1
- #define SLONG 2
- #define SDOUBLE 3
- #define SDEC_INT 4
- #define SDECIMAL 5
- #define SFRACTION 7
- #define SRATIONAL 8
-
-
- /***********************************************
- Provides common parts of multi-precision number
- ************************************************/
- class SNumber : public SNManager{
- // Do not use default constructor and operator=().
- SNumber(); // has no body.
- SNumber& operator=(const SNumber& sn); // has no body.
- // table of cutDown (ENABLE | DISABLE), the size can be reduced or not
- static bool cutDownTable[DETECT_IR];
- uchar pushCD; // change cutDownTable[] 1 / 0
- signed char sign; // sign(-1,0,1 or UNDECIDED)
- NumberType type; // type of multi-precision number
- public:
- /*****************************************************************
- Definition of values of "pushCD" for changing the value of "cutDown".
- POP : back to previous value
- PUSH bit is on for an object which change the value of "cutDown".
- defined enum { DISABLE = 0, ENABLE = 1}; in "snconst.h"
- ******************************************************************/
- enum { POP = 2, PUSH = 4 };
- /**********************************************
- Definition of values for sign
- UNDECIDED : figure[] is not initialized or empty
- ***********************************************/
- enum { MINUS = -1, ZERO = 0, PLUS = 1, UNDECIDED = 0x10 };
- protected :
- //position of non-zero figures
- //figure[]:f[0]...f[aTail]...f[aHead]...f[size-1]
- //value : 0 ...0(t>0) ... (h>0)0 ... 0
- uint aHead;
- uint aTail;
-
- FigBlock figure; // variable size array
- void SetType(NumberType tp){ type = tp; }
- /***************************************************************
- allocates memory of figure[]
- Do not free memory. Use "SizeZero()" for free memory.
- Decides the sign by initialization. sign
- copy=0:all elements are initialized by zero ZERO
- copy>0:copy lower elements,upper initialized by zero ----
- copy<0:allocate memory only,do not initialize UNDECIDED
- return figure.size()
- Even if sz > minArraySize, "CutDown(DISABLE)" is not done.
- ****************************************************************/
- uint valloc(uint sz, int copy); // 101
- /*************************************************************************
- Allocate memory to be possible to substitute the element figure[index].
- The extended part is initialized by zero. For integer type(SLong/SInteger)
- if index>=maxmum size, the program terminate by overflow error.
- return success(1)/not(0)
- ****************************************************************/
- bool Reserve(uint index){
- if(index < figure.size()) return 1;
- valloc(index+1, 1); // do not use "figure.reserve()"
- return (index < figure.size()) ? 1 : 0;
- }
- /******************************************************
- Get the position of figure[aTail] and figure[aHead].
- Give id the position ID number of function for debug.
- ******************************************************/
- void CheckArray(int id); // 102
- /****************************************************
- Shift figure[] to upper(shift>0) or lower(shift<0).
- Move figure[i] to figure[i+shift].
- The values of "aHead" and "aTail" should be decided.
- return the sign or zero if out of range by moving.
- ******************************************************/
- int ShiftArray(int shift); // 103
- /*******************************************************************
- copy function
- cs=COPY:set "figure = a.figure" for copy constructor.
- cs=SUBS:change the size of figure[] for the operator=()
- ********************************************************************/
- enum { COPY = 1, SUBS = 2 };
- void CopyValue(const SNumber& a, int cs); // 104
- /*****************************************************
- normalization function
- Do not call "CheckArray()".
- The values of "aHead" and "aTail" should be decided.
- Used in "operator()++", etc.
- ******************************************************/
- void Normalize(); // 105
- /****************************************************************************
- CutDown
- Reduce the size of figure[] by cutting upper/lower zeros for integer/real number.
- When the size of result after the operation is less than half of previous one,
- call this function.
- CutDown(DISABLE) take into a mode in which the size of figure[] is not reducible.
- Mainly used in the fixed-point mode in "SDouble" function.
- See also the function "BdFact(n)".
- ****************************************************************************/
- void CutDown(uchar cd); // cd = DISABLE , ENABLE or POP. 106
- void DoCutDown(); // reudce size if ENABLE 107
- uchar PushedCD() const { return pushCD; }
- void PushCD(uchar cd) { pushCD = cd; }
- // check sign
- void SignCheck(int id) const{
- // Trying to take out the sign of uninitialized value causes error.
- if(sign == UNDECIDED) SetError(UNDEC_VALUE, NULL, id);
- #ifndef NDEBUG
- else assert( (sign == ZERO) || (sign == PLUS) || (sign == MINUS) );
- #endif
- }
- void SetSign(double s){
- if(s == 0.0) sign = ZERO;
- else sign = s > 0 ? PLUS : MINUS;
- }
- public:
- // type & DETECT_IR = DEC_INT or REAL.
- bool CutDown() const {
- return (type == BIN_DEC) ? DISABLE : cutDownTable[type & DETECT_IR];
- }
- /****************************************
- Set a number by use of FigBlock and sign.
- Do not call Normalize().
- *****************************************/
- void SetFigBlock(const FigBlock& a, int sgn); // 108
- // version in which call Normalize()
- void SetFigBlockNorm(const FigBlock& a, int sgn){
- SetFigBlock(a, sgn); Normalize();
- }
- /*****************************************************
- Set all elements of figure[] and sign zero.
- If cutDown==ENABLE, reduce the size to minArraySize.
- *****************************************************/
- virtual void SetZero(); // 109
- uint Head() const { return aHead; }
- uint Tail() const { return aTail; }
- /*************************************************************
- Read elements of figure[] via pointer. Faster than operator().
- [usage] const fType* mv = m.ReadFigures();
- .... mv[] can be used as right hand side/not lhs......
- reference:"Effective C++"
- *************************************************************/
- const fType* ReadFigures() const {
- SNumber* const temp = (SNumber* const)this;
- return temp->figure.Elements();
- }
- /************************************************************
- Read figure[] by FigBlock&.
- Provides a method which change a part of figure[] and make a
- new object by use of SetFigBlock().
- [usage]const FigBlock& mv = m.FigBlock(); // take out figure[]
- FigBlock a(mv); // copy
- ...change the a.figure[]....
- x.SetFigBlock(...);
- ************************************************************/
- const FigBlock& ReadFigBlock() const {
- SNumber* const temp = (SNumber* const)this;
- return temp->figure;
- }
- // free memory
- void SizeZero(){
- sign = UNDECIDED; aHead = aTail = 0; figure.size(0, 0);
- }
- /************************************************************************
- Provides a user's method to allocate memory of figure[].
- The meaning of "copy" is same as that of "valloc()".
- If size > minArraySize, "CutDown(DISABLE)" is called.
- *************************************************************************/
- void FigureAlloc(uint size, int copy = 0); // 110
- void FigureClear(uint from, uint to); // 111 ver. 2.17
- //Make the size to be smaller if possible in "DISABLE" mode.
- void ReduceSize(){
- if( 2u*(aHead+1u) <= figure.size() ){
- CutDown(ENABLE); valloc( aHead+1u, 1); CutDown(POP);
- }
- }
- /*************
- various sizes
- **************/
- uint MaxSize() const { return SNMaxSize(type); }
- virtual uint MinSize() const { return minArraySize; }
- uint FigureSize() const { return figure.size(); }
- virtual uint Size() const { // override in SDouble class
- return min(figure.size(), SNMaxSize(type));
- }
- //Standard multiplication in BRADIX is faster twice that in DRADIX.
- uint FFTMinSize() const{
- return (type & BIN_RDX) ? 2u*MFFTMinSize() : MFFTMinSize();
- }
- uint FFTMinSize(NumberType tp) const{
- return (tp & BIN_RDX) ? 2u*MFFTMinSize() : MFFTMinSize();
- }
- // radix
- fType Radix() const { return (type & BIN_RDX) ? BRADIX : DRADIX; }
- ulong RadixSq() const { return (type & BIN_RDX) ? BRADIX_SQ : DRADIX_SQ; }
- //Maximum value of small number in operation (SXXX)/(ulong).
- ulong SlOpMaxValue() const {
- return (type & BIN_RDX) ? ULONG_MAX/(ulong)BRADIX : ULONG_MAX/(ulong)DRADIX;
- }
- // constructor
- // do not allocate memory of figure[]
- SNumber(NumberType tp):pushCD(0), sign(UNDECIDED), type(tp),
- aHead(0), aTail(0), figure(){ objectCounter++; }
- /**************************************************
- fsz:size of figure[]
- sign = UNDECIDED;
- If fsz == 0, do not allocate memory of figure[].
- ***************************************************/
- SNumber( NumberType tp, uint fsz); // 111
- //copy constructor
- //Do not copy "pushCD",i.e. not initialize as pushCD(a.pushCD).
- SNumber(const SNumber& a) : pushCD(0), sign(a.sign), type(a.type),
- aHead(a.aHead), aTail(a.aTail) , figure(a.figure){ objectCounter++; }
- // destructor
- virtual ~SNumber(); // Restore cutDownTable[] value. 112
- /*************************************************************
- For read(lhs) only
- Do not use in large for() roop, because of much overhead time.
- **************************************************************/
- fType operator()(int n) const { // "SNumber::" is deleteted since version 2.912.
- if(n < 0) return 0;
- return ( (uint)n < figure.size() ) ? figure(n) : 0;
- }
- /*******************************************************************
- return raw value without cheking the range of n and size of figure[].
- Use when you are sure that n < figure.size().
- Can not used as lhs. For writting use SetFigBlock(const FigBlock& a,int sgn);
- *********************************************************************/
- fType operator[](uint n) const { return figure(n); }
- NumberType Type() const { return type; }
- virtual int Sign(int id = 11) const { SignCheck(id); return (int)sign; }
- int RawSign() const { return (int)sign; }
- /**************************************************************
- Faster than the statement "r = -r;" which call copying routine.
- ***************************************************************/
- void ChangeSign(int id = 13){ SignCheck(id); sign = -sign; }
- };
- inline fType Radix(SNumber::NumberType tp) {
- return (tp & SNManager::BIN_RDX) ? BRADIX : DRADIX;
- }
- #endif // S_NUMBER_H
snum.h : last modifiled at 2017/08/20 11:53:05(13,013 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).