Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | |
2 | #define __anumber_h__ |
||
3 | |||
4 | |||
5 | #include "yacasbase.h" |
||
6 | #include "lispassert.h" |
||
7 | #include "lispstring.h" |
||
8 | |||
9 | |||
10 | |||
11 | |||
12 | * arithmetic. |
||
13 | */ |
||
14 | |||
15 | |||
16 | #define WordBase (((PlatDoubleWord)1)< |
||
17 | #define WordMask (WordBase-1) |
||
18 | |||
19 | |||
20 | // one or two words. For these numbers memory allocation will be a lot more friendly. |
||
21 | class ANumberOps : public ArrOpsPOD |
||
22 | { |
||
23 | public: |
||
24 | ANumberOps() {} |
||
25 | inline int granularity() const { return 2; } |
||
26 | }; |
||
27 | |||
28 | |||
29 | |||
30 | * basically an array of PlatWord objects, with the first element |
||
31 | * being the least significant. iExp <= 0 for integers. |
||
32 | */ |
||
33 | class ANumber : public CArrayGrower |
||
34 | { |
||
35 | public: |
||
36 | typedef CArrayGrower |
||
37 | public: |
||
38 | ANumber(const LispChar * aString,LispInt aPrecision,LispInt aBase=10); |
||
39 | ANumber(LispInt aPrecision); |
||
40 | ANumber(PlatWord *aArray, LispInt aSize, LispInt aPrecision); |
||
41 | //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. |
||
42 | inline ANumber(ANumber& aOther) : ASuper(),iExp(0),iNegative(LispFalse),iPrecision(0),iTensExp(0) |
||
43 | { |
||
44 | CopyFrom(aOther); |
||
45 | } |
||
46 | ~ANumber(); |
||
47 | void CopyFrom(const ANumber& aOther); |
||
48 | LispBoolean ExactlyEqual(const ANumber& aOther); |
||
49 | void SetTo(const LispChar * aString,LispInt aBase=10); |
||
50 | inline void SetPrecision(LispInt aPrecision) {iPrecision = aPrecision;} |
||
51 | void ChangePrecision(LispInt aPrecision); |
||
52 | void RoundBits(void); |
||
53 | void DropTrailZeroes(); |
||
54 | |||
55 | |||
56 | LispInt iExp; |
||
57 | LispInt iNegative; |
||
58 | LispInt iPrecision; |
||
59 | LispInt iTensExp; |
||
60 | }; |
||
61 | |||
62 | |||
63 | inline LispBoolean IsNegative(ANumber& a) { return a.iNegative; } |
||
64 | inline LispBoolean IsEven(ANumber& a) { return ((a[0]&1) == 0); } |
||
65 | inline LispBoolean IsOdd(ANumber& a) { return ((a[0]&1) == 1); } |
||
66 | inline LispInt Precision(ANumber& a) { return !a.iPrecision; } |
||
67 | |||
68 | |||
69 | void BaseDivide(ANumber& aQuotient, ANumber& aRemainder, ANumber& a1, ANumber& a2); |
||
70 | |||
71 | |||
72 | |||
73 | |||
74 | |||
75 | |||
76 | |||
77 | |||
78 | void Negate(ANumber& aNumber); |
||
79 | void ANumberToString(LispString& aResult, ANumber& aNumber, LispInt aBase, LispBoolean aForceFloat=0); |
||
80 | void Add(ANumber& aResult, ANumber& a1, ANumber& a2); |
||
81 | void Subtract(ANumber& aResult, ANumber& a1, ANumber& a2); |
||
82 | void Multiply(ANumber& aResult, ANumber& a1, ANumber& a2); |
||
83 | void Divide(ANumber& aQuotient, ANumber& aRemainder, ANumber& a1, ANumber& a2); |
||
84 | LispBoolean GreaterThan(ANumber& a1, ANumber& a2); |
||
85 | LispBoolean LessThan(ANumber& a1, ANumber& a2); |
||
86 | void BaseShiftRight(ANumber& a, LispInt aNrBits); |
||
87 | void BaseShiftLeft(ANumber& a, LispInt aNrBits); |
||
88 | void BaseGcd(ANumber& aResult, ANumber& a1, ANumber& a2); |
||
89 | void Sqrt(ANumber& aResult, ANumber& N); |
||
90 | |||
91 | |||
92 | |||
93 | |||
94 | void NormalizeFloat(ANumber& a2, LispInt digitsNeeded); |
||
95 | |||
96 | |||
97 | |||
98 | |||
99 | |||
100 | |||
101 | #define> |
||
102 |