1 | |
---|
2 | |
---|
3 | /**************************************************************************\ |
---|
4 | |
---|
5 | MODULE: xdouble |
---|
6 | |
---|
7 | SUMMARY: |
---|
8 | |
---|
9 | The class xdouble is used to represent floating point numbers with the |
---|
10 | same precision as a 'double', but with extended exponent range |
---|
11 | (offering a few more bits than that of a 'long' for the exponent). |
---|
12 | |
---|
13 | The programming interface for xdoubles is almost identical to that of |
---|
14 | ordinary doubles. |
---|
15 | |
---|
16 | |
---|
17 | \**************************************************************************/ |
---|
18 | |
---|
19 | #include <NTL/ZZ.h> |
---|
20 | |
---|
21 | |
---|
22 | class xdouble { |
---|
23 | |
---|
24 | public: |
---|
25 | |
---|
26 | xdouble(); // = 0 |
---|
27 | |
---|
28 | xdouble(const xdouble& a); // copy constructor |
---|
29 | |
---|
30 | xdouble& operator=(const xdouble& a); // assignment operator |
---|
31 | xdouble& operator=(double a); |
---|
32 | |
---|
33 | ~xdouble(); |
---|
34 | |
---|
35 | |
---|
36 | double mantissa() const; // read-only access to mantissa |
---|
37 | long exponent() const; // read-only access to exponenent |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | static void SetOutputPrecision(long p); |
---|
42 | // This sets the number of decimal digits to be output. Default is |
---|
43 | // 10. |
---|
44 | |
---|
45 | static long OutputPrecision(); |
---|
46 | // returns current output precision. |
---|
47 | |
---|
48 | }; |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | /**************************************************************************\ |
---|
53 | |
---|
54 | Arithmetic Operations |
---|
55 | |
---|
56 | The following are the standard arithmetic operators, whose meaning should |
---|
57 | be clear. |
---|
58 | |
---|
59 | \**************************************************************************/ |
---|
60 | |
---|
61 | |
---|
62 | xdouble operator+(const xdouble& a, const xdouble& b); |
---|
63 | xdouble operator-(const xdouble& a, const xdouble& b); |
---|
64 | xdouble operator*(const xdouble& a, const xdouble& b); |
---|
65 | xdouble operator/(const xdouble& a, const xdouble& b); |
---|
66 | |
---|
67 | // PROMOTIONS: +, -, *, / promote double to xdouble on (a, b). |
---|
68 | |
---|
69 | xdouble operator-(const xdouble& a); |
---|
70 | |
---|
71 | xdouble& operator+=(xdouble& a, const xdouble& b); |
---|
72 | xdouble& operator+=(xdouble& a, double b); |
---|
73 | |
---|
74 | xdouble& operator-=(xdouble& a, const xdouble& b); |
---|
75 | xdouble& operator-=(xdouble& a, double b); |
---|
76 | |
---|
77 | xdouble& operator*=(xdouble& a, const xdouble& b); |
---|
78 | xdouble& operator*=(xdouble& a, double b); |
---|
79 | |
---|
80 | xdouble& operator/=(xdouble& a, const xdouble& b); |
---|
81 | xdouble& operator/=(xdouble& a, xdouble b); |
---|
82 | |
---|
83 | xdouble& operator++(xdouble& a); // prefix |
---|
84 | void operator++(xdouble& a, int); // postfix |
---|
85 | |
---|
86 | xdouble& operator--(xdouble& a); // prefix |
---|
87 | void operator--(xdouble& a, int); // postfix |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | /**************************************************************************\ |
---|
92 | |
---|
93 | Comparison |
---|
94 | |
---|
95 | \**************************************************************************/ |
---|
96 | |
---|
97 | long sign(const xdouble& a); |
---|
98 | // returns sign (+1, -1, 0) of a |
---|
99 | |
---|
100 | long compare(const xdouble& a, const xdouble& b); |
---|
101 | // returns sign of a - b |
---|
102 | |
---|
103 | long operator==(const xdouble& a, const xdouble& b); |
---|
104 | long operator!=(const xdouble& a, const xdouble& b); |
---|
105 | long operator<=(const xdouble& a, const xdouble& b); |
---|
106 | long operator>=(const xdouble& a, const xdouble& b); |
---|
107 | long operator <(const xdouble& a, const xdouble& b); |
---|
108 | long operator >(const xdouble& a, const xdouble& b); |
---|
109 | |
---|
110 | // PROMOTIONS: compare and operators ==, ..., > promote double to xdouble |
---|
111 | // on (a, b). |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | /**************************************************************************\ |
---|
116 | |
---|
117 | Input/Output |
---|
118 | Input Syntax: |
---|
119 | |
---|
120 | <number>: [ "-" ] <unsigned-number> |
---|
121 | <unsigned-number>: <dotted-number> [ <e-part> ] | <e-part> |
---|
122 | <dotted-number>: <digits> | <digits> "." <digits> | "." <digits> | <digits> "." |
---|
123 | <digits>: <digit> <digits> | <digit> |
---|
124 | <digit>: "0" | ... | "9" |
---|
125 | <e-part>: ( "E" | "e" ) [ "+" | "-" ] <digits> |
---|
126 | |
---|
127 | Examples of valid input: |
---|
128 | |
---|
129 | 17 1.5 0.5 .5 5. -.5 e10 e-10 e+10 1.5e10 .5e10 .5E10 |
---|
130 | |
---|
131 | Note that the number of decimal digits of precision that are used |
---|
132 | for output can be set to any number p >= 1 by calling |
---|
133 | the routine xdouble::SetOutputPrecision(p). |
---|
134 | The default value of p is 10. |
---|
135 | The current value of p is returned by a call to xdouble::OutputPrecision(). |
---|
136 | |
---|
137 | \**************************************************************************/ |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | ostream& operator<<(ostream& s, const xdouble& a); |
---|
142 | |
---|
143 | istream& operator>>(istream& s, xdouble& x); |
---|
144 | |
---|
145 | |
---|
146 | /**************************************************************************\ |
---|
147 | |
---|
148 | Miscellaneous |
---|
149 | |
---|
150 | \**************************************************************************/ |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | xdouble trunc(const xdouble& a); // returns integer obtained by truncating |
---|
155 | xdouble floor(const xdouble& a); // returns greatest integer <= a |
---|
156 | xdouble ceil(const xdouble& a); // returns smallest integer >= a |
---|
157 | xdouble fabs(const xdouble& a); // returns |a| |
---|
158 | xdouble sqrt(const xdouble& a); // returns a^{1/2}; error is raised if a < 0 |
---|
159 | |
---|
160 | double log(const xdouble& a); // returns log(a) (note return val is double!) |
---|
161 | xdouble xexp(double a); // returns exp(a) (note argument is double!) |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | void power(xdouble& z, const xdouble& a, const ZZ& e); |
---|
166 | xdouble power(const xdouble& a, const ZZ& e); |
---|
167 | |
---|
168 | void power(xdouble& z, const xdouble& a, long e); |
---|
169 | xdouble power(const xdouble& a, long e); |
---|
170 | // z = a^e, e may be negative |
---|
171 | |
---|
172 | void power2(xdouble& z, long e); |
---|
173 | xdouble power2_xdouble(long e); |
---|
174 | // z = 2^e, e may be negative |
---|
175 | |
---|
176 | void MulAdd(xdouble& z, const xdouble& a, const xdouble& b, const xdouble& c); |
---|
177 | xdouble MulAdd(const xdouble& a, const xdouble& b, const xdouble& c); |
---|
178 | // z = a + b*c, but faster |
---|
179 | |
---|
180 | void MulSub(xdouble& z, const xdouble& a, const xdouble& b, const xdouble& c); |
---|
181 | xdouble MulSub(const xdouble& a, const xdouble& b, const xdouble& c); |
---|
182 | // z = a - b*c, but faster |
---|
183 | |
---|
184 | |
---|
185 | /**************************************************************************\ |
---|
186 | |
---|
187 | Implementation details: |
---|
188 | |
---|
189 | An xdouble is represented as a mantissa/exponent pair (x, e), where x |
---|
190 | is a double and e is a long. The real number represented by (x, e) is |
---|
191 | x * NTL_XD_BOUND^e, where |
---|
192 | |
---|
193 | NTL_XD_BOUND = NTL_XD_HBOUND^2, and |
---|
194 | NTL_XD_HBOUND = 2^{(max(NTL_DOUBLE_PRECISION,NTL_BITS_PER_LONG)+4)}. |
---|
195 | |
---|
196 | Also, the mantissa x satisfies 1/NTL_XD_HBOUND <= |x| <= NTL_XD_HBOUND, except |
---|
197 | that the number 0 is always represented as (0, 0). |
---|
198 | Both NTL_XD_BOUND and NTL_XD_HBOUND are macros defined in <NTL/xdouble.h>. |
---|
199 | |
---|
200 | SIZE INVARIANT: |e| < 2^(NTL_BITS_PER_LONG-4). |
---|
201 | |
---|
202 | \**************************************************************************/ |
---|
203 | |
---|