Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. #ifndef __anumber_h__
  3. #define __anumber_h__
  4.  
  5. #include "grower.h"
  6. #include "yacasbase.h"
  7. #include "lispassert.h"
  8. #include "lispstring.h"
  9.  
  10.  
  11.  
  12. /* Quantities derived from the platform-dependent types for doing
  13.  * arithmetic.
  14.  */
  15.  
  16. #define WordBits  (8*sizeof(PlatWord))
  17. #define WordBase  (((PlatDoubleWord)1)<<WordBits)
  18. #define WordMask  (WordBase-1)
  19.  
  20. // The default is 8, but it is suspected mose numbers will be short integers that fit into
  21. // one or two words. For these numbers memory allocation will be a lot more friendly.
  22. class ANumberOps : public ArrOpsPOD<PlatWord>
  23. {
  24. public:
  25.   ANumberOps() {}
  26.   inline int granularity() const { return 2; }
  27. };
  28.  
  29.  
  30. /* Class ANumber represents an arbitrary precision number. it is
  31.  * basically an array of PlatWord objects, with the first element
  32.  * being the least significant. iExp <= 0 for integers.
  33.  */
  34. class ANumber : public CArrayGrower<PlatWord,ANumberOps>
  35. {
  36. public:
  37.   typedef CArrayGrower<PlatWord,ANumberOps> ASuper;
  38. public:
  39.     ANumber(const LispChar * aString,LispInt aPrecision,LispInt aBase=10);
  40.     ANumber(LispInt aPrecision);
  41.     ANumber(PlatWord *aArray, LispInt aSize, LispInt aPrecision);
  42.     //TODO the properties of this object are set in the member initialization list, but then immediately overwritten by the CopyFrom. We can make this slightly cleaner by only initializing once.
  43.     inline ANumber(ANumber& aOther) : ASuper(),iExp(0),iNegative(LispFalse),iPrecision(0),iTensExp(0)
  44.     {
  45.       CopyFrom(aOther);
  46.     }
  47.     ~ANumber();
  48.     void CopyFrom(const ANumber& aOther);
  49.     LispBoolean ExactlyEqual(const ANumber& aOther);
  50.     void SetTo(const LispChar * aString,LispInt aBase=10);
  51.     inline void SetPrecision(LispInt aPrecision) {iPrecision = aPrecision;}
  52.     void ChangePrecision(LispInt aPrecision);
  53.     void RoundBits(void);
  54.     void DropTrailZeroes();
  55.  
  56. public:
  57.     LispInt iExp;
  58.     LispInt iNegative;
  59.     LispInt iPrecision;
  60.     LispInt iTensExp;
  61. };
  62.  
  63. inline LispBoolean IsPositive(ANumber& a) { return !a.iNegative; }
  64. inline LispBoolean IsNegative(ANumber& a) { return a.iNegative;  }
  65. inline LispBoolean IsEven(ANumber& a) { return ((a[0]&1) == 0); }
  66. inline LispBoolean IsOdd(ANumber& a)  { return ((a[0]&1) == 1); }
  67. inline LispInt     Precision(ANumber& a) { return !a.iPrecision; }
  68.  
  69. LispBoolean BaseLessThan(ANumber& a1, ANumber& a2);
  70. void BaseDivide(ANumber& aQuotient, ANumber& aRemainder, ANumber& a1, ANumber& a2);
  71.  
  72. void IntegerDivide(ANumber& aQuotient, ANumber& aRemainder, ANumber& a1, ANumber& a2);
  73.  
  74. LispBoolean Significant(ANumber& a);
  75.  
  76. LispInt WordDigits(LispInt aPrecision, LispInt aBase);
  77.  
  78. // Operations on ANumber.
  79. void Negate(ANumber& aNumber);
  80. void  ANumberToString(LispString& aResult, ANumber& aNumber, LispInt aBase, LispBoolean aForceFloat=0);
  81. void Add(ANumber& aResult, ANumber& a1, ANumber& a2);
  82. void Subtract(ANumber& aResult, ANumber& a1, ANumber& a2);
  83. void Multiply(ANumber& aResult, ANumber& a1, ANumber& a2);
  84. void Divide(ANumber& aQuotient, ANumber& aRemainder, ANumber& a1, ANumber& a2);
  85. LispBoolean GreaterThan(ANumber& a1, ANumber& a2);
  86. LispBoolean LessThan(ANumber& a1, ANumber& a2);
  87. void BaseShiftRight(ANumber& a, LispInt aNrBits);
  88. void BaseShiftLeft(ANumber& a, LispInt aNrBits);
  89. void BaseGcd(ANumber& aResult, ANumber& a1, ANumber& a2);
  90. void Sqrt(ANumber& aResult, ANumber& N);
  91.  
  92. void PrintNumber(char* prefix,ANumber& aNumber);
  93.  
  94. #define CORRECT_DIVISION
  95. void NormalizeFloat(ANumber& a2, LispInt digitsNeeded);
  96.  
  97.  
  98. #include "anumber.inl"
  99.  
  100.  
  101. #endif
  102.  
  103.