home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / calc / part09 < prev    next >
Encoding:
Text File  |  1992-05-09  |  43.8 KB  |  2,181 lines

  1. Newsgroups: comp.sources.unix
  2. From: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  3. Subject: v26i035: CALC - An arbitrary precision C-like calculator, Part09/21
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  8. Posting-Number: Volume 26, Issue 35
  9. Archive-Name: calc/part09
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 9 (of 21)."
  18. # Contents:  qmath.c qtrans.c
  19. # Wrapped by dbell@elm on Tue Feb 25 15:21:04 1992
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'qmath.c' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'qmath.c'\"
  23. else
  24. echo shar: Extracting \"'qmath.c'\" \(20144 characters\)
  25. sed "s/^X//" >'qmath.c' <<'END_OF_FILE'
  26. X/*
  27. X * Copyright (c) 1992 David I. Bell
  28. X * Permission is granted to use, distribute, or modify this source,
  29. X * provided that this copyright notice remains intact.
  30. X *
  31. X * Extended precision rational arithmetic primitive routines
  32. X */
  33. X
  34. X#include <stdio.h>
  35. X#include "math.h"
  36. X
  37. X
  38. XNUMBER _qzero_ =    { { _zeroval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  39. XNUMBER _qone_ =        { { _oneval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  40. Xstatic NUMBER _qtwo_ =    { { _twoval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  41. Xstatic NUMBER _qten_ =    { { _tenval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  42. XNUMBER _qnegone_ =    { { _oneval_, 1, 1 }, { _oneval_, 1, 0 }, 1 };
  43. XNUMBER _qonehalf_ =    { { _oneval_, 1, 0 }, { _twoval_, 1, 0 }, 1 };
  44. X
  45. X
  46. X#if 0
  47. Xstatic char *abortmsg = "Calculation aborted";
  48. X#endif
  49. Xstatic char *memmsg = "Not enough memory";
  50. X
  51. X
  52. X/*
  53. X * Create another copy of a number.
  54. X *    q2 = qcopy(q1);
  55. X */
  56. XNUMBER *
  57. Xqcopy(q)
  58. X    register NUMBER *q;
  59. X{
  60. X    register NUMBER *r;
  61. X
  62. X    r = qalloc();
  63. X    r->num.sign = q->num.sign;
  64. X    if (!isunit(q->num)) {
  65. X        r->num.len = q->num.len;
  66. X        r->num.v = alloc(r->num.len);
  67. X        copyval(q->num, r->num);
  68. X    }
  69. X    if (!isunit(q->den)) {
  70. X        r->den.len = q->den.len;
  71. X        r->den.v = alloc(r->den.len);
  72. X        copyval(q->den, r->den);
  73. X    }
  74. X    return r;
  75. X}
  76. X
  77. X
  78. X/*
  79. X * Convert a number to a normal integer.
  80. X *    i = qtoi(q);
  81. X */
  82. Xlong
  83. Xqtoi(q)
  84. X    register NUMBER *q;
  85. X{
  86. X    long i;
  87. X    ZVALUE res;
  88. X
  89. X    if (qisint(q))
  90. X        return ztoi(q->num);
  91. X    zquo(q->num, q->den, &res);
  92. X    i = ztoi(res);
  93. X    freeh(res.v);
  94. X    return i;
  95. X}
  96. X
  97. X
  98. X/*
  99. X * Convert a normal integer into a number.
  100. X *    q = itoq(i);
  101. X */
  102. XNUMBER *
  103. Xitoq(i)
  104. X    long i;
  105. X{
  106. X    register NUMBER *q;
  107. X
  108. X    if ((i >= -1) && (i <= 10)) {
  109. X        switch ((int) i) {
  110. X            case 0: q = &_qzero_; break;
  111. X            case 1: q = &_qone_; break;
  112. X            case 2: q = &_qtwo_; break;
  113. X            case 10: q = &_qten_; break;
  114. X            case -1: q = &_qnegone_; break;
  115. X            default: q = NULL;
  116. X        }
  117. X        if (q)
  118. X            return qlink(q);
  119. X    }
  120. X    q = qalloc();
  121. X    itoz(i, &q->num);
  122. X    return q;
  123. X}
  124. X
  125. X
  126. X/*
  127. X * Create a number from the given integral numerator and denominator.
  128. X *    q = iitoq(inum, iden);
  129. X */
  130. XNUMBER *
  131. Xiitoq(inum, iden)
  132. X    long inum, iden;
  133. X{
  134. X    register NUMBER *q;
  135. X    long d;
  136. X    BOOL sign;
  137. X
  138. X    if (iden == 0)
  139. X        error("Division by zero");
  140. X    if (inum == 0)
  141. X        return qlink(&_qzero_);
  142. X    sign = 0;
  143. X    if (inum < 0) {
  144. X        sign = 1;
  145. X        inum = -inum;
  146. X    }
  147. X    if (iden < 0) {
  148. X        sign = 1 - sign;
  149. X        iden = -iden;
  150. X    }
  151. X    d = iigcd(inum, iden);
  152. X    inum /= d;
  153. X    iden /= d;
  154. X    if (iden == 1)
  155. X        return itoq(sign ? -inum : inum);
  156. X    q = qalloc();
  157. X    if (inum != 1)
  158. X        itoz(inum, &q->num);
  159. X    itoz(iden, &q->den);
  160. X    q->num.sign = sign;
  161. X    return q;
  162. X}
  163. X
  164. X
  165. X/*
  166. X * Add two numbers to each other.
  167. X *    q3 = qadd(q1, q2);
  168. X */
  169. XNUMBER *
  170. Xqadd(q1, q2)
  171. X    register NUMBER *q1, *q2;
  172. X{
  173. X    NUMBER *r;
  174. X    ZVALUE t1, t2, temp, d1, d2, vpd1, upd1;
  175. X
  176. X    r = qalloc();
  177. X    /*
  178. X     * If either number is an integer, then the result is easy.
  179. X     */
  180. X    if (qisint(q1) && qisint(q2)) {
  181. X        zadd(q1->num, q2->num, &r->num);
  182. X        return r;
  183. X    }
  184. X    if (qisint(q2)) {
  185. X        zmul(q1->den, q2->num, &temp);
  186. X        zadd(q1->num, temp, &r->num);
  187. X        freeh(temp.v);
  188. X        zcopy(q1->den, &r->den);
  189. X        return r;
  190. X    }
  191. X    if (qisint(q1)) {
  192. X        zmul(q2->den, q1->num, &temp);
  193. X        zadd(q2->num, temp, &r->num);
  194. X        freeh(temp.v);
  195. X        zcopy(q2->den, &r->den);
  196. X        return r;
  197. X    }
  198. X    /*
  199. X     * Both arguments are true fractions, so we need more work.
  200. X     * If the denominators are relatively prime, then the answer is the
  201. X     * straightforward cross product result with no need for reduction.
  202. X     */
  203. X    zgcd(q1->den, q2->den, &d1);
  204. X    if (isunit(d1)) {
  205. X        freeh(d1.v);
  206. X        zmul(q1->num, q2->den, &t1);
  207. X        zmul(q1->den, q2->num, &t2);
  208. X        zadd(t1, t2, &r->num);
  209. X        freeh(t1.v);
  210. X        freeh(t2.v);
  211. X        zmul(q1->den, q2->den, &r->den);
  212. X        return r;
  213. X    }
  214. X    /*
  215. X     * The calculation is now more complicated.
  216. X     * See Knuth Vol 2 for details.
  217. X     */
  218. X    zquo(q2->den, d1, &vpd1);
  219. X    zquo(q1->den, d1, &upd1);
  220. X    zmul(q1->num, vpd1, &t1);
  221. X    zmul(q2->num, upd1, &t2);
  222. X    zadd(t1, t2, &temp);
  223. X    freeh(t1.v);
  224. X    freeh(t2.v);
  225. X    freeh(vpd1.v);
  226. X    zgcd(temp, d1, &d2);
  227. X    freeh(d1.v);
  228. X    if (isunit(d2)) {
  229. X        freeh(d2.v);
  230. X        r->num = temp;
  231. X        zmul(upd1, q2->den, &r->den);
  232. X        freeh(upd1.v);
  233. X        return r;
  234. X    }
  235. X    zquo(temp, d2, &r->num);
  236. X    freeh(temp.v);
  237. X    zquo(q2->den, d2, &temp);
  238. X    freeh(d2.v);
  239. X    zmul(temp, upd1, &r->den);
  240. X    freeh(temp.v);
  241. X    freeh(upd1.v);
  242. X    return r;
  243. X}
  244. X
  245. X
  246. X/*
  247. X * Subtract one number from another.
  248. X *    q3 = qsub(q1, q2);
  249. X */
  250. XNUMBER *
  251. Xqsub(q1, q2)
  252. X    register NUMBER *q1, *q2;
  253. X{
  254. X    NUMBER t, *r;
  255. X
  256. X    if (q1 == q2)
  257. X        return qlink(&_qzero_);
  258. X    if (qisint(q1) && qisint(q2)) {
  259. X        r = qalloc();
  260. X        zsub(q1->num, q2->num, &r->num);
  261. X        return r;
  262. X    }
  263. X    t = *q2;
  264. X    t.num.sign = !t.num.sign;
  265. X    return qadd(q1, &t);
  266. X}
  267. X
  268. X
  269. X/*
  270. X * Increment a number by one.
  271. X */
  272. XNUMBER *
  273. Xqinc(q)
  274. X    NUMBER *q;
  275. X{
  276. X    NUMBER *r;
  277. X
  278. X    r = qalloc();
  279. X    if (qisint(q)) {
  280. X        zadd(q->num, _one_, &r->num);
  281. X        return r;
  282. X    }
  283. X    zadd(q->num, q->den, &r->num);
  284. X    zcopy(q->den, &r->den);
  285. X    return r;
  286. X}
  287. X
  288. X
  289. X/*
  290. X * Decrement a number by one.
  291. X */
  292. XNUMBER *
  293. Xqdec(q)
  294. X    NUMBER *q;
  295. X{
  296. X    NUMBER *r;
  297. X
  298. X    r = qalloc();
  299. X    if (qisint(q)) {
  300. X        zsub(q->num, _one_, &r->num);
  301. X        return r;
  302. X    }
  303. X    zsub(q->num, q->den, &r->num);
  304. X    zcopy(q->den, &r->den);
  305. X    return r;
  306. X}
  307. X
  308. X
  309. X#ifdef CODE
  310. X/*
  311. X * Add a normal small integer value to an arbitrary number.
  312. X */
  313. XNUMBER *
  314. Xqaddi(q1, n)
  315. X    NUMBER *q1;
  316. X    long n;
  317. X{
  318. X    NUMBER addnum;        /* temporary number */
  319. X    HALF addval[2];        /* value of small number */
  320. X    BOOL neg;        /* TRUE if number is neg */
  321. X
  322. X    if (n == 0)
  323. X        return qlink(q1);
  324. X    if (n == 1)
  325. X        return qinc(q1);
  326. X    if (n == -1)
  327. X        return qdec(q1);
  328. X    if (qiszero(q1))
  329. X        return itoq(n);
  330. X    addnum.num.sign = 0;
  331. X    addnum.num.len = 1;
  332. X    addnum.num.v = addval;
  333. X    addnum.den = _one_;
  334. X    neg = (n < 0);
  335. X    if (neg)
  336. X        n = -n;
  337. X    addval[0] = (HALF) n;
  338. X    n = (((FULL) n) >> BASEB);
  339. X    if (n) {
  340. X        addval[1] = (HALF) n;
  341. X        addnum.num.len = 2;
  342. X    }
  343. X    if (neg)
  344. X        return qsub(q1, &addnum);
  345. X    else
  346. X        return qadd(q1, &addnum);
  347. X}
  348. X#endif
  349. X
  350. X
  351. X/*
  352. X * Multiply two numbers.
  353. X *    q3 = qmul(q1, q2);
  354. X */
  355. XNUMBER *
  356. Xqmul(q1, q2)
  357. X    register NUMBER *q1, *q2;
  358. X{
  359. X    NUMBER *r;            /* returned value */
  360. X    ZVALUE n1, n2, d1, d2;        /* numerators and denominators */
  361. X    ZVALUE tmp;
  362. X
  363. X    if (qiszero(q1) || qiszero(q2))
  364. X        return qlink(&_qzero_);
  365. X    if (qisone(q1))
  366. X        return qlink(q2);
  367. X    if (qisone(q2))
  368. X        return qlink(q1);
  369. X    if (qisint(q1) && qisint(q2)) {    /* easy results if integers */
  370. X        r = qalloc();
  371. X        zmul(q1->num, q2->num, &r->num);
  372. X        return r;
  373. X    }
  374. X    n1 = q1->num;
  375. X    n2 = q2->num;
  376. X    d1 = q1->den;
  377. X    d2 = q2->den;
  378. X    if (iszero(d1) || iszero(d2))
  379. X        error("Division by zero");
  380. X    if (iszero(n1) || iszero(n2))
  381. X        return qlink(&_qzero_);
  382. X    if (!isunit(n1) && !isunit(d2)) {    /* possibly reduce */
  383. X        zgcd(n1, d2, &tmp);
  384. X        if (!isunit(tmp)) {
  385. X            zquo(q1->num, tmp, &n1);
  386. X            zquo(q2->den, tmp, &d2);
  387. X        }
  388. X        freeh(tmp.v);
  389. X    }
  390. X    if (!isunit(n2) && !isunit(d1)) {    /* again possibly reduce */
  391. X        zgcd(n2, d1, &tmp);
  392. X        if (!isunit(tmp)) {
  393. X            zquo(q2->num, tmp, &n2);
  394. X            zquo(q1->den, tmp, &d1);
  395. X        }
  396. X        freeh(tmp.v);
  397. X    }
  398. X    r = qalloc();
  399. X    zmul(n1, n2, &r->num);
  400. X    zmul(d1, d2, &r->den);
  401. X    if (q1->num.v != n1.v)
  402. X        freeh(n1.v);
  403. X    if (q1->den.v != d1.v)
  404. X        freeh(d1.v);
  405. X    if (q2->num.v != n2.v)
  406. X        freeh(n2.v);
  407. X    if (q2->den.v != d2.v)
  408. X        freeh(d2.v);
  409. X    return r;
  410. X}
  411. X
  412. X
  413. X#if 0
  414. X/*
  415. X * Multiply a number by a small integer.
  416. X *    q2 = qmuli(q1, n);
  417. X */
  418. XNUMBER *
  419. Xqmuli(q, n)
  420. X    NUMBER *q;
  421. X    long n;
  422. X{
  423. X    NUMBER *r;
  424. X    long d;            /* gcd of multiplier and denominator */
  425. X    int sign;
  426. X
  427. X    if ((n == 0) || qiszero(q))
  428. X        return qlink(&_qzero_);
  429. X    if (n == 1)
  430. X        return qlink(q);
  431. X    r = qalloc();
  432. X    if (qisint(q)) {
  433. X        zmuli(q->num, n, &r->num);
  434. X        return r;
  435. X    }
  436. X    sign = 1;
  437. X    if (n < 0) {
  438. X        n = -n;
  439. X        sign = -1;
  440. X    }
  441. X    d = zmodi(q->den, n);
  442. X    d = iigcd(d, n);
  443. X    zmuli(q->num, (n * sign) / d, &r->num);
  444. X    (void) zdivi(q->den, d, &r->den);
  445. X    return r;
  446. X}
  447. X#endif
  448. X
  449. X
  450. X/*
  451. X * Divide two numbers (as fractions).
  452. X *    q3 = qdiv(q1, q2);
  453. X */
  454. XNUMBER *
  455. Xqdiv(q1, q2)
  456. X    register NUMBER *q1, *q2;
  457. X{
  458. X    NUMBER temp;
  459. X
  460. X    if (q1 == q2) {
  461. X        if (qiszero(q2))
  462. X            error("Division by zero");
  463. X        return qlink(&_qone_);
  464. X    }
  465. X    if (qisone(q1))
  466. X        return qinv(q2);
  467. X    temp.num = q2->den;
  468. X    temp.den = q2->num;
  469. X    temp.num.sign = temp.den.sign;
  470. X    temp.den.sign = 0;
  471. X    temp.links = 1;
  472. X    return qmul(q1, &temp);
  473. X}
  474. X
  475. X
  476. X/*
  477. X * Divide a number by a small integer.
  478. X *    q2 = qdivi(q1, n);
  479. X */
  480. XNUMBER *
  481. Xqdivi(q, n)
  482. X    NUMBER *q;
  483. X    long n;
  484. X{
  485. X    NUMBER *r;
  486. X    long d;            /* gcd of divisor and numerator */
  487. X    int sign;
  488. X
  489. X    if (n == 0)
  490. X        error("Division by zero");
  491. X    if ((n == 1) || qiszero(q))
  492. X        return qlink(q);
  493. X    sign = 1;
  494. X    if (n < 0) {
  495. X        n = -n;
  496. X        sign = -1;
  497. X    }
  498. X    r = qalloc();
  499. X    d = zmodi(q->num, n);
  500. X    d = iigcd(d, n);
  501. X    (void) zdivi(q->num, d * sign, &r->num);
  502. X    zmuli(q->den, n / d, &r->den);
  503. X    return r;
  504. X}
  505. X
  506. X
  507. X/*
  508. X * Return the quotient when one number is divided by another.
  509. X * This works for fractional values also, and in all cases:
  510. X *    qquo(q1, q2) = int(q1 / q2).
  511. X */
  512. XNUMBER *
  513. Xqquo(q1, q2)
  514. X    register NUMBER *q1, *q2;
  515. X{
  516. X    ZVALUE num, den, res;
  517. X    NUMBER *q;
  518. X
  519. X    if (isunit(q1->num))
  520. X        num = q2->den;
  521. X    else if (isunit(q2->den))
  522. X        num = q1->num;
  523. X    else
  524. X        zmul(q1->num, q2->den, &num);
  525. X    if (isunit(q1->den))
  526. X        den = q2->num;
  527. X    else if (isunit(q2->num))
  528. X        den = q1->den;
  529. X    else
  530. X        zmul(q1->den, q2->num, &den);
  531. X    zquo(num, den, &res);
  532. X    if ((num.v != q2->den.v) && (num.v != q1->num.v))
  533. X        freeh(num.v);
  534. X    if ((den.v != q2->num.v) && (den.v != q1->den.v))
  535. X        freeh(den.v);
  536. X    if (iszero(res)) {
  537. X        freeh(res.v);
  538. X        return qlink(&_qzero_);
  539. X    }
  540. X    if (isunit(res)) {
  541. X        q = (res.sign ? &_qnegone_ : &_qone_);
  542. X        freeh(res.v);
  543. X        return qlink(q);
  544. X    }
  545. X    q = qalloc();
  546. X    q->num = res;
  547. X    return q;
  548. X}
  549. X
  550. X
  551. X/*
  552. X * Return the absolute value of a number.
  553. X *    q2 = qabs(q1);
  554. X */
  555. XNUMBER *
  556. Xqabs(q)
  557. X    register NUMBER *q;
  558. X{
  559. X    register NUMBER *r;
  560. X
  561. X    if (q->num.sign == 0)
  562. X        return qlink(q);
  563. X    r = qalloc();
  564. X    if (!isunit(q->num))
  565. X        zcopy(q->num, &r->num);
  566. X    if (!isunit(q->den))
  567. X        zcopy(q->den, &r->den);
  568. X    r->num.sign = 0;
  569. X    return r;
  570. X}
  571. X
  572. X
  573. X/*
  574. X * Negate a number.
  575. X *    q2 = qneg(q1);
  576. X */
  577. XNUMBER *
  578. Xqneg(q)
  579. X    register NUMBER *q;
  580. X{
  581. X    register NUMBER *r;
  582. X
  583. X    if (qiszero(q))
  584. X        return qlink(&_qzero_);
  585. X    r = qalloc();
  586. X    if (!isunit(q->num))
  587. X        zcopy(q->num, &r->num);
  588. X    if (!isunit(q->den))
  589. X        zcopy(q->den, &r->den);
  590. X    r->num.sign = !q->num.sign;
  591. X    return r;
  592. X}
  593. X
  594. X
  595. X/*
  596. X * Return the sign of a number (-1, 0 or 1)
  597. X */
  598. XNUMBER *
  599. Xqsign(q)
  600. X    NUMBER *q;
  601. X{
  602. X    if (qiszero(q))
  603. X        return qlink(&_qzero_);
  604. X    if (qisneg(q))
  605. X        return qlink(&_qnegone_);
  606. X    return qlink(&_qone_);
  607. X}
  608. X
  609. X
  610. X/*
  611. X * Invert a number.
  612. X *    q2 = qinv(q1);
  613. X */
  614. XNUMBER *
  615. Xqinv(q)
  616. X    register NUMBER *q;
  617. X{
  618. X    register NUMBER *r;
  619. X
  620. X    if (qisunit(q)) {
  621. X        r = (qisneg(q) ? &_qnegone_ : &_qone_);
  622. X        return qlink(r);
  623. X    }
  624. X    if (qiszero(q))
  625. X        error("Division by zero");
  626. X    r = qalloc();
  627. X    if (!isunit(q->num))
  628. X        zcopy(q->num, &r->den);
  629. X    if (!isunit(q->den))
  630. X        zcopy(q->den, &r->num);
  631. X    r->num.sign = q->num.sign;
  632. X    r->den.sign = 0;
  633. X    return r;
  634. X}
  635. X
  636. X
  637. X/*
  638. X * Return just the numerator of a number.
  639. X *    q2 = qnum(q1);
  640. X */
  641. XNUMBER *
  642. Xqnum(q)
  643. X    register NUMBER *q;
  644. X{
  645. X    register NUMBER *r;
  646. X
  647. X    if (qisint(q))
  648. X        return qlink(q);
  649. X    if (isunit(q->num)) {
  650. X        r = (qisneg(q) ? &_qnegone_ : &_qone_);
  651. X        return qlink(r);
  652. X    }
  653. X    r = qalloc();
  654. X    zcopy(q->num, &r->num);
  655. X    return r;
  656. X}
  657. X
  658. X
  659. X/*
  660. X * Return just the denominator of a number.
  661. X *    q2 = qden(q1);
  662. X */
  663. XNUMBER *
  664. Xqden(q)
  665. X    register NUMBER *q;
  666. X{
  667. X    register NUMBER *r;
  668. X
  669. X    if (qisint(q))
  670. X        return qlink(&_qone_);
  671. X    r = qalloc();
  672. X    zcopy(q->den, &r->num);
  673. X    return r;
  674. X}
  675. X
  676. X
  677. X/*
  678. X * Return the fractional part of a number.
  679. X *    q2 = qfrac(q1);
  680. X */
  681. XNUMBER *
  682. Xqfrac(q)
  683. X    register NUMBER *q;
  684. X{
  685. X    register NUMBER *r;
  686. X
  687. X    if (qisint(q))
  688. X        return qlink(&_qzero_);
  689. X    if ((q->num.len < q->den.len) || ((q->num.len == q->den.len) &&
  690. X        (q->num.v[q->num.len - 1] < q->den.v[q->num.len - 1])))
  691. X            return qlink(q);
  692. X    r = qalloc();
  693. X    zmod(q->num, q->den, &r->num);
  694. X    zcopy(q->den, &r->den);
  695. X    r->num.sign = q->num.sign;
  696. X    return r;
  697. X}
  698. X
  699. X
  700. X/*
  701. X * Return the integral part of a number.
  702. X *    q2 = qint(q1);
  703. X */
  704. XNUMBER *
  705. Xqint(q)
  706. X    register NUMBER *q;
  707. X{
  708. X    register NUMBER *r;
  709. X
  710. X    if (qisint(q))
  711. X        return qlink(q);
  712. X    if ((q->num.len < q->den.len) || ((q->num.len == q->den.len) &&
  713. X        (q->num.v[q->num.len - 1] < q->den.v[q->num.len - 1])))
  714. X            return qlink(&_qzero_);
  715. X    r = qalloc();
  716. X    zquo(q->num, q->den, &r->num);
  717. X    return r;
  718. X}
  719. X
  720. X
  721. X/*
  722. X * Compute the square of a number.
  723. X */
  724. XNUMBER *
  725. Xqsquare(q)
  726. X    register NUMBER *q;
  727. X{
  728. X    ZVALUE num, den;
  729. X
  730. X    if (qiszero(q))
  731. X        return qlink(&_qzero_);
  732. X    if (qisunit(q))
  733. X        return qlink(&_qone_);
  734. X    num = q->num;
  735. X    den = q->den;
  736. X    q = qalloc();
  737. X    if (!isunit(num))
  738. X        zsquare(num, &q->num);
  739. X    if (!isunit(den))
  740. X        zsquare(den, &q->den);
  741. X    return q;
  742. X}
  743. X
  744. X
  745. X/*
  746. X * Shift an integer by a given number of bits. This multiplies the number
  747. X * by the appropriate power of two.  Positive numbers shift left, negative
  748. X * ones shift right.  Low bits are truncated when shifting right.
  749. X */
  750. XNUMBER *
  751. Xqshift(q, n)
  752. X    NUMBER *q;
  753. X    long n;
  754. X{
  755. X    register NUMBER *r;
  756. X
  757. X    if (qisfrac(q))
  758. X        error("Shift of non-integer");
  759. X    if (qiszero(q) || (n == 0))
  760. X        return qlink(q);
  761. X    if (n <= -(q->num.len * BASEB))
  762. X        return qlink(&_qzero_);
  763. X    r = qalloc();
  764. X    zshift(q->num, n, &r->num);
  765. X    return r;
  766. X}
  767. X
  768. X
  769. X/*
  770. X * Scale a number by a power of two, as in:
  771. X *    ans = q * 2^n.
  772. X * This is similar to shifting, except that fractions work.
  773. X */
  774. XNUMBER *
  775. Xqscale(q, pow)
  776. X    NUMBER *q;
  777. X    long pow;
  778. X{
  779. X    long numshift, denshift, tmp;
  780. X    NUMBER *r;
  781. X
  782. X    if (qiszero(q) || (pow == 0))
  783. X        return qlink(q);
  784. X    if ((pow > 1000000L) || (pow < -1000000L))
  785. X        error("Very large scale value");
  786. X    numshift = isodd(q->num) ? 0 : zlowbit(q->num);
  787. X    denshift = isodd(q->den) ? 0 : zlowbit(q->den);
  788. X    if (pow > 0) {
  789. X        tmp = pow;
  790. X        if (tmp > denshift)
  791. X        tmp = denshift;
  792. X        denshift = -tmp;
  793. X        numshift = (pow - tmp);
  794. X    } else {
  795. X        pow = -pow;
  796. X        tmp = pow;
  797. X        if (tmp > numshift)
  798. X            tmp = numshift;
  799. X        numshift = -tmp;
  800. X        denshift = (pow - tmp);
  801. X    }
  802. X    r = qalloc();
  803. X    if (numshift)
  804. X        zshift(q->num, numshift, &r->num);
  805. X    else
  806. X        zcopy(q->num, &r->num);
  807. X    if (denshift)
  808. X        zshift(q->den, denshift, &r->den);
  809. X    else
  810. X        zcopy(q->den, &r->den);
  811. X    return r;
  812. X}
  813. X
  814. X
  815. X/*
  816. X * Return the minimum of two numbers.
  817. X */
  818. XNUMBER *
  819. Xqmin(q1, q2)
  820. X    NUMBER *q1, *q2;
  821. X{
  822. X    if (qrel(q1, q2) > 0)
  823. X        q1 = q2;
  824. X    return qlink(q1);
  825. X}
  826. X
  827. X
  828. X/*
  829. X * Return the maximum of two numbers.
  830. X */
  831. XNUMBER *
  832. Xqmax(q1, q2)
  833. X    NUMBER *q1, *q2;
  834. X{
  835. X    if (qrel(q1, q2) < 0)
  836. X        q1 = q2;
  837. X    return qlink(q1);
  838. X}
  839. X
  840. X
  841. X/*
  842. X * Perform the logical OR of two integers.
  843. X */
  844. XNUMBER *
  845. Xqor(q1, q2)
  846. X    NUMBER *q1, *q2;
  847. X{
  848. X    register NUMBER *r;
  849. X
  850. X    if (qisfrac(q1) || qisfrac(q2))
  851. X        error("Non-integers for logical or");
  852. X    if ((q1 == q2) || qiszero(q2))
  853. X        return qlink(q1);
  854. X    if (qiszero(q1))
  855. X        return qlink(q2);
  856. X    r = qalloc();
  857. X    zor(q1->num, q2->num, &r->num);
  858. X    return r;
  859. X}
  860. X
  861. X
  862. X/*
  863. X * Perform the logical AND of two integers.
  864. X */
  865. XNUMBER *
  866. Xqand(q1, q2)
  867. X    NUMBER *q1, *q2;
  868. X{
  869. X    register NUMBER *r;
  870. X    ZVALUE res;
  871. X
  872. X    if (qisfrac(q1) || qisfrac(q2))
  873. X        error("Non-integers for logical and");
  874. X    if (q1 == q2)
  875. X        return qlink(q1);
  876. X    if (qiszero(q1) || qiszero(q2))
  877. X        return qlink(&_qzero_);
  878. X    zand(q1->num, q2->num, &res);
  879. X    if (iszero(res)) {
  880. X        freeh(res.v);
  881. X        return qlink(&_qzero_);
  882. X    }
  883. X    r = qalloc();
  884. X    r->num = res;
  885. X    return r;
  886. X}
  887. X
  888. X
  889. X/*
  890. X * Perform the logical XOR of two integers.
  891. X */
  892. XNUMBER *
  893. Xqxor(q1, q2)
  894. X    NUMBER *q1, *q2;
  895. X{
  896. X    register NUMBER *r;
  897. X    ZVALUE res;
  898. X
  899. X    if (qisfrac(q1) || qisfrac(q2))
  900. X        error("Non-integers for logical xor");
  901. X    if (q1 == q2)
  902. X        return qlink(&_qzero_);
  903. X    if (qiszero(q1))
  904. X        return qlink(q2);
  905. X    if (qiszero(q2))
  906. X        return qlink(q1);
  907. X    zxor(q1->num, q2->num, &res);
  908. X    if (iszero(res)) {
  909. X        freeh(res.v);
  910. X        return qlink(&_qzero_);
  911. X    }
  912. X    r = qalloc();
  913. X    r->num = res;
  914. X    return r;
  915. X}
  916. X
  917. X
  918. X#if 0
  919. X/*
  920. X * Return the number whose binary representation only has the specified
  921. X * bit set (counting from zero).  This thus produces a given power of two.
  922. X */
  923. XNUMBER *
  924. Xqbitvalue(n)
  925. X    long n;
  926. X{
  927. X    register NUMBER *r;
  928. X
  929. X    if (n <= 0)
  930. X        return qlink(&_qone_);
  931. X    r = qalloc();
  932. X    zbitvalue(n, &r->num);
  933. X    return r;
  934. X}
  935. X
  936. X
  937. X/*
  938. X * Test to see if the specified bit of a number is on (counted from zero).
  939. X * Returns TRUE if the bit is set, or FALSE if it is not.
  940. X *    i = qbittest(q, n);
  941. X */
  942. XBOOL
  943. Xqbittest(q, n)
  944. X    register NUMBER *q;
  945. X    long n;
  946. X{
  947. X    int x, y;
  948. X
  949. X    if ((n < 0) || (n >= (q->num.len * BASEB)))
  950. X        return FALSE;
  951. X    x = q->num.v[n / BASEB];
  952. X    y = (1 << (n % BASEB));
  953. X    return ((x & y) != 0);
  954. X}
  955. X#endif
  956. X
  957. X
  958. X/*
  959. X * Return the precision of a number (usually for examining an epsilon value).
  960. X * This is the largest power of two whose reciprocal is not smaller in absolute
  961. X * value than the specified number.  For example, qbitprec(1/100) = 6.
  962. X * Numbers larger than one have a precision of zero.
  963. X */
  964. Xlong
  965. Xqprecision(q)
  966. X    NUMBER *q;
  967. X{
  968. X    long r;
  969. X
  970. X    if (qisint(q))
  971. X        return 0;
  972. X    if (isunit(q->num))
  973. X        return zhighbit(q->den);
  974. X    r = zhighbit(q->den) - zhighbit(q->num) - 1;
  975. X    if (r < 0)
  976. X        r = 0;
  977. X    return r;
  978. X}
  979. X
  980. X
  981. X#if 0
  982. X/*
  983. X * Return an integer indicating the sign of a number (-1, 0, or 1).
  984. X *    i = qtst(q);
  985. X */
  986. XFLAG
  987. Xqtest(q)
  988. X    register NUMBER *q;
  989. X{
  990. X    if (!ztest(q->num))
  991. X        return 0;
  992. X    if (q->num.sign)
  993. X        return -1;
  994. X    return 1;
  995. X}
  996. X#endif
  997. X
  998. X
  999. X/*
  1000. X * Compare two numbers and return an integer indicating their relative size.
  1001. X *    i = qrel(q1, q2);
  1002. X */
  1003. XFLAG
  1004. Xqrel(q1, q2)
  1005. X    register NUMBER *q1, *q2;
  1006. X{
  1007. X    ZVALUE z1, z2;
  1008. X    long wc1, wc2;
  1009. X    int sign;
  1010. X    int z1f = 0, z2f = 0;
  1011. X
  1012. X    if (q1 == q2)
  1013. X        return 0;
  1014. X    sign = q2->num.sign - q1->num.sign;
  1015. X    if (sign)
  1016. X        return sign;
  1017. X    if (qiszero(q2))
  1018. X        return !qiszero(q1);
  1019. X    if (qiszero(q1))
  1020. X        return -1;
  1021. X    /*
  1022. X     * Make a quick comparison by calculating the number of words resulting as
  1023. X     * if we multiplied through by the denominators, and then comparing the
  1024. X     * word counts.
  1025. X     */
  1026. X    sign = 1;
  1027. X    if (qisneg(q1))
  1028. X        sign = -1;
  1029. X    wc1 = q1->num.len + q2->den.len;
  1030. X    wc2 = q2->num.len + q1->den.len;
  1031. X    if (wc1 < wc2 - 1)
  1032. X        return -sign;
  1033. X    if (wc2 < wc1 - 1)
  1034. X        return sign;
  1035. X    /*
  1036. X     * Quick check failed, must actually do the full comparison.
  1037. X     */
  1038. X    if (isunit(q2->den))
  1039. X        z1 = q1->num;
  1040. X    else if (isone(q1->num))
  1041. X        z1 = q2->den;
  1042. X    else {
  1043. X        z1f = 1;
  1044. X        zmul(q1->num, q2->den, &z1);
  1045. X    }
  1046. X    if (isunit(q1->den))
  1047. X        z2 = q2->num;
  1048. X    else if (isone(q2->num))
  1049. X        z2 = q1->den;
  1050. X    else {
  1051. X        z2f = 1;
  1052. X        zmul(q2->num, q1->den, &z2);
  1053. X    }
  1054. X    sign = zrel(z1, z2);
  1055. X    if (z1f)
  1056. X        freeh(z1.v);
  1057. X    if (z2f)
  1058. X        freeh(z2.v);
  1059. X    return sign;
  1060. X}
  1061. X
  1062. X
  1063. X/*
  1064. X * Compare two numbers to see if they are equal.
  1065. X * This differs from qrel in that the numbers are not ordered.
  1066. X * Returns TRUE if they differ.
  1067. X */
  1068. XBOOL
  1069. Xqcmp(q1, q2)
  1070. X    register NUMBER *q1, *q2;
  1071. X{
  1072. X    if (q1 == q2)
  1073. X        return FALSE;
  1074. X    if ((q1->num.sign != q2->num.sign) || (q1->num.len != q2->num.len) ||
  1075. X        (q2->den.len != q2->den.len) || (*q1->num.v != *q2->num.v) ||
  1076. X        (*q1->den.v != *q2->den.v))
  1077. X            return TRUE;
  1078. X    if (zcmp(q1->num, q2->num))
  1079. X        return TRUE;
  1080. X    if (qisint(q1))
  1081. X        return FALSE;
  1082. X    return zcmp(q1->den, q2->den);
  1083. X}
  1084. X
  1085. X
  1086. X/*
  1087. X * Compare a number against a normal small integer.
  1088. X * Returns 1, 0, or -1, according to whether the first number is greater,
  1089. X * equal, or less than the second number.
  1090. X *    n = qreli(q, n);
  1091. X */
  1092. XFLAG
  1093. Xqreli(q, n)
  1094. X    NUMBER *q;
  1095. X    long n;
  1096. X{
  1097. X    int sign;
  1098. X    ZVALUE num;
  1099. X    HALF h2[2];
  1100. X    NUMBER q2;
  1101. X
  1102. X    sign = ztest(q->num);        /* do trivial sign checks */
  1103. X    if (sign == 0) {
  1104. X        if (n > 0)
  1105. X            return -1;
  1106. X        return (n < 0);
  1107. X    }
  1108. X    if ((sign < 0) && (n >= 0))
  1109. X        return -1;
  1110. X    if ((sign > 0) && (n <= 0))
  1111. X        return 1;
  1112. X    n *= sign;
  1113. X    if (n == 1) {            /* quick check against 1 or -1 */
  1114. X        num = q->num;
  1115. X        num.sign = 0;
  1116. X        return (sign * zrel(num, q->den));
  1117. X    }
  1118. X    num.sign = (sign < 0);
  1119. X    num.len = 1 + (n >= BASE);
  1120. X    num.v = h2;
  1121. X    h2[0] = (n & BASE1);
  1122. X    h2[1] = (n >> BASEB);
  1123. X    if (isunit(q->den))    /* integer compare if no denominator */
  1124. X        return zrel(q->num, num);
  1125. X    q2.num = num;
  1126. X    q2.den = _one_;
  1127. X    q2.links = 1;
  1128. X    return qrel(q, &q2);    /* full fractional compare */
  1129. X}
  1130. X
  1131. X
  1132. X/*
  1133. X * Compare a number against a small integer to see if they are equal.
  1134. X * Returns TRUE if they differ.
  1135. X */
  1136. XBOOL
  1137. Xqcmpi(q, n)
  1138. X    NUMBER *q;
  1139. X    long n;
  1140. X{
  1141. X    long len;
  1142. X
  1143. X    len = q->num.len;
  1144. X    if ((len > 2) || qisfrac(q) || (q->num.sign != (n < 0)))
  1145. X        return TRUE;
  1146. X    if (n < 0)
  1147. X        n = -n;
  1148. X    if (((HALF)(n)) != q->num.v[0])
  1149. X        return TRUE;
  1150. X    n = ((FULL) n) >> BASEB;
  1151. X    return (((n != 0) != (len == 2)) || (n != q->num.v[1]));
  1152. X}
  1153. X
  1154. X
  1155. X/*
  1156. X * Number node allocation routines
  1157. X */
  1158. X
  1159. X#define    NNALLOC    1000
  1160. X
  1161. Xunion allocNode {
  1162. X    NUMBER    num;
  1163. X    union allocNode    *link;
  1164. X};
  1165. X
  1166. Xstatic union allocNode    *freeNum;
  1167. X
  1168. X
  1169. XNUMBER *
  1170. Xqalloc()
  1171. X{
  1172. X    register union allocNode *temp;
  1173. X
  1174. X    if (!freeNum) {
  1175. X        freeNum = (union allocNode *)
  1176. X        malloc(sizeof (NUMBER) * NNALLOC);
  1177. X        if (freeNum == NULL)
  1178. X            error(memmsg);
  1179. X        temp = freeNum;
  1180. X        while (temp != freeNum + NNALLOC - 2) {
  1181. X            temp->link = temp+1;
  1182. X            ++temp;
  1183. X        }
  1184. X    }
  1185. X    temp = freeNum;
  1186. X    freeNum = temp->link;
  1187. X    temp->num.links = 1;
  1188. X    temp->num.num = _one_;
  1189. X    temp->num.den = _one_;
  1190. X    return &temp->num;
  1191. X}
  1192. X
  1193. X
  1194. Xvoid
  1195. Xqfreenum(q)
  1196. X    register NUMBER *q;
  1197. X{
  1198. X    union allocNode *a;
  1199. X
  1200. X    if (q == NULL)
  1201. X        return;
  1202. X    freeh(q->num.v);
  1203. X    freeh(q->den.v);
  1204. X    a = (union allocNode *) q;
  1205. X    a->link = freeNum;
  1206. X    freeNum = a;
  1207. X}
  1208. X
  1209. X/* END CODE */
  1210. END_OF_FILE
  1211. if test 20144 -ne `wc -c <'qmath.c'`; then
  1212.     echo shar: \"'qmath.c'\" unpacked with wrong size!
  1213. fi
  1214. # end of 'qmath.c'
  1215. fi
  1216. if test -f 'qtrans.c' -a "${1}" != "-c" ; then 
  1217.   echo shar: Will not clobber existing file \"'qtrans.c'\"
  1218. else
  1219. echo shar: Extracting \"'qtrans.c'\" \(20565 characters\)
  1220. sed "s/^X//" >'qtrans.c' <<'END_OF_FILE'
  1221. X/*
  1222. X * Copyright (c) 1992 David I. Bell
  1223. X * Permission is granted to use, distribute, or modify this source,
  1224. X * provided that this copyright notice remains intact.
  1225. X *
  1226. X * Transcendental functions for real numbers.
  1227. X * These are sin, cos, exp, ln, power, cosh, sinh.
  1228. X */
  1229. X
  1230. X#include "math.h"
  1231. X
  1232. XBOOL _sinisneg_;    /* whether sin(x) < 0 (set by cos(x)) */
  1233. X
  1234. X
  1235. X/*
  1236. X * Calculate the cosine of a number with an accuracy within epsilon.
  1237. X * This also saves the sign of the corresponding sin function.
  1238. X */
  1239. XNUMBER *
  1240. Xqcos(q, epsilon)
  1241. X    NUMBER *q, *epsilon;
  1242. X{
  1243. X    NUMBER *term, *sum, *qsq, *epsilon2, *tmp;
  1244. X    FULL n, i;
  1245. X    long scale, bits, bits2;
  1246. X
  1247. X    _sinisneg_ = qisneg(q);
  1248. X    if (qisneg(epsilon) || qiszero(epsilon))
  1249. X        error("Illegal epsilon value for cosine");
  1250. X    if (qiszero(q))
  1251. X        return qlink(&_qone_);
  1252. X    bits = qprecision(epsilon) + 1;
  1253. X    epsilon = qscale(epsilon, -4L);
  1254. X    /*
  1255. X     * If the argument is larger than one, then divide it by a power of two
  1256. X     * so that it is one or less.  This will make the series converge quickly.
  1257. X     * We will extrapolate the result for the original argument afterwards.
  1258. X     */
  1259. X    scale = zhighbit(q->num) - zhighbit(q->den) + 1;
  1260. X    if (scale < 0)
  1261. X        scale = 0;
  1262. X    if (scale > 0) {
  1263. X        q = qscale(q, -scale);
  1264. X        tmp = qscale(epsilon, -scale);
  1265. X        qfree(epsilon);
  1266. X        epsilon = tmp;
  1267. X    }
  1268. X    epsilon2 = qscale(epsilon, -4L);
  1269. X    qfree(epsilon);
  1270. X    bits2 = qprecision(epsilon2) + 10;
  1271. X    /*
  1272. X     * Now use the Taylor series expansion to calculate the cosine.
  1273. X     * Keep using approximations so that the fractions don't get too large.
  1274. X     */
  1275. X    qsq = qsquare(q);
  1276. X    if (scale > 0)
  1277. X        qfree(q);
  1278. X    term = qlink(&_qone_);
  1279. X    sum = qlink(&_qone_);
  1280. X    n = 0;
  1281. X    while (qrel(term, epsilon2) > 0) {
  1282. X        i = ++n;
  1283. X        i *= ++n;
  1284. X        tmp = qmul(term, qsq);
  1285. X        qfree(term);
  1286. X        term = qdivi(tmp, (long) i);
  1287. X        qfree(tmp);
  1288. X        tmp = qbround(term, bits2);
  1289. X        qfree(term);
  1290. X        term = tmp;
  1291. X        if (n & 2)
  1292. X            tmp = qsub(sum, term);
  1293. X        else
  1294. X            tmp = qadd(sum, term);
  1295. X        qfree(sum);
  1296. X        sum = qbround(tmp, bits2);
  1297. X        qfree(tmp);
  1298. X    }
  1299. X    qfree(term);
  1300. X    qfree(qsq);
  1301. X    qfree(epsilon2);
  1302. X    /*
  1303. X     * Now scale back up to the original value of x by using the formula:
  1304. X     *    cos(2 * x) = 2 * (cos(x) ^ 2) - 1.
  1305. X     */
  1306. X    while (--scale >= 0) {
  1307. X        if (qisneg(sum))
  1308. X            _sinisneg_ = !_sinisneg_;
  1309. X        tmp = qsquare(sum);
  1310. X        qfree(sum);
  1311. X        sum = qscale(tmp, 1L);
  1312. X        qfree(tmp);
  1313. X        tmp = qdec(sum);
  1314. X        qfree(sum);
  1315. X        sum = qbround(tmp, bits2);
  1316. X        qfree(tmp);
  1317. X    }
  1318. X    tmp = qbround(sum, bits);
  1319. X    qfree(sum);
  1320. X    return tmp;
  1321. X}
  1322. X
  1323. X
  1324. X/*
  1325. X * Calculate the sine of a number with an accuracy within epsilon.
  1326. X * This is calculated using the formula:
  1327. X *    sin(x)^2 + cos(x)^2 = 1.
  1328. X * The only tricky bit is resolving the sign of the result.
  1329. X * Future: Use sin(3*x) = 3*sin(x) - 4*sin(x)^3.
  1330. X */
  1331. XNUMBER *
  1332. Xqsin(q, epsilon)
  1333. X    NUMBER *q, *epsilon;
  1334. X{
  1335. X    NUMBER *tmp1, *tmp2, *epsilon2;
  1336. X
  1337. X    if (qisneg(epsilon) || qiszero(epsilon))
  1338. X        error("Illegal epsilon value for sine");
  1339. X    if (qiszero(q))
  1340. X        return qlink(q);
  1341. X    epsilon2 = qscale(epsilon, -4L);
  1342. X    tmp1 = qcos(q, epsilon2);
  1343. X    qfree(epsilon2);
  1344. X    tmp2 = qlegtoleg(tmp1, epsilon, _sinisneg_);
  1345. X    qfree(tmp1);
  1346. X    return tmp2;
  1347. X}
  1348. X
  1349. X
  1350. X/*
  1351. X * Calculate the tangent function.
  1352. X */
  1353. XNUMBER *
  1354. Xqtan(q, epsilon)
  1355. X    NUMBER *q, *epsilon;
  1356. X{
  1357. X    NUMBER *cosval, *sinval, *epsilon2, *tmp, *res;
  1358. X
  1359. X    if (qisneg(epsilon) || qiszero(epsilon))
  1360. X        error("Illegal epsilon value for tangent");
  1361. X    if (qiszero(q))
  1362. X        return qlink(q);
  1363. X    epsilon2 = qscale(epsilon, -4L);
  1364. X    cosval = qcos(q, epsilon2);
  1365. X    sinval = qlegtoleg(cosval, epsilon2, _sinisneg_);
  1366. X    qfree(epsilon2);
  1367. X    tmp = qdiv(sinval, cosval);
  1368. X    qfree(cosval);
  1369. X    qfree(sinval);
  1370. X    res = qbround(tmp, qprecision(epsilon) + 1);
  1371. X    qfree(tmp);
  1372. X    return res;
  1373. X}
  1374. X
  1375. X
  1376. X/*
  1377. X * Calculate the arcsine function.
  1378. X * The result is in the range -pi/2 to pi/2.
  1379. X */
  1380. XNUMBER *
  1381. Xqasin(q, epsilon)
  1382. X    NUMBER *q, *epsilon;
  1383. X{
  1384. X    NUMBER *sum, *term, *epsilon2, *qsq, *tmp;
  1385. X    FULL n, i;
  1386. X    long bits, bits2;
  1387. X    int neg;
  1388. X    NUMBER mulnum;
  1389. X    HALF numval[2];
  1390. X    HALF denval[2];
  1391. X
  1392. X    if (qisneg(epsilon) || qiszero(epsilon))
  1393. X        error("Illegal epsilon value for arcsine");
  1394. X    if (qiszero(q))
  1395. X        return qlink(&_qzero_);
  1396. X    if ((qrel(q, &_qone_) > 0) || (qrel(q, &_qnegone_) < 0))
  1397. X        error("Argument too large for asin");
  1398. X    neg = qisneg(q);
  1399. X    q = qabs(q);
  1400. X    epsilon = qscale(epsilon, -4L);
  1401. X    epsilon2 = qscale(epsilon, -4L);
  1402. X    mulnum.num.sign = 0;
  1403. X    mulnum.num.len = 1;
  1404. X    mulnum.num.v = numval;
  1405. X    mulnum.den.sign = 0;
  1406. X    mulnum.den.len = 1;
  1407. X    mulnum.den.v = denval;
  1408. X    /*
  1409. X     * If the argument is too near one (we use .5) then reduce the
  1410. X     * argument to a more accurate range using the formula:
  1411. X     *    asin(x) = 2 * asin(sqrt((1 - sqrt(1 - x^2)) / 2)).
  1412. X     */
  1413. X    if (qrel(q, &_qonehalf_) > 0) {
  1414. X        sum = qlegtoleg(q, epsilon2, FALSE);
  1415. X        qfree(q);
  1416. X        tmp = qsub(&_qone_, sum);
  1417. X        qfree(sum);
  1418. X        sum = qscale(tmp, -1L);
  1419. X        qfree(tmp);
  1420. X        tmp = qsqrt(sum, epsilon2);
  1421. X        qfree(sum);
  1422. X        qfree(epsilon2);
  1423. X        sum = qasin(tmp, epsilon);
  1424. X        qfree(tmp);
  1425. X        qfree(epsilon);
  1426. X        tmp = qscale(sum, 1L);
  1427. X        qfree(sum);
  1428. X        if (neg) {
  1429. X            sum = qneg(tmp);
  1430. X            qfree(tmp);
  1431. X            tmp = sum;
  1432. X        }
  1433. X        return tmp;
  1434. X    }
  1435. X    /*
  1436. X     * Argument is between zero and .5, so use the series.
  1437. X     */
  1438. X    epsilon = qscale(epsilon, -4L);
  1439. X    epsilon2 = qscale(epsilon, -4L);
  1440. X    bits = qprecision(epsilon) + 1;
  1441. X    bits2 = bits + 10;
  1442. X    sum = qlink(q);
  1443. X    term = qlink(q);
  1444. X    qsq = qsquare(q);
  1445. X    qfree(q);
  1446. X    n = 1;
  1447. X    while (qrel(term, epsilon2) > 0) {
  1448. X        i = n * n;
  1449. X        numval[0] = i & BASE1;
  1450. X        if (i >= BASE) {
  1451. X            numval[1] = i / BASE;
  1452. X            mulnum.den.len = 2;
  1453. X        }
  1454. X        i = (n + 1) * (n + 2);
  1455. X        denval[0] = i & BASE1;
  1456. X        if (i >= BASE) {
  1457. X            denval[1] = i / BASE;
  1458. X            mulnum.den.len = 2;
  1459. X        }
  1460. X        tmp = qmul(term, qsq);
  1461. X        qfree(term);
  1462. X        term = qmul(tmp, &mulnum);
  1463. X        qfree(tmp);
  1464. X        tmp = qbround(term, bits2);
  1465. X        qfree(term);
  1466. X        term = tmp;
  1467. X        tmp = qadd(sum, term);
  1468. X        qfree(sum);
  1469. X        sum = qbround(tmp, bits2);
  1470. X        qfree(tmp);
  1471. X        n += 2;
  1472. X    }
  1473. X    qfree(epsilon);
  1474. X    qfree(epsilon2);
  1475. X    qfree(term);
  1476. X    qfree(qsq);
  1477. X    tmp = qbround(sum, bits);
  1478. X    qfree(sum);
  1479. X    if (neg) {
  1480. X        term = qneg(tmp);
  1481. X        qfree(tmp);
  1482. X        tmp = term;
  1483. X    }
  1484. X    return tmp;
  1485. X}
  1486. X
  1487. X
  1488. X/*
  1489. X * Calculate the acos function.
  1490. X * The result is in the range 0 to pi.
  1491. X */
  1492. XNUMBER *
  1493. Xqacos(q, epsilon)
  1494. X    NUMBER *q, *epsilon;
  1495. X{
  1496. X    NUMBER *tmp1, *tmp2, *epsilon2;
  1497. X
  1498. X    if (qisneg(epsilon) || qiszero(epsilon))
  1499. X        error("Illegal epsilon value for arccosine");
  1500. X    if (qisone(q))
  1501. X        return qlink(&_qzero_);
  1502. X    if ((qrel(q, &_qone_) > 0) || (qrel(q, &_qnegone_) < 0))
  1503. X        error("Argument too large for acos");
  1504. X    /*
  1505. X     * Calculate the result using the formula:
  1506. X     *    acos(x) = asin(sqrt(1 - x^2)).
  1507. X     */
  1508. X    epsilon2 = qscale(epsilon, -8L);
  1509. X    tmp1 = qlegtoleg(q, epsilon2, FALSE);
  1510. X    qfree(epsilon2);
  1511. X    tmp2 = qasin(tmp1, epsilon);
  1512. X    qfree(tmp1);
  1513. X    return tmp2;
  1514. X}
  1515. X
  1516. X
  1517. X/*
  1518. X * Calculate the arctangent function with a accuracy less than epsilon.
  1519. X * This uses the formula:
  1520. X *    atan(x) = asin(sqrt(x^2 / (x^2 + 1))).
  1521. X */
  1522. XNUMBER *
  1523. Xqatan(q, epsilon)
  1524. X    NUMBER *q, *epsilon;
  1525. X{
  1526. X    NUMBER *tmp1, *tmp2, *tmp3, *epsilon2;
  1527. X
  1528. X    if (qisneg(epsilon) || qiszero(epsilon))
  1529. X        error("Illegal epsilon value for arctangent");
  1530. X    if (qiszero(q))
  1531. X        return qlink(&_qzero_);
  1532. X    tmp1 = qsquare(q);
  1533. X    tmp2 = qinc(tmp1);
  1534. X    tmp3 = qdiv(tmp1, tmp2);
  1535. X    qfree(tmp1);
  1536. X    qfree(tmp2);
  1537. X    epsilon2 = qscale(epsilon, -8L);
  1538. X    tmp1 = qsqrt(tmp3, epsilon2);
  1539. X    qfree(epsilon2);
  1540. X    qfree(tmp3);
  1541. X    tmp2 = qasin(tmp1, epsilon);
  1542. X    qfree(tmp1);
  1543. X    if (qisneg(q)) {
  1544. X        tmp1 = qneg(tmp2);
  1545. X        qfree(tmp2);
  1546. X        tmp2 = tmp1;
  1547. X    }
  1548. X    return tmp2;
  1549. X}
  1550. X
  1551. X
  1552. X/*
  1553. X * Calculate the angle which is determined by the point (x,y).
  1554. X * This is the same as arctan for non-negative x, but gives the correct
  1555. X * value for negative x.  By convention, y is the first argument.
  1556. X * For example, qatan2(1, -1) = 3/4 * pi.
  1557. X */
  1558. XNUMBER *
  1559. Xqatan2(qy, qx, epsilon)
  1560. X    NUMBER *qy, *qx, *epsilon;
  1561. X{
  1562. X    NUMBER *tmp1, *tmp2, *epsilon2;
  1563. X
  1564. X    if (qisneg(epsilon) || qiszero(epsilon))
  1565. X        error("Illegal epsilon value for atan2");
  1566. X    if (qiszero(qy) && qiszero(qx))
  1567. X        error("Zero coordinates for atan2");
  1568. X    /*
  1569. X     * If the point is on the negative real axis, then the answer is pi.
  1570. X     */
  1571. X    if (qiszero(qy) && qisneg(qx))
  1572. X        return qpi(epsilon);
  1573. X    /*
  1574. X     * If the point is in the right half plane, then use the normal atan.
  1575. X     */
  1576. X    if (!qisneg(qx) && !qiszero(qx)) {
  1577. X        if (qiszero(qy))
  1578. X            return qlink(&_qzero_);
  1579. X        tmp1 = qdiv(qy, qx);
  1580. X        tmp2 = qatan(tmp1, epsilon);
  1581. X        qfree(tmp1);
  1582. X        return tmp2;
  1583. X    }
  1584. X    /*
  1585. X     * The point is in the left half plane.  Calculate the angle by finding
  1586. X     * the atan of half the angle using the formula:
  1587. X     *    atan2(y,x) = 2 * atan((sqrt(x^2 + y^2) - x) / y).
  1588. X     */
  1589. X    epsilon2 = qscale(epsilon, -4L);
  1590. X    tmp1 = qhypot(qx, qy, epsilon2);
  1591. X    tmp2 = qsub(tmp1, qx);
  1592. X    qfree(tmp1);
  1593. X    tmp1 = qdiv(tmp2, qy);
  1594. X    qfree(tmp2);
  1595. X    tmp2 = qatan(tmp1, epsilon2);
  1596. X    qfree(tmp1);
  1597. X    qfree(epsilon2);
  1598. X    tmp1 = qscale(tmp2, 1L);
  1599. X    qfree(tmp2);
  1600. X    return tmp1;
  1601. X}
  1602. X
  1603. X
  1604. X/*
  1605. X * Calculate the value of pi to within the required epsilon.
  1606. X * This uses the following formula which only needs integer calculations
  1607. X * except for the final operation:
  1608. X *    pi = 1 / SUMOF(comb(2 * N, N) ^ 3 * (42 * N + 5) / 2 ^ (12 * N + 4)),
  1609. X * where the summation runs from N=0.  This formula gives about 6 bits of
  1610. X * accuracy per term.  Since the denominator for each term is a power of two,
  1611. X * we can simply use shifts to sum the terms.  The combinatorial numbers
  1612. X * in the formula are calculated recursively using the formula:
  1613. X *    comb(2*(N+1), N+1) = 2 * comb(2 * N, N) * (2 * N + 1) / N.
  1614. X */
  1615. XNUMBER *
  1616. Xqpi(epsilon)
  1617. X    NUMBER *epsilon;
  1618. X{
  1619. X    ZVALUE comb;            /* current combinatorial value */
  1620. X    ZVALUE sum;            /* current sum */
  1621. X    ZVALUE tmp1, tmp2;
  1622. X    NUMBER *r, *t1, qtmp;
  1623. X    long shift;            /* current shift of result */
  1624. X    long N;                /* current term number */
  1625. X    long bits;            /* needed number of bits of precision */
  1626. X    long t;
  1627. X
  1628. X    if (qiszero(epsilon) || qisneg(epsilon))
  1629. X        error("Bad epsilon value for pi");
  1630. X    bits = qprecision(epsilon) + 4;
  1631. X    comb = _one_;
  1632. X    itoz(5L, &sum);
  1633. X    N = 0;
  1634. X    shift = 4;
  1635. X    do {
  1636. X        t = 1 + (++N & 0x1);
  1637. X        (void) zdivi(comb, N / (3 - t), &tmp1);
  1638. X        freeh(comb.v);
  1639. X        zmuli(tmp1, t * (2 * N - 1), &comb);
  1640. X        freeh(tmp1.v);
  1641. X        zsquare(comb, &tmp1);
  1642. X        zmul(comb, tmp1, &tmp2);
  1643. X        freeh(tmp1.v);
  1644. X        zmuli(tmp2, 42 * N + 5, &tmp1);
  1645. X        freeh(tmp2.v);
  1646. X        zshift(sum, 12L, &tmp2);
  1647. X        freeh(sum.v);
  1648. X        zadd(tmp1, tmp2, &sum);
  1649. X        t = zhighbit(tmp1);
  1650. X        freeh(tmp1.v);
  1651. X        freeh(tmp2.v);
  1652. X        shift += 12;
  1653. X    } while ((shift - t) < bits);
  1654. X    qtmp.num = _one_;
  1655. X    qtmp.den = sum;
  1656. X    t1 = qscale(&qtmp, shift);
  1657. X    freeh(sum.v);
  1658. X    r = qbround(t1, bits);
  1659. X    qfree(t1);
  1660. X    return r;
  1661. X}
  1662. X
  1663. X
  1664. X/*
  1665. X * Calculate the exponential function with a relative accuracy less than
  1666. X * epsilon.
  1667. X */
  1668. XNUMBER *
  1669. Xqexp(q, epsilon)
  1670. X    NUMBER *q, *epsilon;
  1671. X{
  1672. X    long scale;
  1673. X    FULL n;
  1674. X    long bits, bits2;
  1675. X    NUMBER *sum, *term, *qs, *epsilon2, *tmp;
  1676. X
  1677. X    if (qisneg(epsilon) || qiszero(epsilon))
  1678. X        error("Illegal epsilon value for exp");
  1679. X    if (qiszero(q))
  1680. X        return qlink(&_qone_);
  1681. X    epsilon = qscale(epsilon, -4L);
  1682. X    /*
  1683. X     * If the argument is larger than one, then divide it by a power of two
  1684. X     * so that it is one or less.  This will make the series converge quickly.
  1685. X     * We will extrapolate the result for the original argument afterwards.
  1686. X     * Also make the argument non-negative.
  1687. X     */
  1688. X    qs = qabs(q);
  1689. X    scale = zhighbit(q->num) - zhighbit(q->den) + 1;
  1690. X    if (scale < 0)
  1691. X        scale = 0;
  1692. X    if (scale > 0) {
  1693. X        if (scale > 100000)
  1694. X            error("Very large argument for exp");
  1695. X        tmp = qscale(qs, -scale);
  1696. X        qfree(qs);
  1697. X        qs = tmp;
  1698. X        tmp = qscale(epsilon, -scale);
  1699. X        qfree(epsilon);
  1700. X        epsilon = tmp;
  1701. X    }
  1702. X    epsilon2 = qscale(epsilon, -4L);
  1703. X    bits = qprecision(epsilon) + 1;
  1704. X    bits2 = bits + 10;
  1705. X    qfree(epsilon);
  1706. X    /*
  1707. X     * Now use the Taylor series expansion to calculate the exponential.
  1708. X     * Keep using approximations so that the fractions don't get too large.
  1709. X     */
  1710. X    sum = qlink(&_qone_);
  1711. X    term = qlink(&_qone_);
  1712. X    n = 0;
  1713. X    while (qrel(term, epsilon2) > 0) {
  1714. X        n++;
  1715. X        tmp = qmul(term, qs);
  1716. X        qfree(term);
  1717. X        term = qdivi(tmp, (long) n);
  1718. X        qfree(tmp);
  1719. X        tmp = qbround(term, bits2);
  1720. X        qfree(term);
  1721. X        term = tmp;
  1722. X        tmp = qadd(sum, term);
  1723. X        qfree(sum);
  1724. X        sum = qbround(tmp, bits2);
  1725. X        qfree(tmp);
  1726. X    }
  1727. X    qfree(term);
  1728. X    qfree(qs);
  1729. X    qfree(epsilon2);
  1730. X    /*
  1731. X     * Now repeatedly square the answer to get the final result.
  1732. X     * Then invert it if the original argument was negative.
  1733. X     */
  1734. X    while (--scale >= 0) {
  1735. X        tmp = qsquare(sum);
  1736. X        qfree(sum);
  1737. X        sum = qbround(tmp, bits2);
  1738. X        qfree(tmp);
  1739. X    }
  1740. X    tmp = qbround(sum, bits);
  1741. X    qfree(sum);
  1742. X    if (qisneg(q)) {
  1743. X        sum = qinv(tmp);
  1744. X        qfree(tmp);
  1745. X        tmp = sum;
  1746. X    }
  1747. X    return tmp;
  1748. X}
  1749. X
  1750. X
  1751. X/*
  1752. X * Calculate the natural logarithm of a number accurate to the specified
  1753. X * epsilon.
  1754. X */
  1755. XNUMBER *
  1756. Xqln(q, epsilon)
  1757. X    NUMBER *q, *epsilon;
  1758. X{
  1759. X    NUMBER *term, *term2, *sum, *epsilon2, *tmp1, *tmp2;
  1760. X    NUMBER *minr, *maxr;
  1761. X    long shift, bits, bits2;
  1762. X    int neg;
  1763. X    FULL n;
  1764. X
  1765. X    if (qisneg(q) || qiszero(q))
  1766. X        error("log of non-positive number");
  1767. X    if (qisneg(epsilon) || qiszero(epsilon))
  1768. X        error("Illegal epsilon for ln");
  1769. X    epsilon = qscale(epsilon, -4L);
  1770. X    epsilon2 = qscale(epsilon, -4L);
  1771. X    bits = qprecision(epsilon) + 1;
  1772. X    bits2 = bits + 10;
  1773. X    qfree(epsilon);
  1774. X    /*
  1775. X     * Scale down the logarithm to the convergent range by taking square
  1776. X     * roots.  The result will be modified to get the final value later.
  1777. X     */
  1778. X    q = qlink(q);
  1779. X    minr = iitoq(7L, 10L);
  1780. X    maxr = iitoq(7L, 5L);
  1781. X    shift = 1;
  1782. X    while ((qrel(q, minr) < 0) || (qrel(q, maxr) > 0)) {
  1783. X        tmp1 = qsqrt(q, epsilon2);
  1784. X        qfree(q);
  1785. X        q = tmp1;
  1786. X        shift++;
  1787. X    }
  1788. X    qfree(minr);
  1789. X    qfree(maxr);
  1790. X    /*
  1791. X     * Calculate a value which will always converge using the formula:
  1792. X     *    ln((1+x)/(1-x)) = ln(1+x) - ln(1-x).
  1793. X     */
  1794. X    tmp1 = qdec(q);
  1795. X    tmp2 = qinc(q);
  1796. X    term = qdiv(tmp1, tmp2);
  1797. X    qfree(tmp1);
  1798. X    qfree(tmp2);
  1799. X    qfree(q);
  1800. X    neg = 0;
  1801. X    if (qisneg(term)) {
  1802. X        neg = 1;
  1803. X        tmp1 = qabs(term);
  1804. X        qfree(term);
  1805. X        term = tmp1;
  1806. X    }
  1807. X    /*
  1808. X     * Now use the Taylor series expansion to calculate the result.
  1809. X     */
  1810. X    n = 1;
  1811. X    term2 = qsquare(term);
  1812. X    sum = qlink(term);
  1813. X    while (qrel(term, epsilon2) > 0) {
  1814. X        n += 2;
  1815. X        tmp1 = qmul(term, term2);
  1816. X        qfree(term);
  1817. X        term = qbround(tmp1, bits2);
  1818. X        qfree(tmp1);
  1819. X        tmp1 = qdivi(term, (long) n);
  1820. X        tmp2 = qadd(sum, tmp1);
  1821. X        qfree(tmp1);
  1822. X        qfree(sum);
  1823. X        sum = qbround(tmp2, bits2);
  1824. X    }
  1825. X    qfree(epsilon2);
  1826. X    qfree(term);
  1827. X    qfree(term2);
  1828. X    if (neg) {
  1829. X        tmp1 = qneg(sum);
  1830. X        qfree(sum);
  1831. X        sum = tmp1;
  1832. X    }
  1833. X    /*
  1834. X     * Calculate the final result by multiplying by the proper power of two
  1835. X     * to undo the square roots done at the top.
  1836. X     */
  1837. X    tmp1 = qscale(sum, shift);
  1838. X    qfree(sum);
  1839. X    sum = qbround(tmp1, bits);
  1840. X    qfree(tmp1);
  1841. X    return sum;
  1842. X}
  1843. X
  1844. X
  1845. X/*
  1846. X * Calculate the result of raising one number to the power of another.
  1847. X * The result is calculated to within the specified relative error.
  1848. X */
  1849. XNUMBER *
  1850. Xqpower(q1, q2, epsilon)
  1851. X    NUMBER *q1, *q2, *epsilon;
  1852. X{
  1853. X    NUMBER *tmp1, *tmp2, *epsilon2;
  1854. X
  1855. X    if (qisint(q2))
  1856. X        return qpowi(q1, q2);
  1857. X    epsilon2 = qscale(epsilon, -4L);
  1858. X    tmp1 = qln(q1, epsilon2);
  1859. X    tmp2 = qmul(tmp1, q2);
  1860. X    qfree(tmp1);
  1861. X    tmp1 = qexp(tmp2, epsilon);
  1862. X    qfree(tmp2);
  1863. X    qfree(epsilon2);
  1864. X    return tmp1;
  1865. X}
  1866. X
  1867. X
  1868. X/*
  1869. X * Calculate the Kth root of a number to within the specified accuracy.
  1870. X */
  1871. XNUMBER *
  1872. Xqroot(q1, q2, epsilon)
  1873. X    NUMBER *q1, *q2, *epsilon;
  1874. X{
  1875. X    NUMBER *tmp1, *tmp2, *epsilon2;
  1876. X    int neg;
  1877. X
  1878. X    if (qisneg(q2) || qiszero(q2) || qisfrac(q2))
  1879. X        error("Taking bad root of number");
  1880. X    if (qiszero(q1) || qisone(q1) || qisone(q2))
  1881. X        return qlink(q1);
  1882. X    if (qistwo(q2))
  1883. X        return qsqrt(q1, epsilon);
  1884. X    neg = qisneg(q1);
  1885. X    if (neg) {
  1886. X        if (iseven(q2->num))
  1887. X            error("Taking even root of negative number");
  1888. X        q1 = qabs(q1);
  1889. X    }
  1890. X    epsilon2 = qscale(epsilon, -4L);
  1891. X    tmp1 = qln(q1, epsilon2);
  1892. X    tmp2 = qdiv(tmp1, q2);
  1893. X    qfree(tmp1);
  1894. X    tmp1 = qexp(tmp2, epsilon);
  1895. X    qfree(tmp2);
  1896. X    qfree(epsilon2);
  1897. X    if (neg) {
  1898. X        tmp2 = qneg(tmp1);
  1899. X        qfree(tmp1);
  1900. X        tmp1 = tmp2;
  1901. X    }
  1902. X    return tmp1;
  1903. X}
  1904. X
  1905. X
  1906. X/*
  1907. X * Calculate the hyperbolic cosine function with a relative accuracy less
  1908. X * than epsilon.  This is defined by:
  1909. X *    cosh(x) = (exp(x) + exp(-x)) / 2.
  1910. X */
  1911. XNUMBER *
  1912. Xqcosh(q, epsilon)
  1913. X    NUMBER *q, *epsilon;
  1914. X{
  1915. X    long scale;
  1916. X    FULL n;
  1917. X    FULL m;
  1918. X    long bits, bits2;
  1919. X    NUMBER *sum, *term, *qs, *epsilon2, *tmp;
  1920. X
  1921. X    if (qisneg(epsilon) || qiszero(epsilon))
  1922. X        error("Illegal epsilon value for exp");
  1923. X    if (qiszero(q))
  1924. X        return qlink(&_qone_);
  1925. X    epsilon = qscale(epsilon, -4L);
  1926. X    /*
  1927. X     * If the argument is larger than one, then divide it by a power of two
  1928. X     * so that it is one or less.  This will make the series converge quickly.
  1929. X     * We will extrapolate the result for the original argument afterwards.
  1930. X     */
  1931. X    qs = qabs(q);
  1932. X    scale = zhighbit(q->num) - zhighbit(q->den) + 1;
  1933. X    if (scale < 0)
  1934. X        scale = 0;
  1935. X    if (scale > 0) {
  1936. X        if (scale > 100000)
  1937. X            error("Very large argument for exp");
  1938. X        tmp = qscale(qs, -scale);
  1939. X        qfree(qs);
  1940. X        qs = tmp;
  1941. X        tmp = qscale(epsilon, -scale);
  1942. X        qfree(epsilon);
  1943. X        epsilon = tmp;
  1944. X    }
  1945. X    epsilon2 = qscale(epsilon, -4L);
  1946. X    bits = qprecision(epsilon) + 1;
  1947. X    bits2 = bits + 10;
  1948. X    qfree(epsilon);
  1949. X    tmp = qsquare(qs);
  1950. X    qfree(qs);
  1951. X    qs = tmp;
  1952. X    /*
  1953. X     * Now use the Taylor series expansion to calculate the exponential.
  1954. X     * Keep using approximations so that the fractions don't get too large.
  1955. X     */
  1956. X    sum = qlink(&_qone_);
  1957. X    term = qlink(&_qone_);
  1958. X    n = 0;
  1959. X    while (qrel(term, epsilon2) > 0) {
  1960. X        m = ++n;
  1961. X        m *= ++n;
  1962. X        tmp = qmul(term, qs);
  1963. X        qfree(term);
  1964. X        term = qdivi(tmp, (long) m);
  1965. X        qfree(tmp);
  1966. X        tmp = qbround(term, bits2);
  1967. X        qfree(term);
  1968. X        term = tmp;
  1969. X        tmp = qadd(sum, term);
  1970. X        qfree(sum);
  1971. X        sum = qbround(tmp, bits2);
  1972. X        qfree(tmp);
  1973. X    }
  1974. X    qfree(term);
  1975. X    qfree(qs);
  1976. X    qfree(epsilon2);
  1977. X    /*
  1978. X     * Now bring the number back up into range to get the final result.
  1979. X     * This uses the formula:
  1980. X     *    cosh(2 * x) = 2 * cosh(x)^2 - 1.
  1981. X     */
  1982. X    while (--scale >= 0) {
  1983. X        tmp = qsquare(sum);
  1984. X        qfree(sum);
  1985. X        sum = qscale(tmp, 1L);
  1986. X        qfree(tmp);
  1987. X        tmp = qdec(sum);
  1988. X        qfree(sum);
  1989. X        sum = qbround(tmp, bits2);
  1990. X        qfree(tmp);
  1991. X    }
  1992. X    tmp = qbround(sum, bits);
  1993. X    qfree(sum);
  1994. X    return tmp;
  1995. X}
  1996. X
  1997. X
  1998. X/*
  1999. X * Calculate the hyperbolic sine with an accurary less than epsilon.
  2000. X * This is calculated using the formula:
  2001. X *    cosh(x)^2 - sinh(x)^2 = 1.
  2002. X */
  2003. XNUMBER *
  2004. Xqsinh(q, epsilon)
  2005. X    NUMBER *q, *epsilon;
  2006. X{
  2007. X    NUMBER *tmp1, *tmp2;
  2008. X
  2009. X    if (qisneg(epsilon) || qiszero(epsilon))
  2010. X        error("Illegal epsilon value for sinh");
  2011. X    if (qiszero(q))
  2012. X        return qlink(q);
  2013. X    epsilon = qscale(epsilon, -4L);
  2014. X    tmp1 = qcosh(q, epsilon);
  2015. X    tmp2 = qsquare(tmp1);
  2016. X    qfree(tmp1);
  2017. X    tmp1 = qdec(tmp2);
  2018. X    qfree(tmp2);
  2019. X    tmp2 = qsqrt(tmp1, epsilon);
  2020. X    qfree(tmp1);
  2021. X    if (qisneg(q)) {
  2022. X        tmp1 = qneg(tmp2);
  2023. X        qfree(tmp2);
  2024. X        tmp2 = tmp1;
  2025. X    }
  2026. X    qfree(epsilon);
  2027. X    return tmp2;
  2028. X}
  2029. X
  2030. X
  2031. X/*
  2032. X * Calculate the hyperbolic tangent with an accurary less than epsilon.
  2033. X * This is calculated using the formula:
  2034. X *    tanh(x) = sinh(x) / cosh(x).
  2035. X */
  2036. XNUMBER *
  2037. Xqtanh(q, epsilon)
  2038. X    NUMBER *q, *epsilon;
  2039. X{
  2040. X    NUMBER *tmp1, *tmp2, *coshval;
  2041. X
  2042. X    if (qisneg(epsilon) || qiszero(epsilon))
  2043. X        error("Illegal epsilon value for tanh");
  2044. X    if (qiszero(q))
  2045. X        return qlink(q);
  2046. X    epsilon = qscale(epsilon, -4L);
  2047. X    coshval = qcosh(q, epsilon);
  2048. X    tmp2 = qsquare(coshval);
  2049. X    tmp1 = qdec(tmp2);
  2050. X    qfree(tmp2);
  2051. X    tmp2 = qsqrt(tmp1, epsilon);
  2052. X    qfree(tmp1);
  2053. X    if (qisneg(q)) {
  2054. X        tmp1 = qneg(tmp2);
  2055. X        qfree(tmp2);
  2056. X        tmp2 = tmp1;
  2057. X    }
  2058. X    qfree(epsilon);
  2059. X    tmp1 = qdiv(tmp2, coshval);
  2060. X    qfree(tmp2);
  2061. X    qfree(coshval);
  2062. X    return tmp1;
  2063. X}
  2064. X
  2065. X
  2066. X/*
  2067. X * Compute the hyperbolic arccosine within the specified accuracy.
  2068. X * This is calculated using the formula:
  2069. X *    acosh(x) = ln(x + sqrt(x^2 - 1)).
  2070. X */
  2071. XNUMBER *
  2072. Xqacosh(q, epsilon)
  2073. X    NUMBER *q, *epsilon;
  2074. X{
  2075. X    NUMBER *tmp1, *tmp2, *epsilon2;
  2076. X
  2077. X    if (qisneg(epsilon) || qiszero(epsilon))
  2078. X        error("Illegal epsilon value for acosh");
  2079. X    if (qisone(q))
  2080. X        return qlink(&_qzero_);
  2081. X    if (qreli(q, 1L) < 0)
  2082. X        error("Argument less than one for acosh");
  2083. X    epsilon2 = qscale(epsilon, -8L);
  2084. X    tmp1 = qsquare(q);
  2085. X    tmp2 = qdec(tmp1);
  2086. X    qfree(tmp1);
  2087. X    tmp1 = qsqrt(tmp2, epsilon2);
  2088. X    qfree(tmp2);
  2089. X    tmp2 = qadd(tmp1, q);
  2090. X    qfree(tmp1);
  2091. X    tmp1 = qln(tmp2, epsilon);
  2092. X    qfree(tmp2);
  2093. X    qfree(epsilon2);
  2094. X    return tmp1;
  2095. X}
  2096. X
  2097. X
  2098. X/*
  2099. X * Compute the hyperbolic arcsine within the specified accuracy.
  2100. X * This is calculated using the formula:
  2101. X *    asinh(x) = ln(x + sqrt(x^2 + 1)).
  2102. X */
  2103. XNUMBER *
  2104. Xqasinh(q, epsilon)
  2105. X    NUMBER *q, *epsilon;
  2106. X{
  2107. X    NUMBER *tmp1, *tmp2, *epsilon2;
  2108. X
  2109. X    if (qisneg(epsilon) || qiszero(epsilon))
  2110. X        error("Illegal epsilon value for asinh");
  2111. X    if (qiszero(q))
  2112. X        return qlink(&_qzero_);
  2113. X    epsilon2 = qscale(epsilon, -8L);
  2114. X    tmp1 = qsquare(q);
  2115. X    tmp2 = qinc(tmp1);
  2116. X    qfree(tmp1);
  2117. X    tmp1 = qsqrt(tmp2, epsilon2);
  2118. X    qfree(tmp2);
  2119. X    tmp2 = qadd(tmp1, q);
  2120. X    qfree(tmp1);
  2121. X    tmp1 = qln(tmp2, epsilon);
  2122. X    qfree(tmp2);
  2123. X    qfree(epsilon2);
  2124. X    return tmp1;
  2125. X}
  2126. X
  2127. X
  2128. X/*
  2129. X * Compute the hyperbolic arctangent within the specified accuracy.
  2130. X * This is calculated using the formula:
  2131. X *    atanh(x) = ln((1 + u) / (1 - u)) / 2.
  2132. X */
  2133. XNUMBER *
  2134. Xqatanh(q, epsilon)
  2135. X    NUMBER *q, *epsilon;
  2136. X{
  2137. X    NUMBER *tmp1, *tmp2, *tmp3;
  2138. X
  2139. X    if (qisneg(epsilon) || qiszero(epsilon))
  2140. X        error("Illegal epsilon value for atanh");
  2141. X    if (qiszero(q))
  2142. X        return qlink(&_qzero_);
  2143. X    if ((qreli(q, 1L) > 0) || (qreli(q, -1L) < 0))
  2144. X        error("Argument not between -1 and 1 for atanh");
  2145. X    tmp1 = qinc(q);
  2146. X    tmp2 = qsub(&_qone_, q);
  2147. X    tmp3 = qdiv(tmp1, tmp2);
  2148. X    qfree(tmp1);
  2149. X    qfree(tmp2);
  2150. X    tmp1 = qln(tmp3, epsilon);
  2151. X    qfree(tmp3);
  2152. X    tmp2 = qscale(tmp1, -1L);
  2153. X    qfree(tmp1);
  2154. X    return tmp2;
  2155. X}
  2156. X
  2157. X/* END CODE */
  2158. END_OF_FILE
  2159. if test 20565 -ne `wc -c <'qtrans.c'`; then
  2160.     echo shar: \"'qtrans.c'\" unpacked with wrong size!
  2161. fi
  2162. # end of 'qtrans.c'
  2163. fi
  2164. echo shar: End of archive 9 \(of 21\).
  2165. cp /dev/null ark9isdone
  2166. MISSING=""
  2167. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ; do
  2168.     if test ! -f ark${I}isdone ; then
  2169.     MISSING="${MISSING} ${I}"
  2170.     fi
  2171. done
  2172. if test "${MISSING}" = "" ; then
  2173.     echo You have unpacked all 21 archives.
  2174.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2175. else
  2176.     echo You still need to unpack the following archives:
  2177.     echo "        " ${MISSING}
  2178. fi
  2179. ##  End of shell archive.
  2180. exit 0
  2181.