home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume27 / calc-2.9.0 / part11 < prev    next >
Text File  |  1993-12-07  |  59KB  |  2,448 lines

  1. Newsgroups: comp.sources.unix
  2. From: dbell@canb.auug.org.au (David I. Bell)
  3. Subject: v27i138: calc-2.9.0 - arbitrary precision C-like programmable calculator, Part11/19
  4. References: <1.755316719.21314@gw.home.vix.com>
  5. Sender: unix-sources-moderator@gw.home.vix.com
  6. Approved: vixie@gw.home.vix.com
  7.  
  8. Submitted-By: dbell@canb.auug.org.au (David I. Bell)
  9. Posting-Number: Volume 27, Issue 138
  10. Archive-Name: calc-2.9.0/part11
  11.  
  12. #!/bin/sh
  13. # this is part 11 of a multipart archive
  14. # do not concatenate these parts, unpack them in order with /bin/sh
  15. # file calc2.9.0/value.c continued
  16. #
  17. CurArch=11
  18. if test ! -r s2_seq_.tmp
  19. then echo "Please unpack part 1 first!"
  20.      exit 1; fi
  21. ( read Scheck
  22.   if test "$Scheck" != $CurArch
  23.   then echo "Please unpack part $Scheck next!"
  24.        exit 1;
  25.   else exit 0; fi
  26. ) < s2_seq_.tmp || exit 1
  27. echo "x - Continuing file calc2.9.0/value.c"
  28. sed 's/^X//' << 'SHAR_EOF' >> calc2.9.0/value.c
  29. X * Raise one value to another value's power, within the specified error.
  30. X * Result is placed in the indicated location.
  31. X */
  32. Xvoid
  33. Xpowervalue(v1, v2, v3, vres)
  34. X    VALUE *v1, *v2, *v3, *vres;
  35. X{
  36. X    NUMBER *epsilon;
  37. X    COMPLEX *c, ctmp;
  38. X
  39. X    vres->v_type = V_NULL;
  40. X    if (v3->v_type != V_NUM)
  41. X        math_error("Non-real epsilon value for power");
  42. X    epsilon = v3->v_num;
  43. X    if (qisneg(epsilon) || qiszero(epsilon))
  44. X        math_error("Non-positive epsilon value for power");
  45. X    switch (TWOVAL(v1->v_type, v2->v_type)) {
  46. X        case TWOVAL(V_NUM, V_NUM):
  47. X            vres->v_num = qpower(v1->v_num, v2->v_num, epsilon);
  48. X            vres->v_type = V_NUM;
  49. X            return;
  50. X        case TWOVAL(V_NUM, V_COM):
  51. X            ctmp.real = v1->v_num;
  52. X            ctmp.imag = &_qzero_;
  53. X            ctmp.links = 1;
  54. X            vres->v_com = cpower(&ctmp, v2->v_com, epsilon);
  55. X            break;
  56. X        case TWOVAL(V_COM, V_NUM):
  57. X            ctmp.real = v2->v_num;
  58. X            ctmp.imag = &_qzero_;
  59. X            ctmp.links = 1;
  60. X            vres->v_com = cpower(v1->v_com, &ctmp, epsilon);
  61. X            break;
  62. X        case TWOVAL(V_COM, V_COM):
  63. X            vres->v_com = cpower(v1->v_com, v2->v_com, epsilon);
  64. X            break;
  65. X        default:
  66. X            math_error("Illegal value for raising to power");
  67. X    }
  68. X    /*
  69. X     * Here for any complex result.
  70. X     */
  71. X    vres->v_type = V_COM;
  72. X    c = vres->v_com;
  73. X    if (!cisreal(c))
  74. X        return;
  75. X    vres->v_num = qlink(c->real);
  76. X    vres->v_type = V_NUM;
  77. X    comfree(c);
  78. X}
  79. X
  80. X
  81. X/*
  82. X * Divide one arbitrary value by another one.
  83. X * Result is placed in the indicated location.
  84. X */
  85. Xvoid
  86. Xdivvalue(v1, v2, vres)
  87. X    VALUE *v1, *v2, *vres;
  88. X{
  89. X    COMPLEX *c;
  90. X    COMPLEX ctmp;
  91. X    VALUE tmpval;
  92. X
  93. X    vres->v_type = V_NULL;
  94. X    switch (TWOVAL(v1->v_type, v2->v_type)) {
  95. X        case TWOVAL(V_NUM, V_NUM):
  96. X            vres->v_num = qdiv(v1->v_num, v2->v_num);
  97. X            vres->v_type = V_NUM;
  98. X            return;
  99. X        case TWOVAL(V_COM, V_NUM):
  100. X            vres->v_com = cdivq(v1->v_com, v2->v_num);
  101. X            vres->v_type = V_COM;
  102. X            return;
  103. X        case TWOVAL(V_NUM, V_COM):
  104. X            if (qiszero(v1->v_num)) {
  105. X                vres->v_num = qlink(&_qzero_);
  106. X                vres->v_type = V_NUM;
  107. X                return;
  108. X            }
  109. X            ctmp.real = v1->v_num;
  110. X            ctmp.imag = &_qzero_;
  111. X            ctmp.links = 1;
  112. X            vres->v_com = cdiv(&ctmp, v2->v_com);
  113. X            vres->v_type = V_COM;
  114. X            return;
  115. X        case TWOVAL(V_COM, V_COM):
  116. X            vres->v_com = cdiv(v1->v_com, v2->v_com);
  117. X            vres->v_type = V_COM;
  118. X            c = vres->v_com;
  119. X            if (cisreal(c)) {
  120. X                vres->v_num = qlink(c->real);
  121. X                vres->v_type = V_NUM;
  122. X                comfree(c);
  123. X            }
  124. X            return;
  125. X        case TWOVAL(V_MAT, V_NUM):
  126. X        case TWOVAL(V_MAT, V_COM):
  127. X            invertvalue(v2, &tmpval);
  128. X            vres->v_mat = matmulval(v1->v_mat, &tmpval);
  129. X            vres->v_type = V_MAT;
  130. X            freevalue(&tmpval);
  131. X            return;
  132. X        default:
  133. X            if ((v1->v_type != V_OBJ) && (v2->v_type != V_OBJ))
  134. X                math_error("Non-compatible values for divide");
  135. X            *vres = objcall(OBJ_DIV, v1, v2, NULL_VALUE);
  136. X            return;
  137. X    }
  138. X}
  139. X
  140. X
  141. X/*
  142. X * Divide one arbitrary value by another one keeping only the integer part.
  143. X * Result is placed in the indicated location.
  144. X */
  145. Xvoid
  146. Xquovalue(v1, v2, vres)
  147. X    VALUE *v1, *v2, *vres;
  148. X{
  149. X    COMPLEX *c;
  150. X
  151. X    vres->v_type = V_NULL;
  152. X    switch (TWOVAL(v1->v_type, v2->v_type)) {
  153. X        case TWOVAL(V_NUM, V_NUM):
  154. X            vres->v_num = qquo(v1->v_num, v2->v_num);
  155. X            vres->v_type = V_NUM;
  156. X            return;
  157. X        case TWOVAL(V_COM, V_NUM):
  158. X            vres->v_com = cquoq(v1->v_com, v2->v_num);
  159. X            vres->v_type = V_COM;
  160. X            c = vres->v_com;
  161. X            if (cisreal(c)) {
  162. X                vres->v_num = qlink(c->real);
  163. X                vres->v_type = V_NUM;
  164. X                comfree(c);
  165. X            }
  166. X            return;
  167. X        case TWOVAL(V_MAT, V_NUM):
  168. X        case TWOVAL(V_MAT, V_COM):
  169. X            vres->v_mat = matquoval(v1->v_mat, v2);
  170. X            vres->v_type = V_MAT;
  171. X            return;
  172. X        default:
  173. X            if ((v1->v_type != V_OBJ) && (v2->v_type != V_OBJ))
  174. X                math_error("Non-compatible values for quotient");
  175. X            *vres = objcall(OBJ_QUO, v1, v2, NULL_VALUE);
  176. X            return;
  177. X    }
  178. X}
  179. X
  180. X
  181. X/*
  182. X * Divide one arbitrary value by another one keeping only the remainder.
  183. X * Result is placed in the indicated location.
  184. X */
  185. Xvoid
  186. Xmodvalue(v1, v2, vres)
  187. X    VALUE *v1, *v2, *vres;
  188. X{
  189. X    COMPLEX *c;
  190. X
  191. X    vres->v_type = V_NULL;
  192. X    switch (TWOVAL(v1->v_type, v2->v_type)) {
  193. X        case TWOVAL(V_NUM, V_NUM):
  194. X            vres->v_num = qmod(v1->v_num, v2->v_num);
  195. X            vres->v_type = V_NUM;
  196. X            return;
  197. X        case TWOVAL(V_COM, V_NUM):
  198. X            vres->v_com = cmodq(v1->v_com, v2->v_num);
  199. X            vres->v_type = V_COM;
  200. X            c = vres->v_com;
  201. X            if (cisreal(c)) {
  202. X                vres->v_num = qlink(c->real);
  203. X                vres->v_type = V_NUM;
  204. X                comfree(c);
  205. X            }
  206. X            return;
  207. X        case TWOVAL(V_MAT, V_NUM):
  208. X        case TWOVAL(V_MAT, V_COM):
  209. X            vres->v_mat = matmodval(v1->v_mat, v2);
  210. X            vres->v_type = V_MAT;
  211. X            return;
  212. X        default:
  213. X            if ((v1->v_type != V_OBJ) && (v2->v_type != V_OBJ))
  214. X                math_error("Non-compatible values for mod");
  215. X            *vres = objcall(OBJ_MOD, v1, v2, NULL_VALUE);
  216. X            return;
  217. X    }
  218. X}
  219. X
  220. X
  221. X/*
  222. X * Test an arbitrary value to see if it is equal to "zero".
  223. X * The definition of zero varies depending on the value type.  For example,
  224. X * the null string is "zero", and a matrix with zero values is "zero".
  225. X * Returns TRUE if value is not equal to zero.
  226. X */
  227. XBOOL
  228. Xtestvalue(vp)
  229. X    VALUE *vp;
  230. X{
  231. X    VALUE val;
  232. X
  233. X    switch (vp->v_type) {
  234. X        case V_NUM:
  235. X            return !qiszero(vp->v_num);
  236. X        case V_COM:
  237. X            return !ciszero(vp->v_com);
  238. X        case V_STR:
  239. X            return (vp->v_str[0] != '\0');
  240. X        case V_MAT:
  241. X            return mattest(vp->v_mat);
  242. X        case V_LIST:
  243. X            return (vp->v_list->l_count != 0);
  244. X        case V_ASSOC:
  245. X            return (vp->v_assoc->a_count != 0);
  246. X        case V_FILE:
  247. X            return validid(vp->v_file);
  248. X        case V_NULL:
  249. X            return FALSE;
  250. X        case V_OBJ:
  251. X            val = objcall(OBJ_TEST, vp, NULL_VALUE, NULL_VALUE);
  252. X            return (val.v_int != 0);
  253. X        default:
  254. X            return TRUE;
  255. X    }
  256. X}
  257. X
  258. X
  259. X/*
  260. X * Compare two values for equality.
  261. X * Returns TRUE if the two values differ.
  262. X */
  263. XBOOL
  264. Xcomparevalue(v1, v2)
  265. X    VALUE *v1, *v2;
  266. X{
  267. X    int r;
  268. X    VALUE val;
  269. X
  270. X    if ((v1->v_type == V_OBJ) || (v2->v_type == V_OBJ)) {
  271. X        val = objcall(OBJ_CMP, v1, v2, NULL_VALUE);
  272. X        return (val.v_int != 0);
  273. X    }
  274. X    if (v1 == v2)
  275. X        return FALSE;
  276. X    if (v1->v_type != v2->v_type)
  277. X        return TRUE;
  278. X    switch (v1->v_type) {
  279. X        case V_NUM:
  280. X            r = qcmp(v1->v_num, v2->v_num);
  281. X            break;
  282. X        case V_COM:
  283. X            r = ccmp(v1->v_com, v2->v_com);
  284. X            break;
  285. X        case V_STR:
  286. X            r = ((v1->v_str != v2->v_str) &&
  287. X                ((v1->v_str[0] - v2->v_str[0]) ||
  288. X                strcmp(v1->v_str, v2->v_str)));
  289. X            break;
  290. X        case V_MAT:
  291. X            r = matcmp(v1->v_mat, v2->v_mat);
  292. X            break;
  293. X        case V_LIST:
  294. X            r = listcmp(v1->v_list, v2->v_list);
  295. X            break;
  296. X        case V_ASSOC:
  297. X            r = assoccmp(v1->v_assoc, v2->v_assoc);
  298. X            break;
  299. X        case V_NULL:
  300. X            r = FALSE;
  301. X            break;
  302. X        case V_FILE:
  303. X            r = (v1->v_file != v2->v_file);
  304. X            break;
  305. X        default:
  306. X            math_error("Illegal values for comparevalue");
  307. X    }
  308. X    return (r != 0);
  309. X}
  310. X
  311. X
  312. X/*
  313. X * Compare two values for their relative values.
  314. X * Returns minus one if the first value is less than the second one,
  315. X * one if the first value is greater than the second one, and
  316. X * zero if they are equal.
  317. X */
  318. XFLAG
  319. Xrelvalue(v1, v2)
  320. X    VALUE *v1, *v2;
  321. X{
  322. X    int r;
  323. X    VALUE val;
  324. X
  325. X    if ((v1->v_type == V_OBJ) || (v2->v_type == V_OBJ)) {
  326. X        val = objcall(OBJ_REL, v1, v2, NULL_VALUE);
  327. X        return val.v_int;
  328. X    }
  329. X    if (v1 == v2)
  330. X        return 0;
  331. X    if (v1->v_type != v2->v_type)
  332. X        math_error("Relative comparison of differing types");
  333. X    switch (v1->v_type) {
  334. X        case V_NUM:
  335. X            r = qrel(v1->v_num, v2->v_num);
  336. X            break;
  337. X        case V_STR:
  338. X            r = strcmp(v1->v_str, v2->v_str);
  339. X            break;
  340. X        case V_NULL:
  341. X            r = 0;
  342. X            break;
  343. X        default:
  344. X            math_error("Illegal value for relative comparison");
  345. X    }
  346. X    if (r < 0)
  347. X        return -1;
  348. X    return (r != 0);
  349. X}
  350. X
  351. X
  352. X/*
  353. X * Calculate a hash value for a value.
  354. X * The hash does not have to be a perfect one, it is only used for
  355. X * making associations faster.
  356. X */
  357. XHASH
  358. Xhashvalue(vp)
  359. X    VALUE *vp;
  360. X{
  361. X    switch (vp->v_type) {
  362. X        case V_INT:
  363. X            return ((long) vp->v_int);
  364. X            break;
  365. X        case V_NUM:
  366. X            return qhash(vp->v_num);
  367. X            break;
  368. X        case V_COM:
  369. X            return chash(vp->v_com);
  370. X            break;
  371. X        case V_STR:
  372. X            return hashstr(vp->v_str);
  373. X            break;
  374. X        case V_NULL:
  375. X            return 0;
  376. X            break;
  377. X        case V_OBJ:
  378. X            return objhash(vp->v_obj);
  379. X            break;
  380. X        case V_LIST:
  381. X            return listhash(vp->v_list);
  382. X            break;
  383. X        case V_ASSOC:
  384. X            return assochash(vp->v_assoc);
  385. X            break;
  386. X        case V_MAT:
  387. X            return mathash(vp->v_mat);
  388. X            break;
  389. X        case V_FILE:
  390. X            return ((long) vp->v_file);
  391. X            break;
  392. X        default:
  393. X            math_error("Hashing unknown value");
  394. X    }
  395. X    return 0;
  396. X}
  397. X
  398. X
  399. X/*
  400. X * Print the value of a descriptor in one of several formats.
  401. X * If flags contains PRINT_SHORT, then elements of arrays and lists
  402. X * will not be printed.  If flags contains PRINT_UNAMBIG, then quotes
  403. X * are placed around strings and the null value is explicitly printed.
  404. X */
  405. Xvoid
  406. Xprintvalue(vp, flags)
  407. X    VALUE *vp;
  408. X{
  409. X    switch (vp->v_type) {
  410. X        case V_NUM:
  411. X            qprintnum(vp->v_num, MODE_DEFAULT);
  412. X            break;
  413. X        case V_COM:
  414. X            comprint(vp->v_com);
  415. X            break;
  416. X        case V_STR:
  417. X            if (flags & PRINT_UNAMBIG)
  418. X                math_chr('\"');
  419. X            math_str(vp->v_str);
  420. X            if (flags & PRINT_UNAMBIG)
  421. X                math_chr('\"');
  422. X            break;
  423. X        case V_NULL:
  424. X            if (flags & PRINT_UNAMBIG)
  425. X                math_str("NULL");
  426. X            break;
  427. X        case V_OBJ:
  428. X            (void) objcall(OBJ_PRINT, vp, NULL_VALUE, NULL_VALUE);
  429. X            break;
  430. X        case V_LIST:
  431. X            listprint(vp->v_list,
  432. X                ((flags & PRINT_SHORT) ? 0L : maxprint));
  433. X            break;
  434. X        case V_ASSOC:
  435. X            assocprint(vp->v_assoc,
  436. X                ((flags & PRINT_SHORT) ? 0L : maxprint));
  437. X            break;
  438. X        case V_MAT:
  439. X            matprint(vp->v_mat,
  440. X                ((flags & PRINT_SHORT) ? 0L : maxprint));
  441. X            break;
  442. X        case V_FILE:
  443. X            printid(vp->v_file, flags);
  444. X            break;
  445. X        default:
  446. X            math_error("Printing unknown value");
  447. X    }
  448. X}
  449. X
  450. X/* END CODE */
  451. SHAR_EOF
  452. echo "File calc2.9.0/value.c is complete"
  453. chmod 0644 calc2.9.0/value.c || echo "restore of calc2.9.0/value.c fails"
  454. set `wc -c calc2.9.0/value.c`;Sum=$1
  455. if test "$Sum" != "29836"
  456. then echo original size 29836, current size $Sum;fi
  457. echo "x - extracting calc2.9.0/value.h (Text)"
  458. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/value.h &&
  459. X/*
  460. X * Copyright (c) 1993 David I. Bell
  461. X * Permission is granted to use, distribute, or modify this source,
  462. X * provided that this copyright notice remains intact.
  463. X *
  464. X * Definitions of general values and related routines used by the calculator.
  465. X */
  466. X
  467. X#ifndef    VALUE_H
  468. X#define    VALUE_H
  469. X
  470. X#include "cmath.h"
  471. X
  472. X
  473. X#define MAXDIM        4    /* maximum number of dimensions in matrices */
  474. X#define USUAL_ELEMENTS    4    /* usual number of elements for objects */
  475. X
  476. X
  477. X/*
  478. X * Flags to modify results from the printvalue routine.
  479. X * These flags are OR'd together.
  480. X */
  481. X#define    PRINT_NORMAL    0x00    /* print in normal manner */
  482. X#define    PRINT_SHORT    0x01    /* print in short format (no elements) */
  483. X#define    PRINT_UNAMBIG    0x02    /* print in non-ambiguous manner */
  484. X
  485. X
  486. X/*
  487. X * Definition of values of various types.
  488. X */
  489. Xtypedef struct value VALUE;
  490. Xtypedef struct object OBJECT;
  491. Xtypedef struct matrix MATRIX;
  492. Xtypedef struct list LIST;
  493. Xtypedef    struct assoc ASSOC;
  494. Xtypedef    long FILEID;
  495. X
  496. X
  497. Xstruct value {
  498. X    short v_type;            /* type of value */
  499. X    short v_subtype;        /* other data related to some types */
  500. X    union {
  501. X        long vv_int;        /* small integer value */
  502. X        FILEID vv_file;        /* id of opened file */
  503. X        NUMBER *vv_num;        /* arbitrary sized numeric value */
  504. X        COMPLEX *vv_com;    /* complex number */
  505. X        VALUE *vv_addr;        /* address of variable value */
  506. X        MATRIX *vv_mat;        /* address of matrix */
  507. X        LIST *vv_list;        /* address of list */
  508. X        ASSOC *vv_assoc;    /* address of association */
  509. X        OBJECT *vv_obj;        /* address of object */
  510. X        char *vv_str;        /* string value */
  511. X    } v_union;
  512. X};
  513. X
  514. X
  515. X/*
  516. X * For ease in referencing
  517. X */
  518. X#define v_int    v_union.vv_int
  519. X#define    v_file    v_union.vv_file
  520. X#define v_num    v_union.vv_num
  521. X#define v_com    v_union.vv_com
  522. X#define v_addr    v_union.vv_addr
  523. X#define v_str    v_union.vv_str
  524. X#define v_mat    v_union.vv_mat
  525. X#define    v_list    v_union.vv_list
  526. X#define    v_assoc    v_union.vv_assoc
  527. X#define v_obj    v_union.vv_obj
  528. X#define    v_valid    v_union.vv_int
  529. X
  530. X
  531. X/*
  532. X * Value types.
  533. X */
  534. X#define V_NULL    0    /* null value */
  535. X#define V_INT    1    /* normal integer */
  536. X#define V_NUM    2    /* number */
  537. X#define V_COM    3    /* complex number */
  538. X#define V_ADDR    4    /* address of variable value */
  539. X#define V_STR    5    /* address of string */
  540. X#define V_MAT    6    /* address of matrix structure */
  541. X#define    V_LIST    7    /* address of list structure */
  542. X#define    V_ASSOC    8    /* address of association structure */
  543. X#define V_OBJ    9    /* address of object structure */
  544. X#define    V_FILE    10    /* opened file id */
  545. X#define V_MAX    10    /* highest legal value */
  546. X
  547. X#define V_STRLITERAL    0    /* string subtype for literal str */
  548. X#define V_STRALLOC    1    /* string subtype for allocated str */
  549. X
  550. X#define TWOVAL(a,b) ((a) * (V_MAX+1) + (b))    /* for switch of two values */
  551. X
  552. X#define    NULL_VALUE    ((VALUE *) 0)
  553. X
  554. X
  555. Xextern void freevalue MATH_PROTO((VALUE *vp));
  556. Xextern void copyvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  557. Xextern void negvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  558. Xextern void addvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  559. Xextern void subvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  560. Xextern void mulvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  561. Xextern void squarevalue MATH_PROTO((VALUE *vp, VALUE *vres));
  562. Xextern void invertvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  563. Xextern void roundvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  564. Xextern void broundvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  565. Xextern void intvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  566. Xextern void fracvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  567. Xextern void incvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  568. Xextern void decvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  569. Xextern void conjvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  570. Xextern void sqrtvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  571. Xextern void rootvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *v3,
  572. X    VALUE *vres));
  573. Xextern void absvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  574. Xextern void normvalue MATH_PROTO((VALUE *vp, VALUE *vres));
  575. Xextern void shiftvalue MATH_PROTO((VALUE *v1, VALUE *v2, BOOL rightshift,
  576. X    VALUE *vres));
  577. Xextern void scalevalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  578. Xextern void powivalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  579. Xextern void powervalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *v3,
  580. X    VALUE *vres));
  581. Xextern void divvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  582. Xextern void quovalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  583. Xextern void modvalue MATH_PROTO((VALUE *v1, VALUE *v2, VALUE *vres));
  584. Xextern BOOL testvalue MATH_PROTO((VALUE *vp));
  585. Xextern BOOL comparevalue MATH_PROTO((VALUE *v1, VALUE *v2));
  586. Xextern FLAG relvalue MATH_PROTO((VALUE *v1, VALUE *v2));
  587. Xextern HASH hashvalue MATH_PROTO((VALUE *vp));
  588. Xextern void printvalue MATH_PROTO((VALUE *vp, int flags));
  589. X
  590. X
  591. X
  592. X/*
  593. X * Structure of a matrix.
  594. X */
  595. Xstruct matrix {
  596. X    long m_dim;        /* dimension of matrix */
  597. X    long m_size;        /* total number of elements */
  598. X    long m_min[MAXDIM];    /* minimum bound for indices */
  599. X    long m_max[MAXDIM];    /* maximum bound for indices */
  600. X    VALUE m_table[1];    /* actually varying length table */
  601. X};
  602. X
  603. X#define matsize(n) (sizeof(MATRIX) - sizeof(VALUE) + ((n) * sizeof(VALUE)))
  604. X
  605. X
  606. Xextern MATRIX *matadd MATH_PROTO((MATRIX *m1, MATRIX *m2));
  607. Xextern MATRIX *matsub MATH_PROTO((MATRIX *m1, MATRIX *m2));
  608. Xextern MATRIX *matmul MATH_PROTO((MATRIX *m1, MATRIX *m2));
  609. Xextern MATRIX *matneg MATH_PROTO((MATRIX *m));
  610. Xextern MATRIX *matalloc MATH_PROTO((long size));
  611. Xextern MATRIX *matcopy MATH_PROTO((MATRIX *m));
  612. Xextern MATRIX *matsquare MATH_PROTO((MATRIX *m));
  613. Xextern MATRIX *matinv MATH_PROTO((MATRIX *m));
  614. Xextern MATRIX *matscale MATH_PROTO((MATRIX *m, long n));
  615. Xextern MATRIX *matshift MATH_PROTO((MATRIX *m, long n));
  616. Xextern MATRIX *matmulval MATH_PROTO((MATRIX *m, VALUE *vp));
  617. Xextern MATRIX *matpowi MATH_PROTO((MATRIX *m, NUMBER *q));
  618. Xextern MATRIX *matconj MATH_PROTO((MATRIX *m));
  619. Xextern MATRIX *matquoval MATH_PROTO((MATRIX *m, VALUE *vp));
  620. Xextern MATRIX *matmodval MATH_PROTO((MATRIX *m, VALUE *vp));
  621. Xextern MATRIX *matint MATH_PROTO((MATRIX *m));
  622. Xextern MATRIX *matfrac MATH_PROTO((MATRIX *m));
  623. Xextern MATRIX *matround MATH_PROTO((MATRIX *m, long places));
  624. Xextern MATRIX *matbround MATH_PROTO((MATRIX *m, long places));
  625. Xextern MATRIX *mattrans MATH_PROTO((MATRIX *m));
  626. Xextern MATRIX *matcross MATH_PROTO((MATRIX *m1, MATRIX *m2));
  627. Xextern BOOL mattest MATH_PROTO((MATRIX *m));
  628. Xextern BOOL matcmp MATH_PROTO((MATRIX *m1, MATRIX *m2));
  629. Xextern long matsearch MATH_PROTO((MATRIX *m, VALUE *vp, long index));
  630. Xextern long matrsearch MATH_PROTO((MATRIX *m, VALUE *vp, long index));
  631. Xextern HASH mathash MATH_PROTO((MATRIX *m));
  632. Xextern VALUE matdet MATH_PROTO((MATRIX *m));
  633. Xextern VALUE matdot MATH_PROTO((MATRIX *m1, MATRIX *m2));
  634. Xextern void matfill MATH_PROTO((MATRIX *m, VALUE *v1, VALUE *v2));
  635. Xextern void matfree MATH_PROTO((MATRIX *m));
  636. Xextern void matprint MATH_PROTO((MATRIX *m, long max_print));
  637. Xextern VALUE *matindex MATH_PROTO((MATRIX *mp, BOOL create, long dim,
  638. X    VALUE *indices));
  639. X
  640. X
  641. X#if 0
  642. Xextern BOOL matisident MATH_PROTO((MATRIX *m));
  643. X#endif
  644. X
  645. X
  646. X
  647. X/*
  648. X * List definitions.
  649. X * An individual list element.
  650. X */
  651. Xtypedef struct listelem LISTELEM;
  652. Xstruct listelem {
  653. X    LISTELEM *e_next;    /* next element in list (or NULL) */
  654. X    LISTELEM *e_prev;    /* previous element in list (or NULL) */
  655. X    VALUE e_value;        /* value of this element */
  656. X};
  657. X
  658. X
  659. X/*
  660. X * Structure for a list of elements.
  661. X */
  662. Xstruct list {
  663. X    LISTELEM *l_first;    /* first list element (or NULL) */
  664. X    LISTELEM *l_last;    /* last list element (or NULL) */
  665. X    LISTELEM *l_cache;    /* cached list element (or NULL) */
  666. X    long l_cacheindex;    /* index of cached element (or undefined) */
  667. X    long l_count;        /* total number of elements in the list */
  668. X};
  669. X
  670. X
  671. Xextern void insertlistfirst MATH_PROTO((LIST *lp, VALUE *vp));
  672. Xextern void insertlistlast MATH_PROTO((LIST *lp, VALUE *vp));
  673. Xextern void insertlistmiddle MATH_PROTO((LIST *lp, long index, VALUE *vp));
  674. Xextern void removelistfirst MATH_PROTO((LIST *lp, VALUE *vp));
  675. Xextern void removelistlast MATH_PROTO((LIST *lp, VALUE *vp));
  676. Xextern void removelistmiddle MATH_PROTO((LIST *lp, long index, VALUE *vp));
  677. Xextern void listfree MATH_PROTO((LIST *lp));
  678. Xextern void listprint MATH_PROTO((LIST *lp, long max_print));
  679. Xextern long listsearch MATH_PROTO((LIST *lp, VALUE *vp, long index));
  680. Xextern long listrsearch MATH_PROTO((LIST *lp, VALUE *vp, long index));
  681. Xextern HASH listhash MATH_PROTO((LIST *lp));
  682. Xextern BOOL listcmp MATH_PROTO((LIST *lp1, LIST *lp2));
  683. Xextern VALUE *listfindex MATH_PROTO((LIST *lp, long index));
  684. Xextern LIST *listalloc MATH_PROTO((void));
  685. Xextern LIST *listcopy MATH_PROTO((LIST *lp));
  686. X
  687. X
  688. X
  689. X/*
  690. X * Structures for associations.
  691. X * Associations are "indexed" by one or more arbitrary values, and are
  692. X * stored in a hash table with their hash values for quick indexing.
  693. X */
  694. Xtypedef    struct assocelem ASSOCELEM;
  695. Xstruct assocelem {
  696. X    ASSOCELEM *e_next;    /* next element in list (or NULL) */
  697. X    long e_dim;        /* dimension of indexing for this element */
  698. X    HASH e_hash;        /* hash value for this element */
  699. X    VALUE e_value;        /* value of association */
  700. X    VALUE e_indices[1];    /* index values (variable length) */
  701. X};
  702. X
  703. X
  704. Xstruct assoc {
  705. X    long a_count;        /* number of elements in the association */
  706. X    long a_size;        /* current size of association hash table */
  707. X    ASSOCELEM **a_table;    /* current hash table for elements */
  708. X};
  709. X
  710. X
  711. Xextern ASSOC *assocalloc MATH_PROTO((long initsize));
  712. Xextern ASSOC *assoccopy MATH_PROTO((ASSOC *ap));
  713. Xextern void assocfree MATH_PROTO((ASSOC *ap));
  714. Xextern void assocprint MATH_PROTO((ASSOC *ap, long max_print));
  715. Xextern long assocsearch MATH_PROTO((ASSOC *ap, VALUE *vp, long index));
  716. Xextern long assocrsearch MATH_PROTO((ASSOC *ap, VALUE *vp, long index));
  717. Xextern HASH assochash MATH_PROTO((ASSOC *ap));
  718. Xextern BOOL assoccmp MATH_PROTO((ASSOC *ap1, ASSOC *ap2));
  719. Xextern VALUE *assocfindex MATH_PROTO((ASSOC *ap, long index));
  720. Xextern VALUE *associndex MATH_PROTO((ASSOC *ap, BOOL create, long dim,
  721. X    VALUE *indices));
  722. X
  723. X
  724. X/*
  725. X * Object actions.
  726. X */
  727. X#define OBJ_PRINT    0    /* print the value */
  728. X#define OBJ_ONE        1    /* create the multiplicative identity */
  729. X#define OBJ_TEST    2    /* test a value for "zero" */
  730. X#define OBJ_ADD        3    /* add two values */
  731. X#define OBJ_SUB        4    /* subtrace one value from another */
  732. X#define OBJ_NEG        5    /* negate a value */
  733. X#define OBJ_MUL        6    /* multiply two values */
  734. X#define OBJ_DIV        7    /* divide one value by another */
  735. X#define OBJ_INV        8    /* invert a value */
  736. X#define OBJ_ABS        9    /* take absolute value of value */
  737. X#define OBJ_NORM    10    /* take the norm of a value */
  738. X#define OBJ_CONJ    11    /* take the conjugate of a value */
  739. X#define OBJ_POW        12    /* take the power function */
  740. X#define OBJ_SGN        13    /* return the sign of a value */
  741. X#define OBJ_CMP        14    /* compare two values for equality */
  742. X#define OBJ_REL        15    /* compare two values for inequality */
  743. X#define OBJ_QUO        16    /* integer quotient of values */
  744. X#define OBJ_MOD        17    /* remainder of division of values */
  745. X#define OBJ_INT        18    /* integer part of */
  746. X#define OBJ_FRAC    19    /* fractional part of */
  747. X#define OBJ_INC        20    /* increment by one */
  748. X#define OBJ_DEC        21    /* decrement by one */
  749. X#define OBJ_SQUARE    22    /* square value */
  750. X#define OBJ_SCALE    23    /* scale by power of two */
  751. X#define OBJ_SHIFT    24    /* shift left (or right) by number of bits */
  752. X#define OBJ_ROUND    25    /* round to specified decimal places */
  753. X#define OBJ_BROUND    26    /* round to specified binary places */
  754. X#define OBJ_ROOT    27    /* take nth root of value */
  755. X#define OBJ_SQRT    28    /* take square root of value */
  756. X#define OBJ_MAXFUNC    28    /* highest function */
  757. X
  758. X
  759. X/*
  760. X * Definition of an object type.
  761. X * This is actually a varying sized structure.
  762. X */
  763. Xtypedef struct {
  764. X    char *name;            /* name of object */
  765. X    int count;            /* number of elements defined */
  766. X    int actions[OBJ_MAXFUNC+1];    /* function indices for actions */
  767. X    int elements[1];        /* element indexes (MUST BE LAST) */
  768. X} OBJECTACTIONS;
  769. X
  770. X#define objectactionsize(elements) \
  771. X    (sizeof(OBJECTACTIONS) + ((elements) - 1) * sizeof(int))
  772. X
  773. X
  774. X/*
  775. X * Structure of an object.
  776. X * This is actually a varying sized structure.
  777. X * However, there are always at least USUAL_ELEMENTS values in the object.
  778. X */
  779. Xstruct object {
  780. X    OBJECTACTIONS *o_actions;    /* action table for this object */
  781. X    VALUE o_table[USUAL_ELEMENTS];    /* object values (MUST BE LAST) */
  782. X};
  783. X
  784. X#define objectsize(elements) \
  785. X    (sizeof(OBJECT) + ((elements) - USUAL_ELEMENTS) * sizeof(VALUE))
  786. X
  787. X
  788. Xextern OBJECT *objcopy MATH_PROTO((OBJECT *op));
  789. Xextern OBJECT *objalloc MATH_PROTO((long index));
  790. Xextern VALUE objcall MATH_PROTO((int action, VALUE *v1, VALUE *v2, VALUE *v3));
  791. Xextern void objfree MATH_PROTO((OBJECT *op));
  792. Xextern void objuncache MATH_PROTO((void));
  793. Xextern int addelement MATH_PROTO((char *name));
  794. Xextern void defineobject MATH_PROTO((char *name, int indices[], int count));
  795. Xextern int checkobject MATH_PROTO((char *name));
  796. Xextern void showobjfuncs MATH_PROTO((void));
  797. Xextern int findelement MATH_PROTO((char *name));
  798. Xextern int objoffset MATH_PROTO((OBJECT *op, long index));
  799. Xextern HASH objhash MATH_PROTO((OBJECT *op));
  800. X
  801. X#endif
  802. X
  803. X/* END CODE */
  804. SHAR_EOF
  805. chmod 0644 calc2.9.0/value.h || echo "restore of calc2.9.0/value.h fails"
  806. set `wc -c calc2.9.0/value.h`;Sum=$1
  807. if test "$Sum" != "12825"
  808. then echo original size 12825, current size $Sum;fi
  809. echo "x - extracting calc2.9.0/version.c (Text)"
  810. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/version.c &&
  811. X/*
  812. X * Copyright (c) 1993 David I. Bell
  813. X * Permission is granted to use, distribute, or modify this source,
  814. X * provided that this copyright notice remains intact.
  815. X *
  816. X * version - determine the version of calc
  817. X */
  818. X
  819. X#include "calc.h"
  820. X
  821. X#define MAJOR_VER    2    /* major version */
  822. X#define MINOR_VER    9    /* minor version */
  823. X#define PATCH_LEVEL    0    /* patch level */
  824. X
  825. X
  826. Xvoid
  827. Xversion(stream)
  828. X    FILE *stream;    /* stream to write version on */
  829. X{
  830. X    fprintf(stream,
  831. X        "C-style arbitrary precision calculator (version %d.%d.%d)\n", 
  832. X        MAJOR_VER, MINOR_VER, PATCH_LEVEL);
  833. X}
  834. X
  835. X/* END CODE */
  836. SHAR_EOF
  837. chmod 0644 calc2.9.0/version.c || echo "restore of calc2.9.0/version.c fails"
  838. set `wc -c calc2.9.0/version.c`;Sum=$1
  839. if test "$Sum" != "564"
  840. then echo original size 564, current size $Sum;fi
  841. echo "x - extracting calc2.9.0/zfunc.c (Text)"
  842. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/zfunc.c &&
  843. X/*
  844. X * Copyright (c) 1993 David I. Bell
  845. X * Permission is granted to use, distribute, or modify this source,
  846. X * provided that this copyright notice remains intact.
  847. X *
  848. X * Extended precision integral arithmetic non-primitive routines
  849. X */
  850. X
  851. X#include "zmath.h"
  852. X
  853. Xstatic ZVALUE primeprod;        /* product of primes under 100 */
  854. XZVALUE _tenpowers_[32];            /* table of 10^2^n */
  855. X
  856. X
  857. X/*
  858. X * Compute the factorial of a number.
  859. X */
  860. Xvoid
  861. Xzfact(z, dest)
  862. X    ZVALUE z, *dest;
  863. X{
  864. X    long ptwo;        /* count of powers of two */
  865. X    long n;            /* current multiplication value */
  866. X    long m;            /* reduced multiplication value */
  867. X    long mul;        /* collected value to multiply by */
  868. X    ZVALUE res, temp;
  869. X
  870. X    if (zisneg(z))
  871. X        math_error("Negative argument for factorial");
  872. X    if (zisbig(z))
  873. X        math_error("Very large factorial");
  874. X    n = (zistiny(z) ? z1tol(z) : z2tol(z));
  875. X    ptwo = 0;
  876. X    mul = 1;
  877. X    res = _one_;
  878. X    /*
  879. X     * Multiply numbers together, but squeeze out all powers of two.
  880. X     * We will put them back in at the end.  Also collect multiple
  881. X     * numbers together until there is a risk of overflow.
  882. X     */
  883. X    for (; n > 1; n--) {
  884. X        for (m = n; ((m & 0x1) == 0); m >>= 1)
  885. X            ptwo++;
  886. X        mul *= m;
  887. X        if (mul < BASE1/2)
  888. X            continue;
  889. X        zmuli(res, mul, &temp);
  890. X        zfree(res);
  891. X        res = temp;
  892. X        mul = 1;
  893. X    }
  894. X    /*
  895. X     * Multiply by the remaining value, then scale result by
  896. X     * the proper power of two.
  897. X     */
  898. X    if (mul > 1) {
  899. X        zmuli(res, mul, &temp);
  900. X        zfree(res);
  901. X        res = temp;
  902. X    }
  903. X    zshift(res, ptwo, &temp);
  904. X    zfree(res);
  905. X    *dest = temp;
  906. X}
  907. X
  908. X
  909. X/*
  910. X * Compute the product of the primes up to the specified number.
  911. X */
  912. Xvoid
  913. Xzpfact(z, dest)
  914. X    ZVALUE z, *dest;
  915. X{
  916. X    long n;            /* limiting number to multiply by */
  917. X    long p;            /* current prime */
  918. X    long i;            /* test value */
  919. X    long mul;        /* collected value to multiply by */
  920. X    ZVALUE res, temp;
  921. X
  922. X    if (zisneg(z))
  923. X        math_error("Negative argument for factorial");
  924. X    if (zisbig(z))
  925. X        math_error("Very large factorial");
  926. X    n = (zistiny(z) ? z1tol(z) : z2tol(z));
  927. X    /*
  928. X     * Multiply by the primes in order, collecting multiple numbers
  929. X     * together until there is a chance of overflow.
  930. X     */
  931. X    mul = 1 + (n > 1);
  932. X    res = _one_;
  933. X    for (p = 3; p <= n; p += 2) {
  934. X        for (i = 3; (i * i) <= p; i += 2) {
  935. X            if ((p % i) == 0)
  936. X                goto next;
  937. X        }
  938. X        mul *= p;
  939. X        if (mul < BASE1/2)
  940. X            continue;
  941. X        zmuli(res, mul, &temp);
  942. X        zfree(res);
  943. X        res = temp;
  944. X        mul = 1;
  945. Xnext: ;
  946. X    }
  947. X    /*
  948. X     * Multiply by the final value if any.
  949. X     */
  950. X    if (mul > 1) {
  951. X        zmuli(res, mul, &temp);
  952. X        zfree(res);
  953. X        res = temp;
  954. X    }
  955. X    *dest = res;
  956. X}
  957. X
  958. X
  959. X/*
  960. X * Compute the least common multiple of all the numbers up to the
  961. X * specified number.
  962. X */
  963. Xvoid
  964. Xzlcmfact(z, dest)
  965. X    ZVALUE z, *dest;
  966. X{
  967. X    long n;            /* limiting number to multiply by */
  968. X    long p;            /* current prime */
  969. X    long pp;        /* power of prime */
  970. X    long i;            /* test value */
  971. X    ZVALUE res, temp;
  972. X
  973. X    if (zisneg(z) || ziszero(z))
  974. X        math_error("Non-positive argument for lcmfact");
  975. X    if (zisbig(z))
  976. X        math_error("Very large lcmfact");
  977. X    n = (zistiny(z) ? z1tol(z) : z2tol(z));
  978. X    /*
  979. X     * Multiply by powers of the necessary odd primes in order.
  980. X     * The power for each prime is the highest one which is not
  981. X     * more than the specified number.
  982. X     */
  983. X    res = _one_;
  984. X    for (p = 3; p <= n; p += 2) {
  985. X        for (i = 3; (i * i) <= p; i += 2) {
  986. X            if ((p % i) == 0)
  987. X                goto next;
  988. X        }
  989. X        i = p;
  990. X        while (i <= n) {
  991. X            pp = i;
  992. X            i *= p;
  993. X        }
  994. X        zmuli(res, pp, &temp);
  995. X        zfree(res);
  996. X        res = temp;
  997. Xnext: ;
  998. X    }
  999. X    /*
  1000. X     * Finish by scaling by the necessary power of two.
  1001. X     */
  1002. X    zshift(res, zhighbit(z), dest);
  1003. X    zfree(res);
  1004. X}
  1005. X
  1006. X
  1007. X/*
  1008. X * Compute the permutation function  M! / (M - N)!.
  1009. X */
  1010. Xvoid
  1011. Xzperm(z1, z2, res)
  1012. X    ZVALUE z1, z2, *res;
  1013. X{
  1014. X    long count;
  1015. X    ZVALUE cur, tmp, ans;
  1016. X
  1017. X    if (zisneg(z1) || zisneg(z2))
  1018. X        math_error("Negative argument for permutation");
  1019. X    if (zrel(z1, z2) < 0)
  1020. X        math_error("Second arg larger than first in permutation");
  1021. X    if (zisbig(z2))
  1022. X        math_error("Very large permutation");
  1023. X    count = (zistiny(z2) ? z1tol(z2) : z2tol(z2));
  1024. X    zcopy(z1, &ans);
  1025. X    zsub(z1, _one_, &cur);
  1026. X    while (--count > 0) {
  1027. X        zmul(ans, cur, &tmp);
  1028. X        zfree(ans);
  1029. X        ans = tmp;
  1030. X        zsub(cur, _one_, &tmp);
  1031. X        zfree(cur);
  1032. X        cur = tmp;
  1033. X    }
  1034. X    zfree(cur);
  1035. X    *res = ans;
  1036. X}
  1037. X
  1038. X
  1039. X/*
  1040. X * Compute the combinatorial function  M! / ( N! * (M - N)! ).
  1041. X */
  1042. Xvoid
  1043. Xzcomb(z1, z2, res)
  1044. X    ZVALUE z1, z2, *res;
  1045. X{
  1046. X    ZVALUE ans;
  1047. X    ZVALUE mul, div, temp;
  1048. X    FULL count, i;
  1049. X    HALF dh[2];
  1050. X
  1051. X    if (zisneg(z1) || zisneg(z2))
  1052. X        math_error("Negative argument for combinatorial");
  1053. X    zsub(z1, z2, &temp);
  1054. X    if (zisneg(temp)) {
  1055. X        zfree(temp);
  1056. X        math_error("Second arg larger than first for combinatorial");
  1057. X    }
  1058. X    if (zisbig(z2) && zisbig(temp)) {
  1059. X        zfree(temp);
  1060. X        math_error("Very large combinatorial");
  1061. X    }
  1062. X    count = (zistiny(z2) ? z1tol(z2) : z2tol(z2));
  1063. X    i = (zistiny(temp) ? z1tol(temp) : z2tol(temp));
  1064. X    if (zisbig(z2) || (!zisbig(temp) && (i < count)))
  1065. X        count = i;
  1066. X    zfree(temp);
  1067. X    mul = z1;
  1068. X    div.sign = 0;
  1069. X    div.v = dh;
  1070. X    ans = _one_;
  1071. X    for (i = 1; i <= count; i++) {
  1072. X        dh[0] = i & BASE1;
  1073. X        dh[1] = i / BASE;
  1074. X        div.len = 1 + (dh[1] != 0);
  1075. X        zmul(ans, mul, &temp);
  1076. X        zfree(ans);
  1077. X        zquo(temp, div, &ans);
  1078. X        zfree(temp);
  1079. X        zsub(mul, _one_, &temp);
  1080. X        if (mul.v != z1.v)
  1081. X            zfree(mul);
  1082. X        mul = temp;
  1083. X    }
  1084. X    if (mul.v != z1.v)
  1085. X        zfree(mul);
  1086. X    *res = ans;
  1087. X}
  1088. X
  1089. X
  1090. X/*
  1091. X * Perform a probabilistic primality test (algorithm P in Knuth).
  1092. X * Returns FALSE if definitely not prime, or TRUE if probably prime.
  1093. X * Count determines how many times to check for primality.
  1094. X * The chance of a non-prime passing this test is less than (1/4)^count.
  1095. X * For example, a count of 100 fails for only 1 in 10^60 numbers.
  1096. X */
  1097. XBOOL
  1098. Xzprimetest(z, count)
  1099. X    ZVALUE z;        /* number to test for primeness */
  1100. X    long count;
  1101. X{
  1102. X    long ij, ik, ix;
  1103. X    ZVALUE zm1, z1, z2, z3, ztmp;
  1104. X    HALF val[2];
  1105. X
  1106. X    z.sign = 0;
  1107. X    if (ziseven(z))        /* if even, not prime if not 2 */
  1108. X        return (zistwo(z) != 0);
  1109. X    /*
  1110. X     * See if the number is small, and is either a small prime,
  1111. X     * or is divisible by a small prime.
  1112. X     */
  1113. X    if (zistiny(z) && (*z.v <= (HALF)(101*101-1))) {
  1114. X        ix = *z.v;
  1115. X        for (ik = 3; (ik <= 97) && ((ik * ik) <= ix); ik += 2)
  1116. X            if ((ix % ik) == 0)
  1117. X                return FALSE;
  1118. X        return TRUE;
  1119. X    }
  1120. X    /*
  1121. X     * See if the number is divisible by one of the primes 3, 5,
  1122. X     * 7, 11, or 13.  This is a very easy check.
  1123. X     */
  1124. X    ij = zmodi(z, 15015L);
  1125. X    if (!(ij % 3) || !(ij % 5) || !(ij % 7) || !(ij % 11) || !(ij % 13))
  1126. X        return FALSE;
  1127. X    /*
  1128. X     * Check the gcd of the number and the product of more of the first
  1129. X     * few odd primes.  We must build the prime product on the first call.
  1130. X     */
  1131. X    ztmp.sign = 0;
  1132. X    ztmp.len = 1;
  1133. X    ztmp.v = val;
  1134. X    if (primeprod.len == 0) {
  1135. X        val[0] = 101;
  1136. X        zpfact(ztmp, &primeprod);
  1137. X    }
  1138. X    zgcd(z, primeprod, &z1);
  1139. X    if (!zisunit(z1)) {
  1140. X        zfree(z1);
  1141. X        return FALSE;
  1142. X    }
  1143. X    zfree(z1);
  1144. X    /*
  1145. X     * Not divisible by a small prime, so onward with the real test.
  1146. X     * Make sure the count is limited by the number of odd numbers between
  1147. X     * three and the number being tested.
  1148. X     */
  1149. X    ix = ((zistiny(z) ? z1tol(z) : z2tol(z) - 3) / 2);
  1150. X    if (count > ix) count = ix;
  1151. X    zsub(z, _one_, &zm1);
  1152. X    ik = zlowbit(zm1);
  1153. X    zshift(zm1, -ik, &z1);
  1154. X    /*
  1155. X     * Loop over various "random" numbers, testing each one.
  1156. X     * These numbers are the odd numbers starting from three.
  1157. X     */
  1158. X    for (ix = 0; ix < count; ix++) {
  1159. X        val[0] = (ix * 2) + 3;
  1160. X        ij = 0;
  1161. X        zpowermod(ztmp, z1, z, &z3);
  1162. X        for (;;) {
  1163. X            if (zisone(z3)) {
  1164. X                if (ij)    /* number is definitely not prime */
  1165. X                    goto notprime;
  1166. X                break;
  1167. X            }
  1168. X            if (zcmp(z3, zm1) == 0)
  1169. X                break;
  1170. X            if (++ij >= ik)
  1171. X                goto notprime;    /* number is definitely not prime */
  1172. X            zsquare(z3, &z2);
  1173. X            zfree(z3);
  1174. X            zmod(z2, z, &z3);
  1175. X            zfree(z2);
  1176. X        }
  1177. X        zfree(z3);
  1178. X    }
  1179. X    zfree(zm1);
  1180. X    zfree(z1);
  1181. X    return TRUE;    /* number might be prime */
  1182. X
  1183. Xnotprime:
  1184. X    zfree(z3);
  1185. X    zfree(zm1);
  1186. X    zfree(z1);
  1187. X    return FALSE;
  1188. X}
  1189. X
  1190. X
  1191. X/*
  1192. X * Compute the Jacobi function (p / q) for odd q.
  1193. X * If q is prime then the result is:
  1194. X *    1 if p == x^2 (mod q) for some x.
  1195. X *    -1 otherwise.
  1196. X * If q is not prime, then the result is not meaningful if it is 1.
  1197. X * This function returns 0 if q is even or q < 0.
  1198. X */
  1199. XFLAG
  1200. Xzjacobi(z1, z2)
  1201. X    ZVALUE z1, z2;
  1202. X{
  1203. X    ZVALUE p, q, tmp;
  1204. X    long lowbit;
  1205. X    int val;
  1206. X
  1207. X    if (ziseven(z2) || zisneg(z2))
  1208. X        return 0;
  1209. X    val = 1;
  1210. X    if (ziszero(z1) || zisone(z1))
  1211. X        return val;
  1212. X    if (zisunit(z1)) {
  1213. X        if ((*z2.v - 1) & 0x2)
  1214. X            val = -val;
  1215. X        return val;
  1216. X    }
  1217. X    zcopy(z1, &p);
  1218. X    zcopy(z2, &q);
  1219. X    for (;;) {
  1220. X        zmod(p, q, &tmp);
  1221. X        zfree(p);
  1222. X        p = tmp;
  1223. X        if (ziszero(p)) {
  1224. X            zfree(p);
  1225. X            p = _one_;
  1226. X        }
  1227. X        if (ziseven(p)) {
  1228. X            lowbit = zlowbit(p);
  1229. X            zshift(p, -lowbit, &tmp);
  1230. X            zfree(p);
  1231. X            p = tmp;
  1232. X            if ((lowbit & 1) && (((*q.v & 0x7) == 3) || ((*q.v & 0x7) == 5)))
  1233. X                val = -val;
  1234. X        }
  1235. X        if (zisunit(p)) {
  1236. X            zfree(p);
  1237. X            zfree(q);
  1238. X            return val;
  1239. X        }
  1240. X        if ((*p.v & *q.v & 0x3) == 3)
  1241. X            val = -val;
  1242. X        tmp = q;
  1243. X        q = p;
  1244. X        p = tmp;
  1245. X    }
  1246. X}
  1247. X
  1248. X
  1249. X/*
  1250. X * Return the Fibonacci number F(n).
  1251. X * This is evaluated by recursively using the formulas:
  1252. X *    F(2N+1) = F(N+1)^2 + F(N)^2
  1253. X * and
  1254. X *    F(2N) = F(N+1)^2 - F(N-1)^2
  1255. X */
  1256. Xvoid
  1257. Xzfib(z, res)
  1258. X    ZVALUE z, *res;
  1259. X{
  1260. X    unsigned long i;
  1261. X    long n;
  1262. X    int sign;
  1263. X    ZVALUE fnm1, fn, fnp1;        /* consecutive fibonacci values */
  1264. X    ZVALUE t1, t2, t3;
  1265. X
  1266. X    if (zisbig(z))
  1267. X        math_error("Very large Fibonacci number");
  1268. X    n = (zistiny(z) ? z1tol(z) : z2tol(z));
  1269. X    if (n == 0) {
  1270. X        *res = _zero_;
  1271. X        return;
  1272. X    }
  1273. X    sign = z.sign && ((n & 0x1) == 0);
  1274. X    if (n <= 2) {
  1275. X        *res = _one_;
  1276. X        res->sign = (BOOL)sign;
  1277. X        return;
  1278. X    }
  1279. X    i = TOPFULL;
  1280. X    while ((i & n) == 0)
  1281. X        i >>= 1L;
  1282. X    i >>= 1L;
  1283. X    fnm1 = _zero_;
  1284. X    fn = _one_;
  1285. X    fnp1 = _one_;
  1286. X    while (i) {
  1287. X        zsquare(fnm1, &t1);
  1288. X        zsquare(fn, &t2);
  1289. X        zsquare(fnp1, &t3);
  1290. X        zfree(fnm1);
  1291. X        zfree(fn);
  1292. X        zfree(fnp1);
  1293. X        zadd(t2, t3, &fnp1);
  1294. X        zsub(t3, t1, &fn);
  1295. X        zfree(t1);
  1296. X        zfree(t2);
  1297. X        zfree(t3);
  1298. X        if (i & n) {
  1299. X            fnm1 = fn;
  1300. X            fn = fnp1;
  1301. X            zadd(fnm1, fn, &fnp1);
  1302. X        } else
  1303. X            zsub(fnp1, fn, &fnm1);
  1304. X        i >>= 1L;
  1305. X    }
  1306. X    zfree(fnm1);
  1307. X    zfree(fnp1);
  1308. X    *res = fn;
  1309. X    res->sign = (BOOL)sign;
  1310. X}
  1311. X
  1312. X
  1313. X/*
  1314. X * Compute the result of raising one number to the power of another
  1315. X * The second number is assumed to be non-negative.
  1316. X * It cannot be too large except for trivial cases.
  1317. X */
  1318. Xvoid
  1319. Xzpowi(z1, z2, res)
  1320. X    ZVALUE z1, z2, *res;
  1321. X{
  1322. X    int sign;        /* final sign of number */
  1323. X    unsigned long power;    /* power to raise to */
  1324. X    unsigned long bit;    /* current bit value */
  1325. X    long twos;        /* count of times 2 is in result */
  1326. X    ZVALUE ans, temp;
  1327. X
  1328. X    sign = (z1.sign && zisodd(z2));
  1329. X    z1.sign = 0;
  1330. X    z2.sign = 0;
  1331. X    if (ziszero(z2)) {    /* number raised to power 0 */
  1332. X        if (ziszero(z1))
  1333. X            math_error("Zero raised to zero power");
  1334. X        *res = _one_;
  1335. X        return;
  1336. X    }
  1337. X    if (zisleone(z1)) {    /* 0, 1, or -1 raised to a power */
  1338. X        ans = _one_;
  1339. X        ans.sign = (BOOL)sign;
  1340. X        if (*z1.v == 0)
  1341. X            ans = _zero_;
  1342. X        *res = ans;
  1343. X        return;
  1344. X    }
  1345. X    if (zisbig(z2))
  1346. X        math_error("Raising to very large power");
  1347. X    power = (zistiny(z2) ? z1tol(z2) : z2tol(z2));
  1348. X    if (zistwo(z1)) {    /* two raised to a power */
  1349. X        zbitvalue((long) power, res);
  1350. X        return;
  1351. X    }
  1352. X    /*
  1353. X     * See if this is a power of ten
  1354. X     */
  1355. X    if (zistiny(z1) && (*z1.v == 10)) {
  1356. X        ztenpow((long) power, res);
  1357. X        res->sign = (BOOL)sign;
  1358. X        return;
  1359. X    }
  1360. X    /*
  1361. X     * Handle low powers specially
  1362. X     */
  1363. X    if (power <= 4) {
  1364. X        switch ((int) power) {
  1365. X            case 1:
  1366. X                ans.len = z1.len;
  1367. X                ans.v = alloc(ans.len);
  1368. X                zcopyval(z1, ans);
  1369. X                ans.sign = (BOOL)sign;
  1370. X                *res = ans;
  1371. X                return;
  1372. X            case 2:
  1373. X                zsquare(z1, res);
  1374. X                return;
  1375. X            case 3:
  1376. X                zsquare(z1, &temp);
  1377. X                zmul(z1, temp, res);
  1378. X                zfree(temp);
  1379. X                res->sign = (BOOL)sign;
  1380. X                return;
  1381. X            case 4:
  1382. X                zsquare(z1, &temp);
  1383. X                zsquare(temp, res);
  1384. X                zfree(temp);
  1385. X                return;
  1386. X        }
  1387. X    }
  1388. X    /*
  1389. X     * Shift out all powers of twos so the multiplies are smaller.
  1390. X     * We will shift back the right amount when done.
  1391. X     */
  1392. X    twos = 0;
  1393. X    if (ziseven(z1)) {
  1394. X        twos = zlowbit(z1);
  1395. X        ans.v = alloc(z1.len);
  1396. X        ans.len = z1.len;
  1397. X        zcopyval(z1, ans);
  1398. X        zshiftr(ans, twos);
  1399. X        ztrim(&ans);
  1400. X        z1 = ans;
  1401. X        twos *= power;
  1402. X    }
  1403. X    /*
  1404. X     * Compute the power by squaring and multiplying.
  1405. X     * This uses the left to right method of power raising.
  1406. X     */
  1407. X    bit = TOPFULL;
  1408. X    while ((bit & power) == 0)
  1409. X        bit >>= 1L;
  1410. X    bit >>= 1L;
  1411. X    zsquare(z1, &ans);
  1412. X    if (bit & power) {
  1413. X        zmul(ans, z1, &temp);
  1414. X        zfree(ans);
  1415. X        ans = temp;
  1416. X    }
  1417. X    bit >>= 1L;
  1418. X    while (bit) {
  1419. X        zsquare(ans, &temp);
  1420. X        zfree(ans);
  1421. X        ans = temp;
  1422. X        if (bit & power) {
  1423. X            zmul(ans, z1, &temp);
  1424. X            zfree(ans);
  1425. X            ans = temp;
  1426. X        }
  1427. X        bit >>= 1L;
  1428. X    }
  1429. X    /*
  1430. X     * Scale back up by proper power of two
  1431. X     */
  1432. X    if (twos) {
  1433. X        zshift(ans, twos, &temp);
  1434. X        zfree(ans);
  1435. X        ans = temp;
  1436. X        zfree(z1);
  1437. X    }
  1438. X    ans.sign = (BOOL)sign;
  1439. X    *res = ans;
  1440. X}
  1441. X
  1442. X
  1443. X/*
  1444. X * Compute ten to the specified power
  1445. X * This saves some work since the squares of ten are saved.
  1446. X */
  1447. Xvoid
  1448. Xztenpow(power, res)
  1449. X    long power;
  1450. X    ZVALUE *res;
  1451. X{
  1452. X    long i;
  1453. X    ZVALUE ans;
  1454. X    ZVALUE temp;
  1455. X
  1456. X    if (power <= 0) {
  1457. X        *res = _one_;
  1458. X        return;
  1459. X    }
  1460. X    ans = _one_;
  1461. X    _tenpowers_[0] = _ten_;
  1462. X    for (i = 0; power; i++) {
  1463. X        if (_tenpowers_[i].len == 0)
  1464. X            zsquare(_tenpowers_[i-1], &_tenpowers_[i]);
  1465. X        if (power & 0x1) {
  1466. X            zmul(ans, _tenpowers_[i], &temp);
  1467. X            zfree(ans);
  1468. X            ans = temp;
  1469. X        }
  1470. X        power /= 2;
  1471. X    }
  1472. X    *res = ans;
  1473. X}
  1474. X
  1475. X
  1476. X/*
  1477. X * Calculate modular inverse suppressing unnecessary divisions.
  1478. X * This is based on the Euclidian algorithm for large numbers.
  1479. X * (Algorithm X from Knuth Vol 2, section 4.5.2. and exercise 17)
  1480. X * Returns TRUE if there is no solution because the numbers
  1481. X * are not relatively prime.
  1482. X */
  1483. XBOOL
  1484. Xzmodinv(u, v, res)
  1485. X    ZVALUE u, v;
  1486. X    ZVALUE *res;
  1487. X{
  1488. X    FULL    q1, q2, ui3, vi3, uh, vh, A, B, C, D, T;
  1489. X    ZVALUE    u2, u3, v2, v3, qz, tmp1, tmp2, tmp3;
  1490. X
  1491. X    if (zisneg(u) || zisneg(v) || (zrel(u, v) >= 0))
  1492. X        zmod(u, v, &v3);
  1493. X    else
  1494. X        zcopy(u, &v3);
  1495. X    zcopy(v, &u3);
  1496. X    u2 = _zero_;
  1497. X    v2 = _one_;
  1498. X
  1499. X    /*
  1500. X     * Loop here while the size of the numbers remain above
  1501. X     * the size of a FULL.  Throughout this loop u3 >= v3.
  1502. X     */
  1503. X    while ((u3.len > 1) && !ziszero(v3)) {
  1504. X        uh = (((FULL) u3.v[u3.len - 1]) << BASEB) + u3.v[u3.len - 2];
  1505. X        vh = 0;
  1506. X        if ((v3.len + 1) >= u3.len)
  1507. X            vh = v3.v[v3.len - 1];
  1508. X        if (v3.len == u3.len)
  1509. X            vh = (vh << BASEB) + v3.v[v3.len - 2];
  1510. X        A = 1;
  1511. X        B = 0;
  1512. X        C = 0;
  1513. X        D = 1;
  1514. X
  1515. X        /*
  1516. X         * Calculate successive quotients of the continued fraction
  1517. X         * expansion using only single precision arithmetic until
  1518. X         * greater precision is required.
  1519. X         */
  1520. X        while ((vh + C) && (vh + D)) {
  1521. X            q1 = (uh + A) / (vh + C);
  1522. X            q2 = (uh + B) / (vh + D);
  1523. X            if (q1 != q2)
  1524. X                break;
  1525. X            T = A - q1 * C;
  1526. X            A = C;
  1527. X            C = T;
  1528. X            T = B - q1 * D;
  1529. X            B = D;
  1530. X            D = T;
  1531. X            T = uh - q1 * vh;
  1532. X            uh = vh;
  1533. X            vh = T;
  1534. X        }
  1535. X    
  1536. X        /*
  1537. X         * If B is zero, then we made no progress because
  1538. X         * the calculation requires a very large quotient.
  1539. X         * So we must do this step of the calculation in
  1540. X         * full precision
  1541. X         */
  1542. X        if (B == 0) {
  1543. X            zquo(u3, v3, &qz);
  1544. X            zmul(qz, v2, &tmp1);
  1545. X            zsub(u2, tmp1, &tmp2);
  1546. X            zfree(tmp1);
  1547. X            zfree(u2);
  1548. X            u2 = v2;
  1549. X            v2 = tmp2;
  1550. X            zmul(qz, v3, &tmp1);
  1551. X            zsub(u3, tmp1, &tmp2);
  1552. X            zfree(tmp1);
  1553. X            zfree(u3);
  1554. X            u3 = v3;
  1555. X            v3 = tmp2;
  1556. X            zfree(qz);
  1557. X            continue;
  1558. X        }
  1559. X        /*
  1560. X         * Apply the calculated A,B,C,D numbers to the current
  1561. X         * values to update them as if the full precision
  1562. X         * calculations had been carried out.
  1563. X         */
  1564. X        zmuli(u2, (long) A, &tmp1);
  1565. X        zmuli(v2, (long) B, &tmp2);
  1566. X        zadd(tmp1, tmp2, &tmp3);
  1567. X        zfree(tmp1);
  1568. X        zfree(tmp2);
  1569. X        zmuli(u2, (long) C, &tmp1);
  1570. X        zmuli(v2, (long) D, &tmp2);
  1571. X        zfree(u2);
  1572. X        zfree(v2);
  1573. X        u2 = tmp3;
  1574. X        zadd(tmp1, tmp2, &v2);
  1575. X        zfree(tmp1);
  1576. X        zfree(tmp2);
  1577. X        zmuli(u3, (long) A, &tmp1);
  1578. X        zmuli(v3, (long) B, &tmp2);
  1579. X        zadd(tmp1, tmp2, &tmp3);
  1580. X        zfree(tmp1);
  1581. X        zfree(tmp2);
  1582. X        zmuli(u3, (long) C, &tmp1);
  1583. X        zmuli(v3, (long) D, &tmp2);
  1584. X        zfree(u3);
  1585. X        zfree(v3);
  1586. X        u3 = tmp3;
  1587. X        zadd(tmp1, tmp2, &v3);
  1588. X        zfree(tmp1);
  1589. X        zfree(tmp2);
  1590. X    }
  1591. X
  1592. X    /*
  1593. X     * Here when the remaining numbers become single precision in size.
  1594. X     * Finish the procedure using single precision calculations.
  1595. X     */
  1596. X    if (ziszero(v3) && !zisone(u3)) {
  1597. X        zfree(u3);
  1598. X        zfree(v3);
  1599. X        zfree(u2);
  1600. X        zfree(v2);
  1601. X        return TRUE;
  1602. X    }
  1603. X    ui3 = (zistiny(u3) ? z1tol(u3) : z2tol(u3));
  1604. X    vi3 = (zistiny(v3) ? z1tol(v3) : z2tol(v3));
  1605. X    zfree(u3);
  1606. X    zfree(v3);
  1607. X    while (vi3) {
  1608. X        q1 = ui3 / vi3;
  1609. X        zmuli(v2, (long) q1, &tmp1);
  1610. X        zsub(u2, tmp1, &tmp2);
  1611. X        zfree(tmp1);
  1612. X        zfree(u2);
  1613. X        u2 = v2;
  1614. X        v2 = tmp2;
  1615. X        q2 = ui3 - q1 * vi3;
  1616. X        ui3 = vi3;
  1617. X        vi3 = q2;
  1618. X    }
  1619. X    zfree(v2);
  1620. X    if (ui3 != 1) {
  1621. X        zfree(u2);
  1622. X        return TRUE;
  1623. X    }
  1624. X    if (zisneg(u2)) {
  1625. X        zadd(v, u2, res);
  1626. X        zfree(u2);
  1627. X        return FALSE;
  1628. X    }
  1629. X    *res = u2;
  1630. X    return FALSE;
  1631. X}
  1632. X
  1633. X
  1634. X#if 0
  1635. X/*
  1636. X * Approximate the quotient of two integers by another set of smaller
  1637. X * integers.  This uses continued fractions to determine the smaller set.
  1638. X */
  1639. Xvoid
  1640. Xzapprox(z1, z2, res1, res2)
  1641. X    ZVALUE z1, z2, *res1, *res2;
  1642. X{
  1643. X    int sign;
  1644. X    ZVALUE u1, v1, u3, v3, q, t1, t2, t3;
  1645. X
  1646. X    sign = ((z1.sign != 0) ^ (z2.sign != 0));
  1647. X    z1.sign = 0;
  1648. X    z2.sign = 0;
  1649. X    v3 = z2;
  1650. X    u3 = z1;
  1651. X    u1 = _one_;
  1652. X    v1 = _zero_;
  1653. X    while (!ziszero(v3)) {
  1654. X        zdiv(u3, v3, &q, &t1);
  1655. X        zmul(v1, q, &t2);
  1656. X        zsub(u1, t2, &t3);
  1657. X        zfree(q);
  1658. X        zfree(t2);
  1659. X        zfree(u1);
  1660. X        if ((u3.v != z1.v) && (u3.v != z2.v))
  1661. X            zfree(u3);
  1662. X        u1 = v1;
  1663. X        u3 = v3;
  1664. X        v1 = t3;
  1665. X        v3 = t1;
  1666. X    }
  1667. X    if (!zisunit(u3))
  1668. X        math_error("Non-relativly prime numbers for approx");
  1669. X    if ((u3.v != z1.v) && (u3.v != z2.v))
  1670. X        zfree(u3);
  1671. X    if ((v3.v != z1.v) && (v3.v != z2.v))
  1672. X        zfree(v3);
  1673. X    zfree(v1);
  1674. X    zmul(u1, z1, &t1);
  1675. X    zsub(t1, _one_, &t2);
  1676. X    zfree(t1);
  1677. X    zquo(t2, z2, &t1);
  1678. X    zfree(t2);
  1679. X    u1.sign = (BOOL)sign;
  1680. X    t1.sign = 0;
  1681. X    *res1 = t1;
  1682. X    *res2 = u1;
  1683. X}
  1684. X#endif
  1685. X
  1686. X
  1687. X/*
  1688. X * Binary gcd algorithm
  1689. X * This algorithm taken from Knuth
  1690. X */
  1691. Xvoid
  1692. Xzgcd(z1, z2, res)
  1693. X    ZVALUE z1, z2, *res;
  1694. X{
  1695. X    ZVALUE u, v, t;
  1696. X    register long j, k, olen, mask;
  1697. X    register HALF h;
  1698. X    HALF *oldv1, *oldv2;
  1699. X
  1700. X    z1.sign = 0;
  1701. X    z2.sign = 0;
  1702. X    if (ziszero(z1)) {
  1703. X        zcopy(z2, res);
  1704. X        return;
  1705. X    }
  1706. X    if (ziszero(z2)) {
  1707. X        zcopy(z1, res);
  1708. X        return;
  1709. X    }
  1710. X    if (zisunit(z1) || zisunit(z2)) {
  1711. X        *res = _one_;
  1712. X        return;
  1713. X    }
  1714. X    /*
  1715. X     * First see if one number is very much larger than the other.
  1716. X     * If so, then divide as necessary to get the numbers near each other.
  1717. X     */
  1718. X    oldv1 = z1.v;
  1719. X    oldv2 = z2.v;
  1720. X    if (z1.len < z2.len) {
  1721. X        t = z1;
  1722. X        z1 = z2;
  1723. X        z2 = t;
  1724. X    }
  1725. X    while ((z1.len > (z2.len + 5)) && !ziszero(z2)) {
  1726. X        zmod(z1, z2, &t);
  1727. X        if ((z1.v != oldv1) && (z1.v != oldv2))
  1728. X            zfree(z1);
  1729. X        z1 = z2;
  1730. X        z2 = t;
  1731. X    }
  1732. X    /*
  1733. X     * Ok, now do the binary method proper
  1734. X     */
  1735. X    u.len = z1.len;
  1736. X    v.len = z2.len;
  1737. X    u.sign = 0;
  1738. X    v.sign = 0;
  1739. X    if (!ztest(z1)) {
  1740. X        v.v = alloc(v.len);
  1741. X        zcopyval(z2, v);
  1742. X        *res = v;
  1743. X        goto done;
  1744. X    }
  1745. X    if (!ztest(z2)) {
  1746. X        u.v = alloc(u.len);
  1747. X        zcopyval(z1, u);
  1748. X        *res = u;
  1749. X        goto done;
  1750. X    }
  1751. X    u.v = alloc(u.len);
  1752. X    v.v = alloc(v.len);
  1753. X    zcopyval(z1, u);
  1754. X    zcopyval(z2, v);
  1755. X    k = 0;
  1756. X    while (u.v[k] == 0 && v.v[k] == 0)
  1757. X        ++k;
  1758. X    mask = 01;
  1759. X    h = u.v[k] | v.v[k];
  1760. X    k *= BASEB;
  1761. X    while (!(h & mask)) {
  1762. X        mask <<= 1;
  1763. X        k++;
  1764. X    }
  1765. X    zshiftr(u, k);
  1766. X    zshiftr(v, k);
  1767. X    ztrim(&u);
  1768. X    ztrim(&v);
  1769. X    if (zisodd(u)) {
  1770. X        t.v = alloc(v.len);
  1771. X        t.len = v.len;
  1772. X        zcopyval(v, t);
  1773. X        t.sign = !v.sign;
  1774. X    } else {
  1775. X        t.v = alloc(u.len);
  1776. X        t.len = u.len;
  1777. X        zcopyval(u, t);
  1778. X        t.sign = u.sign;
  1779. X    }
  1780. X    while (ztest(t)) {
  1781. X        j = 0;
  1782. X        while (!t.v[j])
  1783. X            ++j;
  1784. X        mask = 01;
  1785. X        h = t.v[j];
  1786. X        j *= BASEB;
  1787. X        while (!(h & mask)) {
  1788. X            mask <<= 1;
  1789. X            j++;
  1790. X        }
  1791. X        zshiftr(t, j);
  1792. X        ztrim(&t);
  1793. X        if (ztest(t) > 0) {
  1794. X            zfree(u);
  1795. X            u = t;
  1796. X        } else {
  1797. X            zfree(v);
  1798. X            v = t;
  1799. X            v.sign = !t.sign;
  1800. X        }
  1801. X        zsub(u, v, &t);
  1802. X    }
  1803. X    zfree(t);
  1804. X    zfree(v);
  1805. X    if (k) {
  1806. X        olen = u.len;
  1807. X        u.len += k / BASEB + 1;
  1808. X        u.v = (HALF *) realloc(u.v, u.len * sizeof(HALF));
  1809. X        while (olen != u.len)
  1810. X            u.v[olen++] = 0;
  1811. X        zshiftl(u, k);
  1812. X    }
  1813. X    ztrim(&u);
  1814. X    *res = u;
  1815. X
  1816. Xdone:
  1817. X    if ((z1.v != oldv1) && (z1.v != oldv2))
  1818. X        zfree(z1);
  1819. X    if ((z2.v != oldv1) && (z2.v != oldv2))
  1820. X        zfree(z2);
  1821. X}
  1822. X
  1823. X
  1824. X/*
  1825. X * Compute the lcm of two integers (least common multiple).
  1826. X * This is done using the formula:  gcd(a,b) * lcm(a,b) = a * b.
  1827. X */
  1828. Xvoid
  1829. Xzlcm(z1, z2, res)
  1830. X    ZVALUE z1, z2, *res;
  1831. X{
  1832. X    ZVALUE temp1, temp2;
  1833. X
  1834. X    zgcd(z1, z2, &temp1);
  1835. X    zquo(z1, temp1, &temp2);
  1836. X    zfree(temp1);
  1837. X    zmul(temp2, z2, res);
  1838. X    zfree(temp2);
  1839. X}
  1840. X
  1841. X
  1842. X/*
  1843. X * Return whether or not two numbers are relatively prime to each other.
  1844. X */
  1845. XBOOL
  1846. Xzrelprime(z1, z2)
  1847. X    ZVALUE z1, z2;            /* numbers to be tested */
  1848. X{
  1849. X    FULL rem1, rem2;        /* remainders */
  1850. X    ZVALUE rem;
  1851. X    BOOL result;
  1852. X
  1853. X    z1.sign = 0;
  1854. X    z2.sign = 0;
  1855. X    if (ziseven(z1) && ziseven(z2))    /* false if both even */
  1856. X        return FALSE;
  1857. X    if (zisunit(z1) || zisunit(z2))    /* true if either is a unit */
  1858. X        return TRUE;
  1859. X    if (ziszero(z1) || ziszero(z2))    /* false if either is zero */
  1860. X        return FALSE;
  1861. X    if (zistwo(z1) || zistwo(z2))    /* true if either is two */
  1862. X        return TRUE;
  1863. X    /*
  1864. X     * Try reducing each number by the product of the first few odd primes
  1865. X     * to see if any of them are a common factor.
  1866. X     */
  1867. X    rem1 = zmodi(z1, 3L * 5 * 7 * 11 * 13);
  1868. X    rem2 = zmodi(z2, 3L * 5 * 7 * 11 * 13);
  1869. X    if (((rem1 % 3) == 0) && ((rem2 % 3) == 0))
  1870. X        return FALSE;
  1871. X    if (((rem1 % 5) == 0) && ((rem2 % 5) == 0))
  1872. X        return FALSE;
  1873. X    if (((rem1 % 7) == 0) && ((rem2 % 7) == 0))
  1874. X        return FALSE;
  1875. X    if (((rem1 % 11) == 0) && ((rem2 % 11) == 0))
  1876. X        return FALSE;
  1877. X    if (((rem1 % 13) == 0) && ((rem2 % 13) == 0))
  1878. X        return FALSE;
  1879. X    /*
  1880. X     * Try a new batch of primes now
  1881. X     */
  1882. X    rem1 = zmodi(z1, 17L * 19 * 23);
  1883. X    rem2 = zmodi(z2, 17L * 19 * 23);
  1884. X    if (((rem1 % 17) == 0) && ((rem2 % 17) == 0))
  1885. X        return FALSE;
  1886. X    if (((rem1 % 19) == 0) && ((rem2 % 19) == 0))
  1887. X        return FALSE;
  1888. X    if (((rem1 % 23) == 0) && ((rem2 % 23) == 0))
  1889. X        return FALSE;
  1890. X    /*
  1891. X     * Yuk, we must actually compute the gcd to know the answer
  1892. X     */
  1893. X    zgcd(z1, z2, &rem);
  1894. X    result = zisunit(rem);
  1895. X    zfree(rem);
  1896. X    return result;
  1897. X}
  1898. X
  1899. X
  1900. X/*
  1901. X * Compute the log of one number base another, to the closest integer.
  1902. X * This is the largest integer which when the second number is raised to it,
  1903. X * the resulting value is less than or equal to the first number.
  1904. X * Example:  zlog(123456, 10) = 5.
  1905. X */
  1906. Xlong
  1907. Xzlog(z1, z2)
  1908. X    ZVALUE z1, z2;
  1909. X{
  1910. X    register ZVALUE *zp;        /* current square */
  1911. X    long power;            /* current power */
  1912. X    long worth;            /* worth of current square */
  1913. X    ZVALUE val;            /* current value of power */
  1914. X    ZVALUE temp;            /* temporary */
  1915. X    ZVALUE squares[32];        /* table of squares of base */
  1916. X
  1917. X    /*
  1918. X     * Make sure that the numbers are nonzero and the base is greater than one.
  1919. X     */
  1920. X    if (zisneg(z1) || ziszero(z1) || zisneg(z2) || zisleone(z2))
  1921. X        math_error("Bad arguments for log");
  1922. X    /*
  1923. X     * Reject trivial cases.
  1924. X     */
  1925. X    if (z1.len < z2.len)
  1926. X        return 0;
  1927. X    if ((z1.len == z2.len) && (z1.v[z1.len-1] < z2.v[z2.len-1]))
  1928. X        return 0;
  1929. X    power = zrel(z1, z2);
  1930. X    if (power <= 0)
  1931. X        return (power + 1);
  1932. X    /*
  1933. X     * Handle any power of two special.
  1934. X     */
  1935. X    if (zisonebit(z2))
  1936. X        return (zhighbit(z1) / zlowbit(z2));
  1937. X    /*
  1938. X     * Handle base 10 special
  1939. X     */
  1940. X    if ((z2.len == 1) && (*z2.v == 10))
  1941. X        return zlog10(z1);
  1942. X    /*
  1943. X     * Now loop by squaring the base each time, and see whether or
  1944. X     * not each successive square is still smaller than the number.
  1945. X     */
  1946. X    worth = 1;
  1947. X    zp = &squares[0];
  1948. X    *zp = z2;
  1949. X    while (((zp->len * 2) - 1) <= z1.len) {    /* while square not too large */
  1950. X        zsquare(*zp, zp + 1);
  1951. X        zp++;
  1952. X        worth *= 2;
  1953. X    }
  1954. X    /*
  1955. X     * Now back down the squares, and multiply them together to see
  1956. X     * exactly how many times the base can be raised by.
  1957. X     */
  1958. X    val = _one_;
  1959. X    power = 0;
  1960. X    for (; zp >= squares; zp--, worth /= 2) {
  1961. X        if ((val.len + zp->len - 1) <= z1.len) {
  1962. X            zmul(val, *zp, &temp);
  1963. X            if (zrel(z1, temp) >= 0) {
  1964. X                zfree(val);
  1965. X                val = temp;
  1966. X                power += worth;
  1967. X            } else
  1968. X                zfree(temp);
  1969. X        }
  1970. X        if (zp != squares)
  1971. X            zfree(*zp);
  1972. X    }
  1973. X    return power;
  1974. X}
  1975. X
  1976. X
  1977. X/*
  1978. X * Return the integral log base 10 of a number.
  1979. X */
  1980. Xlong
  1981. Xzlog10(z)
  1982. X    ZVALUE z;
  1983. X{
  1984. X    register ZVALUE *zp;        /* current square */
  1985. X    long power;            /* current power */
  1986. X    long worth;            /* worth of current square */
  1987. X    ZVALUE val;            /* current value of power */
  1988. X    ZVALUE temp;            /* temporary */
  1989. X
  1990. X    if (!zispos(z))
  1991. X        math_error("Non-positive number for log10");
  1992. X    /*
  1993. X     * Loop by squaring the base each time, and see whether or
  1994. X     * not each successive square is still smaller than the number.
  1995. X     */
  1996. X    worth = 1;
  1997. X    zp = &_tenpowers_[0];
  1998. X    *zp = _ten_;
  1999. X    while (((zp->len * 2) - 1) <= z.len) {    /* while square not too large */
  2000. X        if (zp[1].len == 0)
  2001. X            zsquare(*zp, zp + 1);
  2002. X        zp++;
  2003. X        worth *= 2;
  2004. X    }
  2005. X    /*
  2006. X     * Now back down the squares, and multiply them together to see
  2007. X     * exactly how many times the base can be raised by.
  2008. X     */
  2009. X    val = _one_;
  2010. X    power = 0;
  2011. X    for (; zp >= _tenpowers_; zp--, worth /= 2) {
  2012. X        if ((val.len + zp->len - 1) <= z.len) {
  2013. X            zmul(val, *zp, &temp);
  2014. X            if (zrel(z, temp) >= 0) {
  2015. X                zfree(val);
  2016. X                val = temp;
  2017. X                power += worth;
  2018. X            } else
  2019. X                zfree(temp);
  2020. X        }
  2021. X    }
  2022. X    return power;
  2023. X}
  2024. X
  2025. X
  2026. X/*
  2027. X * Return the number of times that one number will divide another.
  2028. X * This works similarly to zlog, except that divisions must be exact.
  2029. X * For example, zdivcount(540, 3) = 3, since 3^3 divides 540 but 3^4 won't.
  2030. X */
  2031. Xlong
  2032. Xzdivcount(z1, z2)
  2033. X    ZVALUE z1, z2;
  2034. X{
  2035. X    long count;        /* number of factors removed */
  2036. X    ZVALUE tmp;        /* ignored return value */
  2037. X
  2038. X    count = zfacrem(z1, z2, &tmp);
  2039. X    zfree(tmp);
  2040. X    return count;
  2041. X}
  2042. X
  2043. X
  2044. X/*
  2045. X * Remove all occurances of the specified factor from a number.
  2046. X * Also returns the number of factors removed as a function return value.
  2047. X * Example:  zfacrem(540, 3, &x) returns 3 and sets x to 20.
  2048. X */
  2049. Xlong
  2050. Xzfacrem(z1, z2, rem)
  2051. X    ZVALUE z1, z2, *rem;
  2052. X{
  2053. X    register ZVALUE *zp;        /* current square */
  2054. X    long count;            /* total count of divisions */
  2055. X    long worth;            /* worth of current square */
  2056. X    ZVALUE temp1, temp2, temp3;    /* temporaries */
  2057. X    ZVALUE squares[32];        /* table of squares of factor */
  2058. X
  2059. X    z1.sign = 0;
  2060. X    z2.sign = 0;
  2061. X    /*
  2062. X     * Make sure factor isn't 0 or 1.
  2063. X     */
  2064. X    if (zisleone(z2))
  2065. X        math_error("Bad argument for facrem");
  2066. X    /*
  2067. X     * Reject trivial cases.
  2068. X     */
  2069. X    if ((z1.len < z2.len) || (zisodd(z1) && ziseven(z2)) ||
  2070. X        ((z1.len == z2.len) && (z1.v[z1.len-1] < z2.v[z2.len-1]))) {
  2071. X        rem->v = alloc(z1.len);
  2072. X        rem->len = z1.len;
  2073. X        rem->sign = 0;
  2074. X        zcopyval(z1, *rem);
  2075. X        return 0;
  2076. X    }
  2077. X    /*
  2078. X     * Handle any power of two special.
  2079. X     */
  2080. X    if (zisonebit(z2)) {
  2081. X        count = zlowbit(z1) / zlowbit(z2);
  2082. X        rem->v = alloc(z1.len);
  2083. X        rem->len = z1.len;
  2084. X        rem->sign = 0;
  2085. X        zcopyval(z1, *rem);
  2086. X        zshiftr(*rem, count);
  2087. X        ztrim(rem);
  2088. X        return count;
  2089. X    }
  2090. X    /*
  2091. X     * See if the factor goes in even once.
  2092. X     */
  2093. X    zdiv(z1, z2, &temp1, &temp2);
  2094. X    if (!ziszero(temp2)) {
  2095. X        zfree(temp1);
  2096. X        zfree(temp2);
  2097. X        rem->v = alloc(z1.len);
  2098. X        rem->len = z1.len;
  2099. X        rem->sign = 0;
  2100. X        zcopyval(z1, *rem);
  2101. X        return 0;
  2102. X    }
  2103. X    zfree(temp2);
  2104. X    z1 = temp1;
  2105. X    /*
  2106. X     * Now loop by squaring the factor each time, and see whether
  2107. X     * or not each successive square will still divide the number.
  2108. X     */
  2109. X    count = 1;
  2110. X    worth = 1;
  2111. X    zp = &squares[0];
  2112. X    *zp = z2;
  2113. X    while (((zp->len * 2) - 1) <= z1.len) {    /* while square not too large */
  2114. X        zsquare(*zp, &temp1);
  2115. X        zdiv(z1, temp1, &temp2, &temp3);
  2116. X        if (!ziszero(temp3)) {
  2117. X            zfree(temp1);
  2118. X            zfree(temp2);
  2119. X            zfree(temp3);
  2120. X            break;
  2121. X        }
  2122. X        zfree(temp3);
  2123. X        zfree(z1);
  2124. X        z1 = temp2;
  2125. X        *++zp = temp1;
  2126. X        worth *= 2;
  2127. X        count += worth;
  2128. X    }
  2129. X    /*
  2130. X     * Now back down the list of squares, and see if the lower powers
  2131. X     * will divide any more times.
  2132. X     */
  2133. X    for (; zp >= squares; zp--, worth /= 2) {
  2134. X        if (zp->len <= z1.len) {
  2135. X            zdiv(z1, *zp, &temp1, &temp2);
  2136. X            if (ziszero(temp2)) {
  2137. X                temp3 = z1;
  2138. X                z1 = temp1;
  2139. X                temp1 = temp3;
  2140. X                count += worth;
  2141. X            }
  2142. X            zfree(temp1);
  2143. X            zfree(temp2);
  2144. X        }
  2145. X        if (zp != squares)
  2146. X            zfree(*zp);
  2147. X    }
  2148. X    *rem = z1;
  2149. X    return count;
  2150. X}
  2151. X
  2152. X
  2153. X/*
  2154. X * Keep dividing a number by the gcd of it with another number until the
  2155. X * result is relatively prime to the second number.
  2156. X */
  2157. Xvoid
  2158. Xzgcdrem(z1, z2, res)
  2159. X    ZVALUE z1, z2, *res;
  2160. X{
  2161. X    ZVALUE tmp1, tmp2;
  2162. X
  2163. X    /*
  2164. X     * Begin by taking the gcd for the first time.
  2165. X     * If the number is already relatively prime, then we are done.
  2166. X     */
  2167. X    zgcd(z1, z2, &tmp1);
  2168. X    if (zisunit(tmp1) || ziszero(tmp1)) {
  2169. X        res->len = z1.len;
  2170. X        res->v = alloc(z1.len);
  2171. X        res->sign = z1.sign;
  2172. X        zcopyval(z1, *res);
  2173. X        return;
  2174. X    }
  2175. X    zquo(z1, tmp1, &tmp2);
  2176. X    z1 = tmp2;
  2177. X    z2 = tmp1;
  2178. X    /*
  2179. X     * Now keep alternately taking the gcd and removing factors until
  2180. X     * the gcd becomes one.
  2181. X     */
  2182. X    while (!zisunit(z2)) {
  2183. X        (void) zfacrem(z1, z2, &tmp1);
  2184. X        zfree(z1);
  2185. X        z1 = tmp1;
  2186. X        zgcd(z1, z2, &tmp1);
  2187. X        zfree(z2);
  2188. X        z2 = tmp1;
  2189. X    }
  2190. X    *res = z1;
  2191. X}
  2192. X
  2193. X
  2194. X/*
  2195. X * Find the lowest prime factor of a number if one can be found.
  2196. X * Search is conducted for the first count primes.
  2197. X * Returns 1 if no factor was found.
  2198. X */
  2199. Xlong
  2200. Xzlowfactor(z, count)
  2201. X    ZVALUE z;
  2202. X    long count;
  2203. X{
  2204. X    FULL p, d;
  2205. X    ZVALUE div, tmp;
  2206. X    HALF divval[2];
  2207. X
  2208. X    if ((--count < 0) || ziszero(z))
  2209. X        return 1;
  2210. X    if (ziseven(z))
  2211. X        return 2;
  2212. X    div.sign = 0;
  2213. X    div.v = divval;
  2214. X    for (p = 3; (count > 0); p += 2) {
  2215. X        for (d = 3; (d * d) <= p; d += 2)
  2216. X            if ((p % d) == 0)
  2217. X                goto next;
  2218. X        divval[0] = (p & BASE1);
  2219. X        divval[1] = (p / BASE);
  2220. X        div.len = 1 + (p >= BASE);
  2221. X        zmod(z, div, &tmp);
  2222. X        if (ziszero(tmp))
  2223. X            return p;
  2224. X        zfree(tmp);
  2225. X        count--;
  2226. Xnext:;
  2227. X    }
  2228. X    return 1;
  2229. X}
  2230. X
  2231. X
  2232. X/*
  2233. X * Return the number of digits (base 10) in a number, ignoring the sign.
  2234. X */
  2235. Xlong
  2236. Xzdigits(z1)
  2237. X    ZVALUE z1;
  2238. X{
  2239. X    long count, val;
  2240. X
  2241. X    z1.sign = 0;
  2242. X    if (zistiny(z1)) {    /* do small numbers ourself */
  2243. X        count = 1;
  2244. X        val = 10;
  2245. X        while (*z1.v >= (HALF)val) {
  2246. X            count++;
  2247. X            val *= 10;
  2248. X        }
  2249. X        return count;
  2250. X    }
  2251. X    return (zlog10(z1) + 1);
  2252. X}
  2253. X
  2254. X
  2255. X/*
  2256. X * Return the single digit at the specified decimal place of a number,
  2257. X * where 0 means the rightmost digit.  Example:  zdigit(1234, 1) = 3.
  2258. X */
  2259. XFLAG
  2260. Xzdigit(z1, n)
  2261. X    ZVALUE z1;
  2262. X    long n;
  2263. X{
  2264. X    ZVALUE tmp1, tmp2;
  2265. X    FLAG res;
  2266. X
  2267. X    z1.sign = 0;
  2268. X    if (ziszero(z1) || (n < 0) || (n / BASEDIG >= z1.len))
  2269. X        return 0;
  2270. X    if (n == 0)
  2271. X        return zmodi(z1, 10L);
  2272. X    if (n == 1)
  2273. X        return zmodi(z1, 100L) / 10;
  2274. X    if (n == 2)
  2275. X        return zmodi(z1, 1000L) / 100;
  2276. X    if (n == 3)
  2277. X        return zmodi(z1, 10000L) / 1000;
  2278. X    ztenpow(n, &tmp1);
  2279. X    zquo(z1, tmp1, &tmp2);
  2280. X    res = zmodi(tmp2, 10L);
  2281. X    zfree(tmp1);
  2282. X    zfree(tmp2);
  2283. X    return res;
  2284. X}
  2285. X
  2286. X
  2287. X/*
  2288. X * Find the square root of a number.  This is the greatest integer whose
  2289. X * square is less than or equal to the number. Returns TRUE if the
  2290. X * square root is exact.
  2291. X */
  2292. XBOOL
  2293. Xzsqrt(z1, dest)
  2294. X    ZVALUE z1, *dest;
  2295. X{
  2296. X    ZVALUE try, quo, rem, old, temp;
  2297. X    FULL iquo, val;
  2298. X    long i,j;
  2299. X
  2300. X    if (z1.sign)
  2301. X        math_error("Square root of negative number");
  2302. X    if (ziszero(z1)) {
  2303. X        *dest = _zero_;
  2304. X        return TRUE;
  2305. X    }
  2306. X    if ((*z1.v < 4) && zistiny(z1)) {
  2307. X        *dest = _one_;
  2308. X        return (*z1.v == 1);
  2309. X    }
  2310. X    /*
  2311. X     * Pick the square root of the leading one or two digits as a first guess.
  2312. X     */
  2313. X    val = z1.v[z1.len-1];
  2314. X    if ((z1.len & 0x1) == 0)
  2315. X        val = (val * BASE) + z1.v[z1.len-2];
  2316. X
  2317. X    /*
  2318. X     * Find the largest power of 2 that when squared
  2319. X     * is <= val > 0.  Avoid multiply overflow by doing 
  2320. X     * a careful check at the BASE boundary.
  2321. X     */
  2322. X    j = 1<<(BASEB+BASEB-2);
  2323. X    if (val > j) {
  2324. X        iquo = BASE;
  2325. X    } else {
  2326. X        i = BASEB-1;
  2327. X        while (j > val) {
  2328. X            --i;
  2329. X            j >>= 2;
  2330. X        }
  2331. X        iquo = bitmask[i];
  2332. X    }
  2333. X
  2334. X    for (i = 8; i > 0; i--)
  2335. X        iquo = (iquo + (val / iquo)) / 2;
  2336. X    if (iquo > BASE1)
  2337. X        iquo = BASE1;
  2338. X    /*
  2339. X     * Allocate the numbers to use for the main loop.
  2340. X     * The size and high bits of the final result are correctly set here.
  2341. X     * Notice that the remainder of the test value is rubbish, but this
  2342. X     * is unimportant.
  2343. X     */
  2344. X    try.sign = 0;
  2345. X    try.len = (z1.len + 1) / 2;
  2346. X    try.v = alloc(try.len);
  2347. X    try.v[try.len-1] = (HALF)iquo;
  2348. X    old.sign = 0;
  2349. X    old.v = alloc(try.len);
  2350. X    old.len = 1;
  2351. X    *old.v = 0;
  2352. X    /*
  2353. X     * Main divide and average loop
  2354. X     */
  2355. X    for (;;) {
  2356. X        zdiv(z1, try, &quo, &rem);
  2357. X        i = zrel(try, quo);
  2358. X        if ((i == 0) && ziszero(rem)) {    /* exact square root */
  2359. X            zfree(rem);
  2360. X            zfree(quo);
  2361. X            zfree(old);
  2362. X            *dest = try;
  2363. X            return TRUE;
  2364. X        }
  2365. X        zfree(rem);
  2366. X        if (i <= 0) {
  2367. X            /*
  2368. X            * Current try is less than or equal to the square root since
  2369. X            * it is less than the quotient.  If the quotient is equal to
  2370. X            * the try, we are all done.  Also, if the try is equal to the
  2371. X            * old try value, we are done since no improvement occurred.
  2372. X            * If not, save the improved value and loop some more.
  2373. X            */
  2374. X            if ((i == 0) || (zcmp(old, try) == 0)) {
  2375. X                zfree(quo);
  2376. X                zfree(old);
  2377. X                *dest = try;
  2378. X                return FALSE;
  2379. X            }
  2380. X            old.len = try.len;
  2381. X            zcopyval(try, old);
  2382. X        }
  2383. X        /* average current try and quotent for the new try */
  2384. X        zadd(try, quo, &temp);
  2385. X        zfree(quo);
  2386. X        zfree(try);
  2387. X        try = temp;
  2388. X        zshiftr(try, 1L);
  2389. X        zquicktrim(try);
  2390. X    }
  2391. X}
  2392. X
  2393. X
  2394. X/*
  2395. X * Take an arbitrary root of a number (to the greatest integer).
  2396. X * This uses the following iteration to get the Kth root of N:
  2397. X *    x = ((K-1) * x + N / x^(K-1)) / K
  2398. X */
  2399. Xvoid
  2400. Xzroot(z1, z2, dest)
  2401. X    ZVALUE z1, z2, *dest;
  2402. X{
  2403. X    ZVALUE try, quo, old, temp, temp2;
  2404. X    ZVALUE k1;            /* holds k - 1 */
  2405. X    int sign;
  2406. X    long i, k, highbit;
  2407. X    SIUNION sival;
  2408. X
  2409. X    sign = z1.sign;
  2410. X    if (sign && ziseven(z2))
  2411. X        math_error("Even root of negative number");
  2412. X    if (ziszero(z2) || zisneg(z2))
  2413. X        math_error("Non-positive root");
  2414. X    if (ziszero(z1)) {    /* root of zero */
  2415. X        *dest = _zero_;
  2416. X        return;
  2417. X    }
  2418. X    if (zisunit(z2)) {    /* first root */
  2419. X        zcopy(z1, dest);
  2420. X        return;
  2421. X    }
  2422. X    if (zisbig(z2)) {    /* humongous root */
  2423. X        *dest = _one_;
  2424. X        dest->sign = (HALF)sign;
  2425. X        return;
  2426. X    }
  2427. X    k = (zistiny(z2) ? z1tol(z2) : z2tol(z2));
  2428. X    highbit = zhighbit(z1);
  2429. X    if (highbit < k) {    /* too high a root */
  2430. X        *dest = _one_;
  2431. X        dest->sign = (HALF)sign;
  2432. X        return;
  2433. X    }
  2434. X    sival.ivalue = k - 1;
  2435. X    k1.v = &sival.silow;
  2436. X    k1.len = 1 + (sival.sihigh != 0);
  2437. X    k1.sign = 0;
  2438. X    z1.sign = 0;
  2439. X    /*
  2440. X     * Allocate the numbers to use for the main loop.
  2441. X     * The size and high bits of the final result are correctly set here.
  2442. X     * Notice that the remainder of the test value is rubbish, but this
  2443. SHAR_EOF
  2444. echo "End of part 11"
  2445. echo "File calc2.9.0/zfunc.c is continued in part 12"
  2446. echo "12" > s2_seq_.tmp
  2447. exit 0
  2448.