|
|
1.1 ! root 1: ! 2: ! 3: multiple-precision mathematicsOverviewmultiple-precision mathematics ! 4: ! 5: ! 6: ! 7: ! 8: The COHERENT system includes the library libmp, whose routines ! 9: allow you to perform multiple precision arithmetic. These ! 10: functions manipulate a data structure called a mint, or ``mul- ! 11: tiple-precision integer,'' which the header file mprec.h defines ! 12: as follows: ! 13: ! 14: ! 15: typedef struct { ! 16: unsigned len; ! 17: char *val; ! 18: } mint; ! 19: ! 20: ! 21: You should not depend on the details of this structure, because ! 22: on some machines a different representation may be more effi- ! 23: cient. Using the listed functions is always safe. ! 24: ! 25: The following gives the multiple-precision routines: ! 26: ! 27: ggccdd() Set variable to greatest common divisor ! 28: iissppooss() Return if variable is positive or negative ! 29: iittoomm() Create a multiple-precision integer ! 30: mmaadddd() Add multiple-precision integers ! 31: mmccmmpp() Compare multiple-precision integers ! 32: mmccooppyy() Copy a multiple-precision integer ! 33: mmddiivv() Divide multiple-precision integers ! 34: mmiinn() Read multiple-precision integer from stdin ! 35: mmiinniitt() Condition global or auto multiple-precision integer ! 36: mmiinnttffrr() Free a multiple-precision integer ! 37: mmiittoomm() Reinitialize a multiple-precision integer ! 38: mmnneegg() Negate multiple-precision integer ! 39: mmoouutt() Write multiple-precision integer to stdout ! 40: mmssqqrrtt() Compute square root of multiple-precision integer ! 41: mmssuubb() Subtract multiple-precision integers ! 42: mmttooii() Convert multiple-precision integer to integer ! 43: mmttooss() Convert multiple-precision integer to string ! 44: mmuulltt() Multiple multiple-precision integers ! 45: mmvvffrreeee() Free multiple-precision integer ! 46: ppooww() Raise multiple-precision integer to power ! 47: rrppooww() Raise multiple-precision integer to power ! 48: ssddiivv() Divide multiple-precision integers ! 49: ssmmuulltt() Multiply multiple-precision integers ! 50: ssppooww() Raise multiple-precision integer to power ! 51: xxggccdd() Extended greatest-common-divisor function ! 52: zzeerroopp() Indicate if multi-precision integer is zero ! 53: ! 54: itom() creates a new mint, initializes it to the signed integer ! 55: value n, and returns a pointer to it. Storage used by a mint ! 56: created with itom may be reclaimed using mintfr(). ! 57: ! 58: A mint that already exists may be reinitialized by mitom(), which ! 59: sets a to the value n. If the mint was declared as a global or ! 60: automatic variable, it must be conditioned before first use by ! 61: minit(), which prevents garbage values in the mint structure from ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: multiple-precision mathematicsOverviewmultiple-precision mathematics ! 70: ! 71: ! 72: ! 73: causing chaos. A mint conditioned by minit() has no value; ! 74: however, it may be used to receive the result of an operation. ! 75: For mints automatic to a function, mvfree() should be used before ! 76: the function is exited to free the storage used by the val field ! 77: of the mint structure. Otherwise, this storage will never be ! 78: reclaimed. ! 79: ! 80: madd(), msub(), and mult() set c to the sum, difference, or ! 81: product of a and b. mdiv divides a by b and writes the quotient ! 82: and remainder in q and r. b must not be zero. The results of ! 83: the operation are defined by the following conditions: ! 84: ! 85: 11. _a=_q*_b+_r ! 86: ! 87: 22. The sign of _r equals the sign of q ! 88: ! 89: 33. The absolute value of r < the absolute value of b. ! 90: ! 91: smult() is like mult(), except the second argument is an integer ! 92: in the range 0 <= n <= 127. sdiv() is like mdiv(), except the ! 93: second argument is an integer in the range 1 <= n <= 128, and the ! 94: remainder argument points to an int instead of a mint(). ! 95: ! 96: pow() sets c to a raised to the b power reduced modulo m. rpow() ! 97: sets c to a raised to the b power. spow() is like rpow(), except ! 98: the exponent is an integer. In no case may the exponent be nega- ! 99: tive. ! 100: ! 101: mcopy() sets b equal to a. mneg() sets b equal to negative a. ! 102: ! 103: msqrt() sets b to the integral portion of the positive square ! 104: root of a; r is set to the remainder. a must not be negative. ! 105: The result of the operation is defined by the condition ! 106: ! 107: ! 108: _a = _b * _b + _r ! 109: ! 110: ! 111: gcd() sets c to the greatest common divisor of a and b. xgcd() ! 112: is an extended gcd routine that sets g to the greatest common ! 113: divisor of a and b, and sets r and s so the relation ! 114: ! 115: ! 116: _g = _a * _r + _b * _s ! 117: ! 118: ! 119: holds. For xgcd(), r, s and g must all be distinct. ! 120: ! 121: mints may be compared with mcmp(), which returns a signed integer ! 122: less than, equal to, or greater than zero according to whether a ! 123: is less than, equal to, or greater than b. ispos() returns true ! 124: (nonzero) if a is not negative, false (zero) if a is negative. ! 125: zerop returns true if a is zero, false otherwise. ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132: ! 133: ! 134: ! 135: multiple-precision mathematicsOverviewmultiple-precision mathematics ! 136: ! 137: ! 138: ! 139: mtoi() returns an integer equal to the value of a. a should be ! 140: in the allowable range for a signed integer. ! 141: ! 142: The external integers ibase and obase govern the I/O and ASCII ! 143: conversion routines. Allowable bases run from two to 16. Per- ! 144: missible digits are 0 through 9 and A through F (lower-case let- ! 145: ters are not allowed). min reads a mint in base ibase from the ! 146: standard input and sets a to that value. Leading blanks and an ! 147: optional leading minus sign are allowed; the number is terminated ! 148: by the first non-legal digit. mout() outputs a on the standard ! 149: output in base obase. mtos() performs the same conversion as ! 150: mout(), but the result is placed in a character string instead of ! 151: being output; a pointer to the string is returned. The string is ! 152: actually allocated by malloc(), and may be freed by free(). ! 153: ! 154: mzero() and mone() point to mints with values zero and one. ! 155: mminint() and mmaxint() point to mints containing the minimum and ! 156: maximum values that will fit in a signed integer. These con- ! 157: stants should never be used as the result of an operation. ! 158: ! 159: All the necessary declarations for these constants and for the ! 160: library functions are contained in the header file mprec.h. They ! 161: need not be repeated. ! 162: ! 163: To link mp modules with an executable object, use the argument ! 164: -lmp with the cc or ld commands. ! 165: ! 166: ***** Example ***** ! 167: ! 168: The following example converts a string into a multi-precision ! 169: integer. ! 170: ! 171: ! 172: #include <stdio.h> ! 173: #include <mprec.h> ! 174: #include <ctype.h> ! 175: ! 176: ! 177: ! 178: /* ! 179: * "ibase" is an int which contains the input base used by "stom". ! 180: * It should be between 2 and 16. ! 181: */ ! 182: int ibase = 10; ! 183: ! 184: ! 185: ! 186: /* ! 187: * stom() reads in a number in base ibase from string 'a' and returns ! 188: * pointer to multiple-precision integer. ! 189: */ ! 190: mint *stom(s) ! 191: register char *s; ! 192: { ! 193: char cval; ! 194: ! 195: ! 196: COHERENT Lexicon Page 3 ! 197: ! 198: ! 199: ! 200: ! 201: multiple-precision mathematicsOverviewmultiple-precision mathematics ! 202: ! 203: ! 204: ! 205: mint c = {1, &cval}; ! 206: register int ch; ! 207: char mifl = 0;/* leading minus flag */ ! 208: static mintnumber; ! 209: ! 210: ! 211: ! 212: mcopy(mzero, &number);/* set number to zero */ ! 213: if ((ch = *s) == '-') {/* skip leading '-' */ ! 214: mifl = 1; ! 215: ch = *++s; ! 216: } ! 217: ! 218: ! 219: ! 220: /* scan thru string 's', building result in "number" */ ! 221: while (isascii(ch) && isdigit(ch)) { ! 222: cval = (isdigit(ch) ? ch - '0': ch - 'A'); ! 223: smult(&number, ibase, &number); ! 224: madd(&number, &c, &number); ! 225: ch = *++s; ! 226: } ! 227: ! 228: ! 229: ! 230: if (mifl)/* adjust sign of a "number" */ ! 231: mneg(&number, &number); ! 232: return(&number); ! 233: } ! 234: ! 235: ! 236: ! 237: /* simple test for "stom" */ ! 238: main() ! 239: { ! 240: charbuffer[80]; ! 241: ! 242: ! 243: ! 244: printf("Input string ? "); ! 245: gets(buffer); ! 246: mout(stom(buffer)); /* Print in stdout multiple-precision int */ ! 247: } ! 248: ! 249: ! 250: ***** Files ***** ! 251: ! 252: <mprec.h> ! 253: /usr/lib/libmp.a ! 254: ! 255: ***** See Also ***** ! 256: ! 257: bc, dc, libraries, malloc, mprec.h ! 258: ! 259: ! 260: ! 261: ! 262: COHERENT Lexicon Page 4 ! 263: ! 264: ! 265: ! 266: ! 267: multiple-precision mathematicsOverviewmultiple-precision mathematics ! 268: ! 269: ! 270: ! 271: ***** Diagnostics ***** ! 272: ! 273: On any error, such as division by zero, running out of space or ! 274: taking the square root of a negative number, an appropriate mes- ! 275: sage is printed on the standard error stream and the program ex- ! 276: its with a nonzero status. ! 277: ! 278: ! 279: ! 280: ! 281: ! 282: ! 283: ! 284: ! 285: ! 286: ! 287: ! 288: ! 289: ! 290: ! 291: ! 292: ! 293: ! 294: ! 295: ! 296: ! 297: ! 298: ! 299: ! 300: ! 301: ! 302: ! 303: ! 304: ! 305: ! 306: ! 307: ! 308: ! 309: ! 310: ! 311: ! 312: ! 313: ! 314: ! 315: ! 316: ! 317: ! 318: ! 319: ! 320: ! 321: ! 322: ! 323: ! 324: ! 325: ! 326: ! 327: ! 328: COHERENT Lexicon Page 5 ! 329: ! 330:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.