Debian Codegraph @

terps/tads/tads3/include/bignum.h

raw symbols SCIP
1 #charset "us-ascii"
2 #pragma once
3
4 /*
5 * Copyright (c) 2000, 2006 Michael J. Roberts
6 *
7 * This file is part of TADS 3.
8 *
9 * This header defines the BigNumber intrinsic class.
10 */
11
12 /* include our base class definition */
13 #include "systype.h"
14
15 /*
16 * The BigNumber intrinsic class lets you perform floating-point and
17 * integer arithmetic with (almost) any desired precision. BigNumber uses
18 * a decimal representation, which means that decimal values can be
19 * represented exactly (i.e., with no rounding errors, as can happen with
20 * IEEE 'double' and 'float' values that languages like C typically
21 * support). BigNumber combines a varying-length mantissa with an
22 * exponent; the length of the mantissa determines how many digits of
23 * precision a given BigNumber can store, and the exponent lets you
24 * represent very large or very small values with minimal storage. You can
25 * specify the desired precision when you create a BigNumber explicitly;
26 * when BigNumber values are created implicitly by computations, the system
27 * chooses a precision based on the inputs to the calculations, typically
28 * equal to the largest of the precisions of the input values.
29 *
30 * The maximum precision for a BigNumber is about 64,000 digits, and the
31 * exponent can range from -32768 to +32767. Since this is a decimal
32 * exponent, this implies an absolute value range from 1.0e-32768 to
33 * 1.0e+32767. The more digits of precision stored in a given BigNumber
34 * value, the more memory the object consumes, and the more time it takes
35 * to perform calculations using the value.
36 */
37 intrinsic class BigNumber 'bignumber/030001': Object
38 {
39 /* format to a string */
40 formatString(maxDigits?, flags?,
41 wholePlaces?, fracDigits?, expDigits?, leadFiller?);
42
43 /*
44 * compare for equality after rounding to the smaller of my
45 * precision and num's precision
46 */
47 equalRound(num);
48
49 /*
50 * returns an integer giving the number of digits of precision that
51 * this number stores
52 */
53 getPrecision();
54
55 /*
56 * Return a new number, with the same value as this number but with
57 * the given number of decimal digits of precision. If the new
58 * precision is higher than the old precision, this will increase
59 * the precision to the requested new size and add trailing zeros
60 * to the value. If the new precision is lower than the old
61 * precision, we'll round the number for the reduced precision.
62 */
63 setPrecision(digits);
64
65 /* get the fractional part */
66 getFraction();
67
68 /* get the whole part (truncates the fraction - doesn't round) */
69 getWhole();
70
71 /*
72 * round to the given number of digits after the decimal point; if
73 * the value is zero, round to integer; if the value is negative,
74 * round to the given number of places before the decimal point
75 */
76 roundToDecimal(places);
77
78 /* return the absolute value */
79 getAbs();
80
81 /* least integer greater than or equal to this number */
82 getCeil();
83
84 /* greatest integer less than or equal to this number */
85 getFloor();
86
87 /* get the base-10 scale of the number */
88 getScale();
89
90 /*
91 * scale by 10^x - if x is positive, this multiplies the number by
92 * ten the given number of times; if x is negative, this divides the
93 * number by ten the given number of times
94 */
95 scaleTen(x);
96
97 /* negate - invert the sign of the number */
98 negate();
99
100 /*
101 * copySignFrom - combine the absolute value of self with the sign
102 * of x
103 */
104 copySignFrom(x);
105
106 /* determine if the value is negative */
107 isNegative();
108
109 /*
110 * Calculate the integer quotient and the remainder; returns a list
111 * whose first element is the integer quotient (a BigNumber
112 * containing an integer value), and whose second element is the
113 * remainder (the value R such that dividend = quotient*x + R).
114 *
115 * Note that the quotient returned will not necessarily have the
116 * same value as the whole part of dividing self by x with the '/'
117 * operator, because this division handles rounding differently. In
118 * particular, the '/' operator will perform the appropriate
119 * rounding on the quotient if the quotient has insufficient
120 * precision to represent the exact result. This routine, in
121 * contrast, does NOT round the quotient, but merely truncates any
122 * trailing digits that cannot be represented in the result's
123 * precision. The reason for this difference is that it ensures
124 * that the relation (dividend=quotient*x+remainder) holds, which
125 * would not always be the case if the quotient were rounded up.
126 *
127 * Note also that the remainder will not necessarily be less than
128 * the divisor. If the quotient cannot be exactly represented
129 * (which occurs if the precision of the quotient is smaller than
130 * its scale), the remainder will be the correct value so that the
131 * relationship above holds, rather than the unique remainder that
132 * is smaller than the divisor. In all cases where there is
133 * sufficient precision to represent the quotient exactly (to the
134 * units digit only, since the quotient returned from this method
135 * will always be an integer), the remainder will satisfy the
136 * relationship AND will be the unique remainder with absolute value
137 * less than the divisor.
138 */
139 divideBy(x);
140
141 /*
142 * calculate and return the trigonometric sine of the value (taken
143 * as a radian value)
144 */
145 sine();
146
147 /*
148 * calculate and return the trigonometric cosine of the value (taken
149 * as a radian value)
150 */
151 cosine();
152
153 /*
154 * calculate and return the trigonometric tangent of the value
155 * (taken as a radian value)
156 */
157 tangent();
158
159 /*
160 * interpreting this number as a number of degrees, convert the
161 * value to radians and return the result
162 */
163 degreesToRadians();
164
165 /*
166 * interpreting this number as a number of radians, convert the
167 * value to degrees and return the result
168 */
169 radiansToDegrees();
170
171 /*
172 * Calculate and return the arcsine (in radians) of the value. Note
173 * that the value must be between -1 and +1 inclusive, since sine()
174 * never has a value outside of this range.
175 */
176 arcsine();
177
178 /*
179 * Calculate and return the arccosine (in radians). The value must
180 * be between -1 and +1 inclusive.
181 */
182 arccosine();
183
184 /* calculate and return the arctangent (in radians) */
185 arctangent();
186
187 /* calculate the square root and return the result */
188 sqrt();
189
190 /*
191 * calculate the natural logarithm of this number and return the
192 * result
193 */
194 logE();
195
196 /*
197 * raise e (the base of the natural logarithm) to the power of this
198 * value and return the result
199 */
200 expE();
201
202 /* calculate the base-10 logarithm of the number and return the result */
203 log10();
204
205 /*
206 * raise this number to the power of the argument and return the
207 * result
208 */
209 raiseToPower(x);
210
211 /* calculate the hyperbolic sine, cosine, and tangent */
212 sinh();
213 cosh();
214 tanh();
215
216 /* class method: get the value of pi to a given precision */
217 getPi(digits);
218
219 /* class method: get the value of e to a given precision */
220 getE(digits);
221
222 /*
223 * Get the type of this number. This returns a combination of
224 * NumTypeXxx flags, combined with the '|' operator. This can be used
225 * to check for special values, such as infinites and "not a number"
226 * values.
227 */
228 numType();
229 }
230
231 /* ------------------------------------------------------------------------ */
232 /*
233 * flags for formatString
234 */
235
236 /* always show a sign, even if positive */
237 #define BignumSign 0x0001
238
239 /* always show in exponential format (scientific notation, as in "1.0e20") */
240 #define BignumExp 0x0002
241
242 /* always show a sign in the exponent, even if positive */
243 #define BignumExpSign 0x0004
244
245 /*
246 * show a zero before the decimal point - this is only relevant in
247 * non-exponential format when the number is between -1 and +1
248 */
249 #define BignumLeadingZero 0x0008
250
251 /* always show a decimal point */
252 #define BignumPoint 0x0010
253
254 /* insert commas to denote thousands, millions, etc */
255 #define BignumCommas 0x0020
256
257 /* show a leading space if the number is positive */
258 #define BignumPosSpace 0x0040
259
260 /*
261 * use European-style formatting: use a comma instead of a period for
262 * the decimal point, and use periods instead of commas to set off
263 * thousands, millions, etc
264 */
265 #define BignumEuroStyle 0x0080
266
267 /*
268 * "Compact" format: use the shorter of the regular format and scientific
269 * notation. If the scientific notation exponent is less than -4 or
270 * greater than or equal to the number of digits after the decimal point,
271 * we'll use scientific notation; otherwise we'll use the plain format.
272 */
273 #define BignumCompact 0x0100
274
275 /*
276 * maxDigits counts only significant digits; leading zeros aren't counted
277 * against the maximum.
278 */
279 #define BignumMaxSigDigits 0x0200
280
281 /*
282 * Keep trailing zeros. If there's a maxDigits value, this keeps enough
283 * trailing zeros so that the number of digits shown equals maxDigits. By
284 * default, trailing zeros after the decimal point are removed.
285 */
286 #define BignumKeepTrailingZeros 0x0400
287
288
289 /* ------------------------------------------------------------------------ */
290 /*
291 * Number type flags, for numType()
292 */
293
294 /*
295 * Number type: ordinary number.
296 */
297 #define NumTypeNum 0x0001
298
299 /*
300 * Number type: "Not a number" (NaN). This indicates that the value is the
301 * result of a calculation with invalid input(s). Currently there are no
302 * BigNumber calculations that return NaNs, as all functions on invalid
303 * inputs throw errors instead. But it's possible to construct NaN value,
304 * such as by reading an IEEE 754-2008 NaN value from a file via
305 * unpackBytes().
306 */
307 #define NumTypeNAN 0x0002
308
309 /*
310 * Number type: positive infinity, negative infinity. These indicate a
311 * value that overflowed the capacity of the BigNumber type, or a
312 * calculation that yields infinity (e.g., tan(pi/2)). Currently there are
313 * no BigNumber calculations that return Infinities, as all functions where
314 * an overflow is possible throw errors instead. But it's possible to
315 * construct an Infinity value, such as by reading an IEEE 754-2008
316 * Infinity value from a file via unpackBytes().
317 */
318 #define NumTypePInf 0x0004
319 #define NumTypeNInf 0x0008
320 #define NumTypeInf (NumTypePInf | NumTypeNInf)
321
322 /*
323 * Number type: zero, positive or negative. Mathematically, zero is
324 * neither positive nor negative, but the BigNumber type retains a sign for
325 * all values, even zeros. Negative zeros can come from calculations
326 * that yield negative results with absolute values too small for the
327 * internal representation. It's also possible to construct a negative
328 * zero, such as by reading an IEEE 754-2008 negative zero value from a
329 * file via unpackBytes().
330 */
331 #define NumTypePZero 0x0010
332 #define NumTypeNZero 0x0020
333 #define NumTypeZero (NumTypePZero | NumTypeNZero)
334
335