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

  1. Newsgroups: comp.sources.unix
  2. From: dbell@canb.auug.org.au (David I. Bell)
  3. Subject: v27i135: calc-2.9.0 - arbitrary precision C-like programmable calculator, Part08/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 135
  10. Archive-Name: calc-2.9.0/part08
  11.  
  12. #!/bin/sh
  13. # this is part 8 of a multipart archive
  14. # do not concatenate these parts, unpack them in order with /bin/sh
  15. # file calc2.9.0/opcodes.c continued
  16. #
  17. CurArch=8
  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/opcodes.c"
  28. sed 's/^X//' << 'SHAR_EOF' >> calc2.9.0/opcodes.c
  29. X#endif
  30. X    vsprintf(buf, fmt, ap);
  31. X    va_end(ap);
  32. X    fprintf(stderr, "%s\n", buf);
  33. X    funcname = NULL;
  34. X    longjmp(jmpbuf, 1);
  35. X}
  36. X
  37. X/* END CODE */
  38. SHAR_EOF
  39. echo "File calc2.9.0/opcodes.c is complete"
  40. chmod 0644 calc2.9.0/opcodes.c || echo "restore of calc2.9.0/opcodes.c fails"
  41. set `wc -c calc2.9.0/opcodes.c`;Sum=$1
  42. if test "$Sum" != "52101"
  43. then echo original size 52101, current size $Sum;fi
  44. echo "x - extracting calc2.9.0/opcodes.h (Text)"
  45. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/opcodes.h &&
  46. X/*
  47. X * Copyright (c) 1993 David I. Bell
  48. X * Permission is granted to use, distribute, or modify this source,
  49. X * provided that this copyright notice remains intact.
  50. X */
  51. X
  52. X#ifndef    OPCODES_H
  53. X#define    OPCODES_H
  54. X
  55. X
  56. X/*
  57. X * Opcodes
  58. X */
  59. X#define OP_NOP        0L    /* no operation */
  60. X#define OP_LOCALADDR    1L    /* load address of local variable */
  61. X#define OP_GLOBALADDR    2L    /* load address of global variable */
  62. X#define OP_PARAMADDR    3L    /* load address of paramater variable */
  63. X#define OP_LOCALVALUE    4L    /* load value of local variable */
  64. X#define OP_GLOBALVALUE    5L    /* load value of global variable */
  65. X#define OP_PARAMVALUE    6L    /* load value of paramater variable */
  66. X#define OP_NUMBER    7L    /* load constant real numeric value */
  67. X#define OP_INDEXADDR    8L    /* load array index address */
  68. X#define    OP_PRINTRESULT    9L    /* print result of top-level expression */
  69. X#define OP_ASSIGN    10L    /* assign value to variable */
  70. X#define OP_ADD        11L    /* add top two values */
  71. X#define OP_SUB        12L    /* subtract top two values */
  72. X#define OP_MUL        13L    /* multiply top two values */
  73. X#define OP_DIV        14L    /* divide top two values */
  74. X#define OP_MOD        15L    /* take mod of top two values */
  75. X#define OP_SAVE        16L    /* save value for later use */
  76. X#define OP_NEGATE    17L    /* negate top value */
  77. X#define OP_INVERT    18L    /* invert top value */
  78. X#define OP_INT        19L    /* take integer part of top value */
  79. X#define OP_FRAC        20L    /* take fraction part of top value */
  80. X#define OP_NUMERATOR    21L    /* take numerator of top value */
  81. X#define OP_DENOMINATOR    22L    /* take denominator of top value */
  82. X#define OP_DUPLICATE    23L    /* duplicate top value on stack */
  83. X#define OP_POP        24L    /* pop top value from stack */
  84. X#define OP_RETURN    25L    /* return value of function */
  85. X#define OP_JUMPEQ    26L    /* jump if top value is zero */
  86. X#define OP_JUMPNE    27L    /* jump if top value is nonzero */
  87. X#define OP_JUMP        28L    /* jump unconditionally */
  88. X#define OP_USERCALL    29L    /* call a user-defined function */
  89. X#define OP_GETVALUE    30L    /* convert address to value */
  90. X#define OP_EQ        31L    /* test top two elements for equality */
  91. X#define OP_NE        32L    /* test top two elements for inequality */
  92. X#define OP_LE        33L    /* test top two elements for <= */
  93. X#define OP_GE        34L    /* test top two elements for >= */
  94. X#define OP_LT        35L    /* test top two elements for < */
  95. X#define OP_GT        36L    /* test top two elements for > */
  96. X#define OP_PREINC    37L    /* add one to variable (++x) */
  97. X#define OP_PREDEC    38L    /* subtract one from variable (--x) */
  98. X#define OP_POSTINC    39L    /* add one to variable (x++) */
  99. X#define OP_POSTDEC    40L    /* subtract one from variable (x--) */
  100. X#define OP_DEBUG    41L    /* debugging point */
  101. X#define OP_PRINT    42L    /* print value */
  102. X#define OP_ASSIGNPOP    43L    /* assign to variable and remove it */
  103. X#define OP_ZERO        44L    /* put zero on the stack */
  104. X#define OP_ONE        45L    /* put one on the stack */
  105. X#define OP_PRINTEOL    46L    /* print end of line */
  106. X#define OP_PRINTSPACE    47L    /* print a space */
  107. X#define OP_PRINTSTRING    48L    /* print constant string */
  108. X#define OP_DUPVALUE    49L    /* duplicate value of top value */
  109. X#define OP_OLDVALUE    50L    /* old calculation value */
  110. X#define OP_QUO        51L    /* integer quotient of top two values */
  111. X#define OP_POWER    52L    /* number raised to a power */
  112. X#define OP_QUIT        53L    /* quit program */
  113. X#define OP_CALL        54L    /* call built-in routine */
  114. X#define OP_GETEPSILON    55L    /* get allowed error for calculations */
  115. X#define OP_AND        56L    /* arithmetic and */
  116. X#define OP_OR        57L    /* arithmetic or */
  117. X#define OP_NOT        58L    /* logical not */
  118. X#define OP_ABS        59L    /* absolute value */
  119. X#define OP_SGN        60L    /* sign of number */
  120. X#define OP_ISINT    61L    /* whether top value is integer */
  121. X#define OP_CONDORJUMP    62L    /* conditional or jump */
  122. X#define OP_CONDANDJUMP    63L    /* conditional and jump */
  123. X#define OP_SQUARE    64L    /* square top value */
  124. X#define OP_STRING    65L    /* load constant string value */
  125. X#define OP_ISNUM    66L    /* whether top value is a number */
  126. X#define OP_UNDEF    67L    /* load undefined value on stack */
  127. X#define OP_ISNULL    68L    /* whether variable is the null value */
  128. X#define OP_ARGVALUE    69L    /* load value of argument (parameter) n */
  129. X#define OP_MATCREATE    70L    /* create matrix */
  130. X#define OP_ISMAT    71L    /* whether variable is a matrix */
  131. X#define OP_ISSTR    72L    /* whether variable is a string */
  132. X#define OP_GETCONFIG    73L    /* get value of configuration parameter */
  133. X#define OP_LEFTSHIFT    74L    /* left shift of integer */
  134. X#define OP_RIGHTSHIFT    75L    /* right shift of integer */
  135. X#define OP_CASEJUMP    76L    /* test case and jump if not matched */
  136. X#define OP_ISODD    77L    /* whether value is an odd integer */
  137. X#define OP_ISEVEN    78L    /* whether value is even integer */
  138. X#define OP_FIADDR    79L    /* 'fast index' matrix value address */
  139. X#define OP_FIVALUE    80L    /* 'fast index' matrix value */
  140. X#define OP_ISREAL    81L    /* test value for real number */
  141. X#define OP_IMAGINARY    82L    /* load imaginary numeric constant */
  142. X#define OP_RE        83L    /* real part of complex number */
  143. X#define OP_IM        84L    /* imaginary part of complex number */
  144. X#define OP_CONJUGATE    85L    /* complex conjugate of complex number */
  145. X#define OP_OBJCREATE    86L    /* create object */
  146. X#define OP_ISOBJ    87L    /* whether value is an object */
  147. X#define OP_NORM        88L    /* norm of value (square of abs) */
  148. X#define OP_ELEMADDR    89L    /* address of element of object */
  149. X#define OP_ELEMVALUE    90L    /* value of element of object */
  150. X#define OP_ISTYPE    91L    /* whether two values are the same type */
  151. X#define OP_SCALE    92L    /* scale value by a power of two */
  152. X#define    OP_ISLIST    93L    /* whether value is a list */
  153. X#define    OP_SWAP        94L    /* swap values of two variables */
  154. X#define    OP_ISSIMPLE    95L    /* whether value is a simple type */
  155. X#define    OP_CMP        96L    /* compare values returning -1, 0, or 1 */
  156. X#define    OP_QUOMOD    97L    /* calculate quotient and remainder */
  157. X#define    OP_SETCONFIG    98L    /* set configuration parameter */
  158. X#define    OP_SETEPSILON    99L    /* set allowed error for calculations */
  159. X#define    OP_ISFILE    100L    /* whether value is a file */
  160. X#define    OP_ISASSOC    101L    /* whether value is an association */
  161. X#define    OP_INITSTATIC    102L    /* once only code for static initialization */
  162. X#define    OP_ELEMINIT    103L    /* assign element of matrix or object */
  163. X#define MAX_OPCODE    103L    /* highest legal opcode */
  164. X
  165. X#endif
  166. X
  167. X/* END CODE */
  168. SHAR_EOF
  169. chmod 0644 calc2.9.0/opcodes.h || echo "restore of calc2.9.0/opcodes.h fails"
  170. set `wc -c calc2.9.0/opcodes.h`;Sum=$1
  171. if test "$Sum" != "6052"
  172. then echo original size 6052, current size $Sum;fi
  173. echo "x - extracting calc2.9.0/qfunc.c (Text)"
  174. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/qfunc.c &&
  175. X/*
  176. X * Copyright (c) 1993 David I. Bell
  177. X * Permission is granted to use, distribute, or modify this source,
  178. X * provided that this copyright notice remains intact.
  179. X *
  180. X * Extended precision rational arithmetic non-primitive functions
  181. X */
  182. X
  183. X#include "qmath.h"
  184. X
  185. X
  186. XNUMBER *_epsilon_;    /* default precision for real functions */
  187. Xlong _epsilonprec_;    /* bits of precision for epsilon */
  188. X
  189. X
  190. X/*
  191. X * Set the default precision for real calculations.
  192. X * The precision must be between zero and one.
  193. X */
  194. Xvoid
  195. Xsetepsilon(q)
  196. X    NUMBER *q;        /* number to be set as the new epsilon */
  197. X{
  198. X    NUMBER *old;
  199. X
  200. X    if (qisneg(q) || qiszero(q) || (qreli(q, 1L) >= 0))
  201. X        math_error("Epsilon value must be between zero and one");
  202. X    old = _epsilon_;
  203. X    _epsilonprec_ = qprecision(q);
  204. X    _epsilon_ = qlink(q);
  205. X    if (old)
  206. X        qfree(old);
  207. X}
  208. X
  209. X
  210. X/*
  211. X * Return the inverse of one number modulo another.
  212. X * That is, find x such that:
  213. X *    Ax = 1 (mod B)
  214. X * Returns zero if the numbers are not relatively prime (temporary hack).
  215. X */
  216. XNUMBER *
  217. Xqminv(q1, q2)
  218. X    NUMBER *q1, *q2;
  219. X{
  220. X    NUMBER *r;
  221. X
  222. X    if (qisfrac(q1) || qisfrac(q2))
  223. X        math_error("Non-integers for minv");
  224. X    r = qalloc();
  225. X    if (zmodinv(q1->num, q2->num, &r->num)) {
  226. X        qfree(r);
  227. X        return qlink(&_qzero_);
  228. X    }
  229. X    return r;
  230. X}
  231. X
  232. X
  233. X/*
  234. X * Return the modulo of one number raised to another.
  235. X * Here q1 is the number to be raised, q2 is the power to raise it to,
  236. X * and q3 is the number to take the modulo with the result.
  237. X * The second and third numbers are assumed nonnegative.
  238. X * Returned answer is non-negative.
  239. X *    q4 = qpowermod(q1, q2, q3);
  240. X */
  241. XNUMBER *
  242. Xqpowermod(q1, q2, q3)
  243. X    NUMBER *q1, *q2, *q3;
  244. X{
  245. X    NUMBER *r;
  246. X
  247. X    if (qisfrac(q1) || qisfrac(q2) || qisfrac(q3))
  248. X        math_error("Non-integers for powermod");
  249. X    r = qalloc();
  250. X    zpowermod(q1->num, q2->num, q3->num, &r->num);
  251. X    return r;
  252. X}
  253. X
  254. X
  255. X/*
  256. X * Return the power function of one number with another.
  257. X * The power must be integral.
  258. X *    q3 = qpowi(q1, q2);
  259. X */
  260. XNUMBER *
  261. Xqpowi(q1, q2)
  262. X    NUMBER *q1, *q2;
  263. X{
  264. X    register NUMBER *r;
  265. X    BOOL invert, sign;
  266. X    ZVALUE num, den, z2;
  267. X
  268. X    if (qisfrac(q2))
  269. X        math_error("Raising number to fractional power");
  270. X    num = q1->num;
  271. X    den = q1->den;
  272. X    z2 = q2->num;
  273. X    sign = (num.sign && zisodd(z2));
  274. X    invert = z2.sign;
  275. X    num.sign = 0;
  276. X    z2.sign = 0;
  277. X    /*
  278. X    * Check for trivial cases first.
  279. X    */
  280. X    if (ziszero(num)) {    /* zero raised to a power */
  281. X        if (invert || ziszero(z2))
  282. X            math_error("Zero raised to non-positive power");
  283. X        return qlink(&_qzero_);
  284. X    }
  285. X    if (zisunit(num) && zisunit(den)) {    /* 1 or -1 raised to a power */
  286. X        r = (sign ? q1 : &_qone_);
  287. X        r->links++;
  288. X        return r;
  289. X    }
  290. X    if (ziszero(z2))    /* raising to zeroth power */
  291. X        return qlink(&_qone_);
  292. X    if (zisunit(z2)) {    /* raising to power 1 or -1 */
  293. X        if (invert)
  294. X            return qinv(q1);
  295. X        return qlink(q1);
  296. X    }
  297. X    /*
  298. X     * Not a trivial case.  Do the real work.
  299. X     */
  300. X    r = qalloc();
  301. X    if (!zisunit(num))
  302. X        zpowi(num, z2, &r->num);
  303. X    if (!zisunit(den))
  304. X        zpowi(den, z2, &r->den);
  305. X    if (invert) {
  306. X        z2 = r->num;
  307. X        r->num = r->den;
  308. X        r->den = z2;
  309. X    }
  310. X    r->num.sign = sign;
  311. X    return r;
  312. X}
  313. X
  314. X
  315. X/*
  316. X * Given the legs of a right triangle, compute its hypothenuse within
  317. X * the specified error.  This is sqrt(a^2 + b^2).
  318. X */
  319. XNUMBER *
  320. Xqhypot(q1, q2, epsilon)
  321. X    NUMBER *q1, *q2, *epsilon;
  322. X{
  323. X    NUMBER *tmp1, *tmp2, *tmp3;
  324. X
  325. X    if (qisneg(epsilon) || qiszero(epsilon))
  326. X        math_error("Bad epsilon value for hypot");
  327. X    if (qiszero(q1))
  328. X        return qabs(q2);
  329. X    if (qiszero(q2))
  330. X        return qabs(q1);
  331. X    tmp1 = qsquare(q1);
  332. X    tmp2 = qsquare(q2);
  333. X    tmp3 = qadd(tmp1, tmp2);
  334. X    qfree(tmp1);
  335. X    qfree(tmp2);
  336. X    tmp1 = qsqrt(tmp3, epsilon);
  337. X    qfree(tmp3);
  338. X    return tmp1;
  339. X}
  340. X
  341. X
  342. X/*
  343. X * Given one leg of a right triangle with unit hypothenuse, calculate
  344. X * the other leg within the specified error.  This is sqrt(1 - a^2).
  345. X * If the wantneg flag is nonzero, then negative square root is returned.
  346. X */
  347. XNUMBER *
  348. Xqlegtoleg(q, epsilon, wantneg)
  349. X    NUMBER *q, *epsilon;
  350. X    BOOL wantneg;
  351. X{
  352. X    NUMBER *qt, *res, qtmp;
  353. X    ZVALUE num, ztmp1, ztmp2;
  354. X
  355. X    if (qisneg(epsilon) || qiszero(epsilon))
  356. X        math_error("Bad epsilon value for legtoleg");
  357. X    if (qisunit(q))
  358. X        return qlink(&_qzero_);
  359. X    if (qiszero(q)) {
  360. X        if (wantneg)
  361. X            return qlink(&_qnegone_);
  362. X        return qlink(&_qone_);
  363. X    }
  364. X    num = q->num;
  365. X    num.sign = 0;
  366. X    if (zrel(num, q->den) >= 0)
  367. X        math_error("Leg too large in legtoleg");
  368. X    zsquare(q->den, &ztmp1);
  369. X    zsquare(num, &ztmp2);
  370. X    zsub(ztmp1, ztmp2, &qtmp.num);
  371. X    zfree(ztmp1);
  372. X    zfree(ztmp2);
  373. X    qtmp.den = _one_;
  374. X    qt = qsqrt(&qtmp, epsilon);
  375. X    zfree(qtmp.num);
  376. X    qtmp.num = q->den;
  377. X    res = qdiv(qt, &qtmp);
  378. X    qfree(qt);
  379. X    qt = qbappr(res, epsilon);
  380. X    qfree(res);
  381. X    if (wantneg) {
  382. X        res = qneg(qt);
  383. X        qfree(qt);
  384. X        qt = res;
  385. X    }
  386. X    return qt;
  387. X}
  388. X
  389. X
  390. X/*
  391. X * Compute the square root of a number to within the specified error.
  392. X * If the number is an exact square, the exact result is returned.
  393. X *    q3 = qsqrt(q1, q2);
  394. X */
  395. XNUMBER *
  396. Xqsqrt(q1, epsilon)
  397. X    register NUMBER *q1, *epsilon;
  398. X{
  399. X    long bits, bits2;    /* number of bits of precision */
  400. X    int exact;
  401. X    NUMBER *r;
  402. X    ZVALUE t1, t2;
  403. X
  404. X    if (qisneg(q1))
  405. X        math_error("Square root of negative number");
  406. X    if (qisneg(epsilon) || qiszero(epsilon))
  407. X        math_error("Bad epsilon value for sqrt");
  408. X    if (qiszero(q1))
  409. X        return qlink(&_qzero_);
  410. X    if (qisunit(q1))
  411. X        return qlink(&_qone_);
  412. X    if (qiszero(epsilon) && qisint(q1) && zistiny(q1->num) && (*q1->num.v <= 3))
  413. X        return qlink(&_qone_);
  414. X    bits = zhighbit(epsilon->den) - zhighbit(epsilon->num) + 1;
  415. X    if (bits < 0)
  416. X        bits = 0;
  417. X    bits2 = zhighbit(q1->num) - zhighbit(q1->den) + 1;
  418. X    if (bits2 > 0)
  419. X        bits += bits2;
  420. X    r = qalloc();
  421. X    zshift(q1->num, bits * 2, &t2);
  422. X    zmul(q1->den, t2, &t1);
  423. X    zfree(t2);
  424. X    exact = zsqrt(t1, &t2);
  425. X    zfree(t1);
  426. X    if (exact) {
  427. X        zshift(q1->den, bits, &t1);
  428. X        zreduce(t2, t1, &r->num, &r->den);
  429. X    } else {
  430. X        zquo(t2, q1->den, &t1);
  431. X        zfree(t2);
  432. X        zbitvalue(bits, &t2);
  433. X        zreduce(t1, t2, &r->num, &r->den);
  434. X    }
  435. X    zfree(t1);
  436. X    zfree(t2);
  437. X    if (qiszero(r)) {
  438. X        qfree(r);
  439. X        r = qlink(&_qzero_);
  440. X    }
  441. X    return r;
  442. X}
  443. X
  444. X
  445. X/*
  446. X * Calculate the integral part of the square root of a number.
  447. X * Example:  qisqrt(13) = 3.
  448. X */
  449. XNUMBER *
  450. Xqisqrt(q)
  451. X    NUMBER *q;
  452. X{
  453. X    NUMBER *r;
  454. X    ZVALUE tmp;
  455. X
  456. X    if (qisneg(q))
  457. X        math_error("Square root of negative number");
  458. X    if (qiszero(q))
  459. X        return qlink(&_qzero_);
  460. X    if (qisint(q) && zistiny(q->num) && (z1tol(q->num) < 4))
  461. X        return qlink(&_qone_);
  462. X    r = qalloc();
  463. X    if (qisint(q)) {
  464. X        (void) zsqrt(q->num, &r->num);
  465. X        return r;
  466. X    }
  467. X    zquo(q->num, q->den, &tmp);
  468. X    (void) zsqrt(tmp, &r->num);
  469. X    zfree(tmp);
  470. X    return r;
  471. X}
  472. X
  473. X
  474. X/*
  475. X * Return whether or not a number is an exact square.
  476. X */
  477. XBOOL
  478. Xqissquare(q)
  479. X    NUMBER *q;
  480. X{
  481. X    BOOL flag;
  482. X
  483. X    flag = zissquare(q->num);
  484. X    if (qisint(q) || !flag)
  485. X        return flag;
  486. X    return zissquare(q->den);
  487. X}
  488. X
  489. X
  490. X/*
  491. X * Compute the greatest integer of the Kth root of a number.
  492. X * Example:  qiroot(85, 3) = 4.
  493. X */
  494. XNUMBER *
  495. Xqiroot(q1, q2)
  496. X    register NUMBER *q1, *q2;
  497. X{
  498. X    NUMBER *r;
  499. X    ZVALUE tmp;
  500. X
  501. X    if (qisneg(q2) || qiszero(q2) || qisfrac(q2))
  502. X        math_error("Taking number to bad root value");
  503. X    if (qiszero(q1))
  504. X        return qlink(&_qzero_);
  505. X    if (qisone(q1) || qisone(q2))
  506. X        return qlink(q1);
  507. X    if (qistwo(q2))
  508. X        return qisqrt(q1);
  509. X    r = qalloc();
  510. X    if (qisint(q1)) {
  511. X        zroot(q1->num, q2->num, &r->num);
  512. X        return r;
  513. X    }
  514. X    zquo(q1->num, q1->den, &tmp);
  515. X    zroot(tmp, q2->num, &r->num);
  516. X    zfree(tmp);
  517. X    return r;
  518. X}
  519. X
  520. X
  521. X/*
  522. X * Return the greatest integer of the base 2 log of a number.
  523. X * This is the number such that  1 <= x / log2(x) < 2.
  524. X * Examples:  qilog2(8) = 3, qilog2(1.3) = 1, qilog2(1/7) = -3.
  525. X */
  526. Xlong
  527. Xqilog2(q)
  528. X    NUMBER *q;        /* number to take log of */
  529. X{
  530. X    long n;            /* power of two */
  531. X    int c;            /* result of comparison */
  532. X    ZVALUE tmp;        /* temporary value */
  533. X
  534. X    if (qisneg(q) || qiszero(q))
  535. X        math_error("Non-positive number for log2");
  536. X    if (qisint(q))
  537. X        return zhighbit(q->num);
  538. X    n = zhighbit(q->num) - zhighbit(q->den);
  539. X    if (n == 0)
  540. X        c = zrel(q->num, q->den);
  541. X    else if (n > 0) {
  542. X        zshift(q->den, n, &tmp);
  543. X        c = zrel(q->num, tmp);
  544. X    } else {
  545. X        zshift(q->num, n, &tmp);
  546. X        c = zrel(tmp, q->den);
  547. X    }
  548. X    if (n)
  549. X        zfree(tmp);
  550. X    if (c < 0)
  551. X        n--;
  552. X    return n;
  553. X}
  554. X
  555. X
  556. X/*
  557. X * Return the greatest integer of the base 10 log of a number.
  558. X * This is the number such that  1 <= x / log10(x) < 10.
  559. X * Examples:  qilog10(100) = 2, qilog10(12.3) = 1, qilog10(.023) = -2.
  560. X */
  561. Xlong
  562. Xqilog10(q)
  563. X    NUMBER *q;        /* number to take log of */
  564. X{
  565. X    long n;            /* log value */
  566. X    ZVALUE temp;        /* temporary value */
  567. X
  568. X    if (qisneg(q) || qiszero(q))
  569. X        math_error("Non-positive number for log10");
  570. X    if (qisint(q))
  571. X        return zlog10(q->num);
  572. X    /*
  573. X     * The number is not an integer.
  574. X     * Compute the result if the number is greater than one.
  575. X     */
  576. X    if ((q->num.len > q->den.len) ||
  577. X        ((q->num.len == q->den.len) && (zrel(q->num, q->den) > 0))) {
  578. X            zquo(q->num, q->den, &temp);
  579. X            n = zlog10(temp);
  580. X            zfree(temp);
  581. X            return n;
  582. X    }
  583. X    /*
  584. X     * Here if the number is less than one.
  585. X     * If the number is the inverse of a power of ten, then the obvious answer
  586. X     * will be off by one.  Subtracting one if the number is the inverse of an
  587. X     * integer will fix it.
  588. X     */
  589. X    if (zisunit(q->num))
  590. X        zsub(q->den, _one_, &temp);
  591. X    else
  592. X        zquo(q->den, q->num, &temp);
  593. X    n = -zlog10(temp) - 1;
  594. X    zfree(temp);
  595. X    return n;
  596. X}
  597. X
  598. X
  599. X/*
  600. X * Return the number of digits in a number, ignoring the sign.
  601. X * For fractions, this is the number of digits of its greatest integer.
  602. X * Examples: qdigits(3456) = 4, qdigits(-23.45) = 2, qdigits(.0120) = 1.
  603. X */
  604. Xlong
  605. Xqdigits(q)
  606. X    NUMBER *q;        /* number to count digits of */
  607. X{
  608. X    long n;            /* number of digits */
  609. X    ZVALUE temp;        /* temporary value */
  610. X
  611. X    if (qisint(q))
  612. X        return zdigits(q->num);
  613. X    zquo(q->num, q->den, &temp);
  614. X    n = zdigits(temp);
  615. X    zfree(temp);
  616. X    return n;
  617. X}
  618. X
  619. X
  620. X/*
  621. X * Return the digit at the specified decimal place of a number represented
  622. X * in floating point.  The lowest digit of the integral part of a number
  623. X * is the zeroth decimal place.  Negative decimal places indicate digits
  624. X * to the right of the decimal point.  Examples: qdigit(1234.5678, 1) = 3,
  625. X * qdigit(1234.5678, -3) = 7.
  626. X */
  627. XFLAG
  628. Xqdigit(q, n)
  629. X    NUMBER *q;
  630. X    long n;
  631. X{
  632. X    ZVALUE tenpow, tmp1, tmp2;
  633. X    FLAG res;
  634. X
  635. X    /*
  636. X     * Zero number or negative decimal place of integer is trivial.
  637. X     */
  638. X    if (qiszero(q) || (qisint(q) && (n < 0)))
  639. X        return 0;
  640. X    /*
  641. X     * For non-negative decimal places, answer is easy.
  642. X     */
  643. X    if (n >= 0) {
  644. X        if (qisint(q))
  645. X            return zdigit(q->num, n);
  646. X        zquo(q->num, q->den, &tmp1);
  647. X        res = zdigit(tmp1, n);
  648. X        zfree(tmp1);
  649. X        return res;
  650. X    }
  651. X    /*
  652. X     * Fractional value and want negative digit, must work harder.
  653. X     */
  654. X    ztenpow(-n, &tenpow);
  655. X    zmul(q->num, tenpow, &tmp1);
  656. X    zfree(tenpow);
  657. X    zquo(tmp1, q->den, &tmp2);
  658. X    res = zmodi(tmp2, 10L);
  659. X    zfree(tmp1);
  660. X    zfree(tmp2);
  661. X    return res;
  662. X}
  663. X
  664. X
  665. X/*
  666. X * Return whether or not a bit is set at the specified bit position in a
  667. X * number.  The lowest bit of the integral part of a number is the zeroth
  668. X * bit position.  Negative bit positions indicate bits to the right of the
  669. X * binary decimal point.  Examples: qdigit(17.1, 0) = 1, qdigit(17.1, -1) = 0.
  670. X */
  671. XBOOL
  672. Xqisset(q, n)
  673. X    NUMBER *q;
  674. X    long n;
  675. X{
  676. X    NUMBER *qtmp1, *qtmp2;
  677. X    ZVALUE ztmp;
  678. X    BOOL res;
  679. X
  680. X    /*
  681. X     * Zero number or negative bit position place of integer is trivial.
  682. X     */
  683. X    if (qiszero(q) || (qisint(q) && (n < 0)))
  684. X        return FALSE;
  685. X    /*
  686. X     * For non-negative bit positions, answer is easy.
  687. X     */
  688. X    if (n >= 0) {
  689. X        if (qisint(q))
  690. X            return zisset(q->num, n);
  691. X        zquo(q->num, q->den, &ztmp);
  692. X        res = zisset(ztmp, n);
  693. X        zfree(ztmp);
  694. X        return res;
  695. X    }
  696. X    /*
  697. X     * Fractional value and want negative bit position, must work harder.
  698. X     */
  699. X    qtmp1 = qscale(q, -n);
  700. X    qtmp2 = qint(qtmp1);
  701. X    qfree(qtmp1);
  702. X    res = ((qtmp2->num.v[0] & 0x01) != 0);
  703. X    qfree(qtmp2);
  704. X    return res;
  705. X}
  706. X
  707. X
  708. X/*
  709. X * Compute the factorial of an integer.
  710. X *    q2 = qfact(q1);
  711. X */
  712. XNUMBER *
  713. Xqfact(q)
  714. X    register NUMBER *q;
  715. X{
  716. X    register NUMBER *r;
  717. X
  718. X    if (qisfrac(q))
  719. X        math_error("Non-integral factorial");
  720. X    if (qiszero(q) || zisone(q->num))
  721. X        return qlink(&_qone_);
  722. X    r = qalloc();
  723. X    zfact(q->num, &r->num);
  724. X    return r;
  725. X}
  726. X
  727. X
  728. X/*
  729. X * Compute the product of the primes less than or equal to a number.
  730. X *    q2 = qpfact(q1);
  731. X */
  732. XNUMBER *
  733. Xqpfact(q)
  734. X    register NUMBER *q;
  735. X{
  736. X    NUMBER *r;
  737. X
  738. X    if (qisfrac(q))
  739. X        math_error("Non-integral factorial");
  740. X    r = qalloc();
  741. X    zpfact(q->num, &r->num);
  742. X    return r;
  743. X}
  744. X
  745. X
  746. X/*
  747. X * Compute the lcm of all the numbers less than or equal to a number.
  748. X *    q2 = qlcmfact(q1);
  749. X */
  750. XNUMBER *
  751. Xqlcmfact(q)
  752. X    register NUMBER *q;
  753. X{
  754. X    NUMBER *r;
  755. X
  756. X    if (qisfrac(q))
  757. X        math_error("Non-integral lcmfact");
  758. X    r = qalloc();
  759. X    zlcmfact(q->num, &r->num);
  760. X    return r;
  761. X}
  762. X
  763. X
  764. X/*
  765. X * Compute the permutation function  M! / (M - N)!.
  766. X */
  767. XNUMBER *
  768. Xqperm(q1, q2)
  769. X    register NUMBER *q1, *q2;
  770. X{
  771. X    register NUMBER *r;
  772. X
  773. X    if (qisfrac(q1) || qisfrac(q2))
  774. X        math_error("Non-integral arguments for permutation");
  775. X    r = qalloc();
  776. X    zperm(q1->num, q2->num, &r->num);
  777. X    return r;
  778. X}
  779. X
  780. X
  781. X/*
  782. X * Compute the combinatorial function  M! / (N! * (M - N)!).
  783. X */
  784. XNUMBER *
  785. Xqcomb(q1, q2)
  786. X    register NUMBER *q1, *q2;
  787. X{
  788. X    register NUMBER *r;
  789. X
  790. X    if (qisfrac(q1) || qisfrac(q2))
  791. X        math_error("Non-integral arguments for combinatorial");
  792. X    r = qalloc();
  793. X    zcomb(q1->num, q2->num, &r->num);
  794. X    return r;
  795. X}
  796. X
  797. X
  798. X/*
  799. X * Compute the Jacobi function (a / b).
  800. X * -1 => a is not quadratic residue mod b
  801. X * 1 => b is composite, or a is quad residue of b
  802. X * 0 => b is even or b < 0
  803. X */
  804. XNUMBER *
  805. Xqjacobi(q1, q2)
  806. X    register NUMBER *q1, *q2;
  807. X{
  808. X    if (qisfrac(q1) || qisfrac(q2))
  809. X        math_error("Non-integral arguments for jacobi");
  810. X    return itoq((long) zjacobi(q1->num, q2->num));
  811. X}
  812. X
  813. X
  814. X/*
  815. X * Compute the Fibonacci number F(n).
  816. X */
  817. XNUMBER *
  818. Xqfib(q)
  819. X    register NUMBER *q;
  820. X{
  821. X    register NUMBER *r;
  822. X
  823. X    if (qisfrac(q))
  824. X        math_error("Non-integral Fibonacci number");
  825. X    r = qalloc();
  826. X    zfib(q->num, &r->num);
  827. X    return r;
  828. X}
  829. X
  830. X
  831. X/*
  832. X * Truncate a number to the specified number of decimal places.
  833. X * Specifying zero places makes the result identical to qint.
  834. X * Example: qtrunc(2/3, 3) = .666
  835. X */
  836. XNUMBER *
  837. Xqtrunc(q1, q2)
  838. X    NUMBER *q1, *q2;
  839. X{
  840. X    long places;
  841. X    NUMBER *r;
  842. X    ZVALUE tenpow, tmp1, tmp2;
  843. X
  844. X    if (qisfrac(q2) || qisneg(q2) || !zistiny(q2->num))
  845. X        math_error("Bad number of places for qtrunc");
  846. X    if (qisint(q1))
  847. X        return qlink(q1);
  848. X    r = qalloc();
  849. X    places = z1tol(q2->num);
  850. X    /*
  851. X     * Ok, produce the result.
  852. X     * First see if we want no places, in which case just take integer part.
  853. X     */
  854. X    if (places == 0) {
  855. X        zquo(q1->num, q1->den, &r->num);
  856. X        return r;
  857. X    }
  858. X    ztenpow(places, &tenpow);
  859. X    zmul(q1->num, tenpow, &tmp1);
  860. X    zquo(tmp1, q1->den, &tmp2);
  861. X    zfree(tmp1);
  862. X    if (ziszero(tmp2)) {
  863. X        zfree(tmp2);
  864. X        return qlink(&_qzero_);
  865. X    }
  866. X    /*
  867. X     * Now reduce the result to the lowest common denominator.
  868. X     */
  869. X    zgcd(tmp2, tenpow, &tmp1);
  870. X    if (zisunit(tmp1)) {
  871. X        zfree(tmp1);
  872. X        r->num = tmp2;
  873. X        r->den = tenpow;
  874. X        return r;
  875. X    }
  876. X    zquo(tmp2, tmp1, &r->num);
  877. X    zquo(tenpow, tmp1, &r->den);
  878. X    zfree(tmp1);
  879. X    zfree(tmp2);
  880. X    zfree(tenpow);
  881. X    return r;
  882. X}
  883. X
  884. X
  885. X/*
  886. X * Round a number to the specified number of decimal places.
  887. X * Zero decimal places means round to the nearest integer.
  888. X * Example: qround(2/3, 3) = .667
  889. X */
  890. XNUMBER *
  891. Xqround(q, places)
  892. X    NUMBER *q;        /* number to be rounded */
  893. X    long places;        /* number of decimal places to round to */
  894. X{
  895. X    NUMBER *r;
  896. X    ZVALUE tenpow, roundval, tmp1, tmp2;
  897. X
  898. X    if (places < 0)
  899. X        math_error("Negative places for qround");
  900. X    if (qisint(q))
  901. X        return qlink(q);
  902. X    /*
  903. X     * Calculate one half of the denominator, ignoring fractional results.
  904. X     * This is the value we will add in order to cause rounding.
  905. X     */
  906. X    zshift(q->den, -1L, &roundval);
  907. X    roundval.sign = q->num.sign;
  908. X    /*
  909. X     * Ok, now do the actual work to produce the result.
  910. X     */
  911. X    r = qalloc();
  912. X    ztenpow(places, &tenpow);
  913. X    zmul(q->num, tenpow, &tmp2);
  914. X    zadd(tmp2, roundval, &tmp1);
  915. X    zfree(tmp2);
  916. X    zfree(roundval);
  917. X    zquo(tmp1, q->den, &tmp2);
  918. X    zfree(tmp1);
  919. X    if (ziszero(tmp2)) {
  920. X        zfree(tmp2);
  921. X        return qlink(&_qzero_);
  922. X    }
  923. X    /*
  924. X     * Now reduce the result to the lowest common denominator.
  925. X     */
  926. X    zgcd(tmp2, tenpow, &tmp1);
  927. X    if (zisunit(tmp1)) {
  928. X        zfree(tmp1);
  929. X        r->num = tmp2;
  930. X        r->den = tenpow;
  931. X        return r;
  932. X    }
  933. X    zquo(tmp2, tmp1, &r->num);
  934. X    zquo(tenpow, tmp1, &r->den);
  935. X    zfree(tmp1);
  936. X    zfree(tmp2);
  937. X    zfree(tenpow);
  938. X    return r;
  939. X}
  940. X
  941. X
  942. X/*
  943. X * Truncate a number to the specified number of binary places.
  944. X * Specifying zero places makes the result identical to qint.
  945. X */
  946. XNUMBER *
  947. Xqbtrunc(q1, q2)
  948. X    NUMBER *q1, *q2;
  949. X{
  950. X    long places, twopow;
  951. X    NUMBER *r;
  952. X    ZVALUE tmp1, tmp2;
  953. X
  954. X    if (qisfrac(q2) || qisneg(q2) || !zistiny(q2->num))
  955. X        math_error("Bad number of places for qtrunc");
  956. X    if (qisint(q1))
  957. X        return qlink(q1);
  958. X    r = qalloc();
  959. X    places = z1tol(q2->num);
  960. X    /*
  961. X     * Ok, produce the result.
  962. X     * First see if we want no places, in which case just take integer part.
  963. X     */
  964. X    if (places == 0) {
  965. X        zquo(q1->num, q1->den, &r->num);
  966. X        return r;
  967. X    }
  968. X    zshift(q1->num, places, &tmp1);
  969. X    zquo(tmp1, q1->den, &tmp2);
  970. X    zfree(tmp1);
  971. X    if (ziszero(tmp2)) {
  972. X        zfree(tmp2);
  973. X        return qlink(&_qzero_);
  974. X    }
  975. X    /*
  976. X     * Now reduce the result to the lowest common denominator.
  977. X     */
  978. X    if (zisodd(tmp2)) {
  979. X        r->num = tmp2;
  980. X        zbitvalue(places, &r->den);
  981. X        return r;
  982. X    }
  983. X    twopow = zlowbit(tmp2);
  984. X    if (twopow > places)
  985. X        twopow = places;
  986. X    places -= twopow;
  987. X    zshift(tmp2, -twopow, &r->num);
  988. X    zfree(tmp2);
  989. X    zbitvalue(places, &r->den);
  990. X    return r;
  991. X}
  992. X
  993. X
  994. X/*
  995. X * Round a number to the specified number of binary places.
  996. X * Zero binary places means round to the nearest integer.
  997. X */
  998. XNUMBER *
  999. Xqbround(q, places)
  1000. X    NUMBER *q;        /* number to be rounded */
  1001. X    long places;        /* number of binary places to round to */
  1002. X{
  1003. X    long twopow;
  1004. X    NUMBER *r;
  1005. X    ZVALUE roundval, tmp1, tmp2;
  1006. X
  1007. X    if (places < 0)
  1008. X        math_error("Negative places for qbround");
  1009. X    if (qisint(q))
  1010. X        return qlink(q);
  1011. X    r = qalloc();
  1012. X    /*
  1013. X     * Calculate one half of the denominator, ignoring fractional results.
  1014. X     * This is the value we will add in order to cause rounding.
  1015. X     */
  1016. X    zshift(q->den, -1L, &roundval);
  1017. X    roundval.sign = q->num.sign;
  1018. X    /*
  1019. X     * Ok, produce the result.
  1020. X     */
  1021. X    zshift(q->num, places, &tmp1);
  1022. X    zadd(tmp1, roundval, &tmp2);
  1023. X    zfree(roundval);
  1024. X    zfree(tmp1);
  1025. X    zquo(tmp2, q->den, &tmp1);
  1026. X    zfree(tmp2);
  1027. X    if (ziszero(tmp1)) {
  1028. X        zfree(tmp1);
  1029. X        return qlink(&_qzero_);
  1030. X    }
  1031. X    /*
  1032. X     * Now reduce the result to the lowest common denominator.
  1033. X     */
  1034. X    if (zisodd(tmp1)) {
  1035. X        r->num = tmp1;
  1036. X        zbitvalue(places, &r->den);
  1037. X        return r;
  1038. X    }
  1039. X    twopow = zlowbit(tmp1);
  1040. X    if (twopow > places)
  1041. X        twopow = places;
  1042. X    places -= twopow;
  1043. X    zshift(tmp1, -twopow, &r->num);
  1044. X    zfree(tmp1);
  1045. X    zbitvalue(places, &r->den);
  1046. X    return r;
  1047. X}
  1048. X
  1049. X
  1050. X/*
  1051. X * Approximate a number by using binary rounding with the minimum number
  1052. X * of binary places so that the resulting number is within the specified
  1053. X * epsilon of the original number.
  1054. X */
  1055. XNUMBER *
  1056. Xqbappr(q, e)
  1057. X    NUMBER *q, *e;
  1058. X{
  1059. X    long bits;
  1060. X
  1061. X    if (qisneg(e) || qiszero(e))
  1062. X        math_error("Bad epsilon value for qbappr");
  1063. X    if (e == _epsilon_)
  1064. X        bits = _epsilonprec_ + 1;
  1065. X    else
  1066. X        bits = qprecision(e) + 1;
  1067. X    return qbround(q, bits);
  1068. X}
  1069. X
  1070. X
  1071. X/*
  1072. X * Approximate a number using continued fractions until the approximation
  1073. X * error is less than the specified value.  If a NULL pointer is given
  1074. X * for the error value, then the closest simpler fraction is returned.
  1075. X */
  1076. XNUMBER *
  1077. Xqcfappr(q, e)
  1078. X    NUMBER *q, *e;
  1079. X{
  1080. X    NUMBER qtest, *qtmp;
  1081. X    ZVALUE u1, u2, u3, v1, v2, v3, t1, t2, t3, qq, tt;
  1082. X    int i;
  1083. X    BOOL haveeps;
  1084. X
  1085. X    haveeps = TRUE;
  1086. X    if (e == NULL) {
  1087. X        haveeps = FALSE;
  1088. X        e = &_qzero_;
  1089. X    }
  1090. X    if (qisneg(e))
  1091. X        math_error("Negative epsilon for cfappr");
  1092. X    if (qisint(q) || zisunit(q->num) || (haveeps && qiszero(e)))
  1093. X        return qlink(q);
  1094. X    u1 = _one_;
  1095. X    u2 = _zero_;
  1096. X    u3 = q->num;
  1097. X    u3.sign = 0;
  1098. X    v1 = _zero_;
  1099. X    v2 = _one_;
  1100. X    v3 = q->den;
  1101. X    while (!ziszero(v3)) {
  1102. X        if (!ziszero(u2) && !ziszero(u1)) {
  1103. X            qtest.num = u2;
  1104. X            qtest.den = u1;
  1105. X            qtest.den.sign = 0;
  1106. X            qtest.num.sign = q->num.sign;
  1107. X            qtmp = qsub(q, &qtest);
  1108. X            qtest = *qtmp;
  1109. X            qtest.num.sign = 0;
  1110. X            i = qrel(&qtest, e);
  1111. X            qfree(qtmp);
  1112. X            if (i <= 0)
  1113. X                break;
  1114. X        }
  1115. X        zquo(u3, v3, &qq);
  1116. X        zmul(qq, v1, &tt); zsub(u1, tt, &t1); zfree(tt);
  1117. X        zmul(qq, v2, &tt); zsub(u2, tt, &t2); zfree(tt);
  1118. X        zmul(qq, v3, &tt); zsub(u3, tt, &t3); zfree(tt);
  1119. X        zfree(qq); zfree(u1); zfree(u2);
  1120. X        if ((u3.v != q->num.v) && (u3.v != q->den.v))
  1121. X            zfree(u3);
  1122. X        u1 = v1; u2 = v2; u3 = v3;
  1123. X        v1 = t1; v2 = t2; v3 = t3;
  1124. X    }
  1125. X    if (u3.v != q->den.v)
  1126. X        zfree(u3);
  1127. X    zfree(v1);
  1128. X    zfree(v2);
  1129. X    i = ziszero(v3);
  1130. X    zfree(v3);
  1131. X    if (i && haveeps) {
  1132. X        zfree(u1);
  1133. X        zfree(u2);
  1134. X        return qlink(q);
  1135. X    }
  1136. X    qtest.num = u2;
  1137. X    qtest.den = u1;
  1138. X    qtest.den.sign = 0;
  1139. X    qtest.num.sign = q->num.sign;
  1140. X    qtmp = qcopy(&qtest);
  1141. X    zfree(u1);
  1142. X    zfree(u2);
  1143. X    return qtmp;
  1144. X}
  1145. X
  1146. X
  1147. X/*
  1148. X * Return an indication on whether or not two fractions are approximately
  1149. X * equal within the specified epsilon. Returns negative if the absolute value
  1150. X * of the difference between the two numbers is less than epsilon, zero if
  1151. X * the difference is equal to epsilon, and positive if the difference is
  1152. X * greater than epsilon.
  1153. X */
  1154. XFLAG
  1155. Xqnear(q1, q2, epsilon)
  1156. X    NUMBER *q1, *q2, *epsilon;
  1157. X{
  1158. X    int res;
  1159. X    NUMBER qtemp, *qq;
  1160. X
  1161. X    if (qisneg(epsilon))
  1162. X        math_error("Negative epsilon for near");
  1163. X    if (q1 == q2) {
  1164. X        if (qiszero(epsilon))
  1165. X            return 0;
  1166. X        return -1;
  1167. X    }
  1168. X    if (qiszero(epsilon))
  1169. X        return qcmp(q1, q2);
  1170. X    if (qiszero(q2)) {
  1171. X        qtemp = *q1;
  1172. X        qtemp.num.sign = 0;
  1173. X        return qrel(&qtemp, epsilon);
  1174. X    }
  1175. X    if (qiszero(q1)) {
  1176. X        qtemp = *q2;
  1177. X        qtemp.num.sign = 0;
  1178. X        return qrel(&qtemp, epsilon);
  1179. X    }
  1180. X    qq = qsub(q1, q2);
  1181. X    qtemp = *qq;
  1182. X    qtemp.num.sign = 0;
  1183. X    res = qrel(&qtemp, epsilon);
  1184. X    qfree(qq);
  1185. X    return res;
  1186. X}
  1187. X
  1188. X
  1189. X/*
  1190. X * Compute the gcd (greatest common divisor) of two numbers.
  1191. X *    q3 = qgcd(q1, q2);
  1192. X */
  1193. XNUMBER *
  1194. Xqgcd(q1, q2)
  1195. X    register NUMBER *q1, *q2;
  1196. X{
  1197. X    ZVALUE z;
  1198. X    NUMBER *q;
  1199. X
  1200. X    if (q1 == q2)
  1201. X        return qabs(q1);
  1202. X    if (qisfrac(q1) || qisfrac(q2)) {
  1203. X        q = qalloc();
  1204. X        zgcd(q1->num, q2->num, &q->num);
  1205. X        zlcm(q1->den, q2->den, &q->den);
  1206. X        return q;
  1207. X    }
  1208. X    if (qiszero(q1))
  1209. X        return qabs(q2);
  1210. X    if (qiszero(q2))
  1211. X        return qabs(q1);
  1212. X    if (qisunit(q1) || qisunit(q2))
  1213. X        return qlink(&_qone_);
  1214. X    zgcd(q1->num, q2->num, &z);
  1215. X    if (zisunit(z)) {
  1216. X        zfree(z);
  1217. X        return qlink(&_qone_);
  1218. X    }
  1219. X    q = qalloc();
  1220. X    q->num = z;
  1221. X    return q;
  1222. X}
  1223. X
  1224. X
  1225. X/*
  1226. X * Compute the lcm (least common multiple) of two numbers.
  1227. X *    q3 = qlcm(q1, q2);
  1228. X */
  1229. XNUMBER *
  1230. Xqlcm(q1, q2)
  1231. X    register NUMBER *q1, *q2;
  1232. X{
  1233. X    NUMBER *q;
  1234. X
  1235. X    if (qiszero(q1) || qiszero(q2))
  1236. X        return qlink(&_qzero_);
  1237. X    if (q1 == q2)
  1238. X        return qabs(q1);
  1239. X    if (qisunit(q1))
  1240. X        return qabs(q2);
  1241. X    if (qisunit(q2))
  1242. X        return qabs(q1);
  1243. X    q = qalloc();
  1244. X    zlcm(q1->num, q2->num, &q->num);
  1245. X    if (qisfrac(q1) || qisfrac(q2))
  1246. X        zgcd(q1->den, q2->den, &q->den);
  1247. X    return q;
  1248. X}
  1249. X
  1250. X
  1251. X/*
  1252. X * Remove all occurances of the specified factor from a number.
  1253. X * Returned number is always positive.
  1254. X */
  1255. XNUMBER *
  1256. Xqfacrem(q1, q2)
  1257. X    NUMBER *q1, *q2;
  1258. X{
  1259. X    long count;
  1260. X    ZVALUE tmp;
  1261. X    NUMBER *r;
  1262. X
  1263. X    if (qisfrac(q1) || qisfrac(q2))
  1264. X        math_error("Non-integers for factor removal");
  1265. X    count = zfacrem(q1->num, q2->num, &tmp);
  1266. X    if (zisunit(tmp)) {
  1267. X        zfree(tmp);
  1268. X        return qlink(&_qone_);
  1269. X    }
  1270. X    if (count == 0) {
  1271. X        zfree(tmp);
  1272. X        return qlink(q1);
  1273. X    }
  1274. X    r = qalloc();
  1275. X    r->num = tmp;
  1276. X    return r;
  1277. X}
  1278. X
  1279. X
  1280. X/*
  1281. X * Divide one number by the gcd of it with another number repeatedly until
  1282. X * the number is relatively prime.
  1283. X */
  1284. XNUMBER *
  1285. Xqgcdrem(q1, q2)
  1286. X    NUMBER *q1, *q2;
  1287. X{
  1288. X    ZVALUE tmp;
  1289. X    NUMBER *r;
  1290. X
  1291. X    if (qisfrac(q1) || qisfrac(q2))
  1292. X        math_error("Non-integers for gcdrem");
  1293. X    zgcdrem(q1->num, q2->num, &tmp);
  1294. X    if (zisunit(tmp)) {
  1295. X        zfree(tmp);
  1296. X        return qlink(&_qone_);
  1297. X    }
  1298. X    if (zcmp(q1->num, tmp) == 0) {
  1299. X        zfree(tmp);
  1300. X        return qlink(q1);
  1301. X    }
  1302. X    r = qalloc();
  1303. X    r->num = tmp;
  1304. X    return r;
  1305. X}
  1306. X
  1307. X
  1308. X/*
  1309. X * Return the lowest prime factor of a number.
  1310. X * Search is conducted for the specified number of primes.
  1311. X * Returns one if no factor was found.
  1312. X */
  1313. XNUMBER *
  1314. Xqlowfactor(q1, q2)
  1315. X    NUMBER *q1, *q2;
  1316. X{
  1317. X    if (qisfrac(q1) || qisfrac(q2))
  1318. X        math_error("Non-integers for lowfactor");
  1319. X    return itoq(zlowfactor(q1->num, ztoi(q2->num)));
  1320. X}
  1321. X
  1322. X
  1323. X/*
  1324. X * Return the number of places after the decimal point needed to exactly
  1325. X * represent the specified number as a real number.  Integers return zero,
  1326. X * and non-terminating decimals return minus one.  Examples:
  1327. X *    qplaces(1/7)=-1, qplaces(3/10)= 1, qplaces(1/8)=3, qplaces(4)=0.
  1328. X */
  1329. Xlong
  1330. Xqplaces(q)
  1331. X    NUMBER *q;
  1332. X{
  1333. X    long twopow, fivepow;
  1334. X    HALF fiveval[2];
  1335. X    ZVALUE five;
  1336. X    ZVALUE tmp;
  1337. X
  1338. X    if (qisint(q))    /* no decimal places if number is integer */
  1339. X        return 0;
  1340. X    /*
  1341. X     * The number of decimal places of a fraction in lowest terms is finite
  1342. X     * if an only if the denominator is of the form 2^A * 5^B, and then the
  1343. X     * number of decimal places is equal to MAX(A, B).
  1344. X     */
  1345. X    five.sign = 0;
  1346. X    five.len = 1;
  1347. X    five.v = fiveval;
  1348. X    fiveval[0] = 5;
  1349. X    fivepow = zfacrem(q->den, five, &tmp);
  1350. X    if (!zisonebit(tmp)) {
  1351. X        zfree(tmp);
  1352. X        return -1;
  1353. X    }
  1354. X    twopow = zlowbit(tmp);
  1355. X    zfree(tmp);
  1356. X    if (twopow < fivepow)
  1357. X        twopow = fivepow;
  1358. X    return twopow;
  1359. X}
  1360. X
  1361. X
  1362. X/*
  1363. X * Perform a probabilistic primality test (algorithm P in Knuth).
  1364. X * Returns FALSE if definitely not prime, or TRUE if probably prime.
  1365. X * Second arg determines how many times to check for primality.
  1366. X */
  1367. XBOOL
  1368. Xqprimetest(q1, q2)
  1369. X    NUMBER *q1, *q2;
  1370. X{
  1371. X    if (qisfrac(q1) || qisfrac(q2) || qisneg(q2))
  1372. X        math_error("Bad arguments for qprimetest");
  1373. X    return zprimetest(q1->num, qtoi(q2));
  1374. X}
  1375. X
  1376. X
  1377. X/*
  1378. X * Return a trivial hash value for a number.
  1379. X */
  1380. XHASH
  1381. Xqhash(q)
  1382. X    NUMBER *q;
  1383. X{
  1384. X    HASH hash;
  1385. X
  1386. X    hash = zhash(q->num);
  1387. X    if (qisfrac(q))
  1388. X        hash += zhash(q->den) * 2000003;
  1389. X    return hash;
  1390. X}
  1391. X
  1392. X/* END CODE */
  1393. SHAR_EOF
  1394. chmod 0644 calc2.9.0/qfunc.c || echo "restore of calc2.9.0/qfunc.c fails"
  1395. set `wc -c calc2.9.0/qfunc.c`;Sum=$1
  1396. if test "$Sum" != "24730"
  1397. then echo original size 24730, current size $Sum;fi
  1398. echo "x - extracting calc2.9.0/qio.c (Text)"
  1399. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/qio.c &&
  1400. X/*
  1401. X * Copyright (c) 1993 David I. Bell
  1402. X * Permission is granted to use, distribute, or modify this source,
  1403. X * provided that this copyright notice remains intact.
  1404. X *
  1405. X * Scanf and printf routines for arbitrary precision rational numbers
  1406. X */
  1407. X
  1408. X#include "stdarg.h"
  1409. X#include "qmath.h"
  1410. X
  1411. X
  1412. X#define    PUTCHAR(ch)        math_chr(ch)
  1413. X#define    PUTSTR(str)        math_str(str)
  1414. X#define    PRINTF1(fmt, a1)    math_fmt(fmt, a1)
  1415. X#define    PRINTF2(fmt, a1, a2)    math_fmt(fmt, a1, a2)
  1416. X
  1417. X
  1418. X#if 0
  1419. Xstatic long    etoalen;
  1420. Xstatic char    *etoabuf = NULL;
  1421. X#endif
  1422. X
  1423. Xstatic long    scalefactor;
  1424. Xstatic ZVALUE    scalenumber = { 0, 0, 0 };
  1425. X
  1426. X
  1427. X/*
  1428. X * Print a formatted string containing arbitrary numbers, similar to printf.
  1429. X * ALL numeric arguments to this routine are rational NUMBERs.
  1430. X * Various forms of printing such numbers are supplied, in addition
  1431. X * to strings and characters.  Output can actually be to any FILE
  1432. X * stream or a string.
  1433. X */
  1434. X#ifdef VARARGS
  1435. X# define VA_ALIST1 fmt, va_alist
  1436. X# define VA_DCL1 char *fmt; va_dcl
  1437. X#else
  1438. X# ifdef __STDC__
  1439. X#  define VA_ALIST1 char *fmt, ...
  1440. X#  define VA_DCL1
  1441. X# else
  1442. X#  define VA_ALIST1 fmt
  1443. X#  define VA_DCL1 char *fmt;
  1444. X# endif
  1445. X#endif
  1446. X/*VARARGS*/
  1447. Xvoid
  1448. Xqprintf(VA_ALIST1)
  1449. X    VA_DCL1
  1450. X{
  1451. X    va_list ap;
  1452. X    NUMBER *q;
  1453. X    int ch, sign;
  1454. X    long width, precision;
  1455. X
  1456. X#ifdef VARARGS
  1457. X    va_start(ap);
  1458. X#else
  1459. X    va_start(ap, fmt);
  1460. X#endif
  1461. X    while ((ch = *fmt++) != '\0') {
  1462. X        if (ch == '\\') {
  1463. X            ch = *fmt++;
  1464. X            switch (ch) {
  1465. X                case 'n': ch = '\n'; break;
  1466. X                case 'r': ch = '\r'; break;
  1467. X                case 't': ch = '\t'; break;
  1468. X                case 'f': ch = '\f'; break;
  1469. X                case 'v': ch = '\v'; break;
  1470. X                case 'b': ch = '\b'; break;
  1471. X                case 0:
  1472. X                    va_end(ap);
  1473. X                    return;
  1474. X            }
  1475. X            PUTCHAR(ch);
  1476. X            continue;
  1477. X        }
  1478. X        if (ch != '%') {
  1479. X            PUTCHAR(ch);
  1480. X            continue;
  1481. X        }
  1482. X        ch = *fmt++;
  1483. X        width = 0; precision = 8; sign = 1;
  1484. Xpercent:    ;
  1485. X        switch (ch) {
  1486. X            case 'd':
  1487. X                q = va_arg(ap, NUMBER *);
  1488. X                qprintfd(q, width);
  1489. X                break;
  1490. X            case 'f':
  1491. X                q = va_arg(ap, NUMBER *);
  1492. X                qprintff(q, width, precision);
  1493. X                break;
  1494. X            case 'e':
  1495. X                q = va_arg(ap, NUMBER *);
  1496. X                qprintfe(q, width, precision);
  1497. X                break;
  1498. X            case 'r':
  1499. X            case 'R':
  1500. X                q = va_arg(ap, NUMBER *);
  1501. X                qprintfr(q, width, (BOOL) (ch == 'R'));
  1502. X                break;
  1503. X            case 'N':
  1504. X                q = va_arg(ap, NUMBER *);
  1505. X                zprintval(q->num, 0L, width);
  1506. X                break;
  1507. X            case 'D':
  1508. X                q = va_arg(ap, NUMBER *);
  1509. X                zprintval(q->den, 0L, width);
  1510. X                break;
  1511. X            case 'o':
  1512. X                q = va_arg(ap, NUMBER *);
  1513. X                qprintfo(q, width);
  1514. X                break;
  1515. X            case 'x':
  1516. X                q = va_arg(ap, NUMBER *);
  1517. X                qprintfx(q, width);
  1518. X                break;
  1519. X            case 'b':
  1520. X                q = va_arg(ap, NUMBER *);
  1521. X                qprintfb(q, width);
  1522. X                break;
  1523. X            case 's':
  1524. X                PUTSTR(va_arg(ap, char *));
  1525. X                break;
  1526. X            case 'c':
  1527. X                PUTCHAR(va_arg(ap, int));
  1528. X                break;
  1529. X            case 0:
  1530. X                va_end(ap);
  1531. X                return;
  1532. X            case '-':
  1533. X                sign = -1;
  1534. X                ch = *fmt++;
  1535. X            default:
  1536. X        if (('0' <= ch && ch <= '9') || ch == '.' || ch == '*') {
  1537. X            if (ch == '*') {
  1538. X                q = va_arg(ap, NUMBER *);
  1539. X                width = sign * qtoi(q);
  1540. X                ch = *fmt++;
  1541. X            } else if (ch != '.') {
  1542. X                width = ch - '0';
  1543. X                while ('0' <= (ch = *fmt++) && ch <= '9')
  1544. X                    width = width * 10 + ch - '0';
  1545. X                width *= sign;
  1546. X            }
  1547. X            if (ch == '.') {
  1548. X                if ((ch = *fmt++) == '*') {
  1549. X                    q = va_arg(ap, NUMBER *);
  1550. X                    precision = qtoi(q);
  1551. X                    ch = *fmt++;
  1552. X                } else {
  1553. X                    precision = 0;
  1554. X                    while ('0' <= (ch = *fmt++) && ch <= '9')
  1555. X                        precision = precision * 10 + ch - '0';
  1556. X                }
  1557. X            }
  1558. X            goto percent;
  1559. X        }
  1560. X        }
  1561. X    }
  1562. X    va_end(ap);
  1563. X}
  1564. X
  1565. X
  1566. X#if 0
  1567. X/*
  1568. X * Read a number from the specified FILE stream (NULL means stdin).
  1569. X * The number can be an integer, a fraction, a real number, an
  1570. X * exponential number, or a hex, octal or binary number.  Leading blanks
  1571. X * are skipped.  Illegal numbers return NULL.  Unrecognized characters
  1572. X * remain to be read on the line.
  1573. X *    q = qreadval(fp);
  1574. X */
  1575. XNUMBER *
  1576. Xqreadval(fp)
  1577. X    FILE *fp;        /* file stream to read from (or NULL) */
  1578. X{
  1579. X    NUMBER *r;        /* returned number */
  1580. X    char *cp;         /* current buffer location */
  1581. X    long savecc;        /* characters saved in buffer */
  1582. X    long scancc;         /* characters parsed correctly */
  1583. X    int ch;            /* current character */
  1584. X
  1585. X    if (fp == NULL)
  1586. X        fp = stdin;
  1587. X    if (etoabuf == NULL) {
  1588. X        etoabuf = (char *)malloc(OUTBUFSIZE + 2);
  1589. X        if (etoabuf == NULL)
  1590. X            return NULL;
  1591. X        etoalen = OUTBUFSIZE;
  1592. X    }
  1593. X    cp = etoabuf;
  1594. X    ch = fgetc(fp);
  1595. X    while ((ch == ' ') || (ch == '\t'))
  1596. X        ch = fgetc(fp);
  1597. X    savecc = 0;
  1598. X    for (;;) {
  1599. X        if (ch == EOF)
  1600. X            return NULL;
  1601. X        if (savecc >= etoalen)
  1602. X        {
  1603. X            cp = (char *)realloc(etoabuf, etoalen + OUTBUFSIZE + 2);
  1604. X            if (cp == NULL)
  1605. X                return NULL;
  1606. X            etoabuf = cp;
  1607. X            etoalen += OUTBUFSIZE;
  1608. X            cp += savecc;
  1609. X        }
  1610. X        *cp++ = (char)ch;
  1611. X        *cp = '\0';
  1612. X        scancc = qparse(etoabuf, QPF_SLASH);
  1613. X        if (scancc != ++savecc)
  1614. X            break;
  1615. X        ch = fgetc(fp);
  1616. X    }
  1617. X    ungetc(ch, fp);
  1618. X    if (scancc < 0)
  1619. X        return NULL;
  1620. X    r = atoq(etoabuf);
  1621. X    if (ziszero(r->den)) {
  1622. X        qfree(r);
  1623. X        r = NULL;
  1624. X    }
  1625. X    return r;
  1626. X}
  1627. X#endif
  1628. X
  1629. X
  1630. X/*
  1631. X * Print a number in the specified output mode.
  1632. X * If MODE_DEFAULT is given, then the default output mode is used.
  1633. X * Any approximate output is flagged with a leading tilde.
  1634. X * Integers are always printed as themselves.
  1635. X */
  1636. Xvoid
  1637. Xqprintnum(q, outmode)
  1638. X    NUMBER *q;
  1639. X{
  1640. X    NUMBER tmpval;
  1641. X    long prec, exp;
  1642. X
  1643. X    if (outmode == MODE_DEFAULT)
  1644. X        outmode = _outmode_;
  1645. X    if ((outmode == MODE_FRAC) || ((outmode == MODE_REAL) && qisint(q))) {
  1646. X        qprintfr(q, 0L, FALSE);
  1647. X        return;
  1648. X    }
  1649. X    switch (outmode) {
  1650. X        case MODE_INT:
  1651. X            if (qisfrac(q))
  1652. X                PUTCHAR('~');
  1653. X            qprintfd(q, 0L);
  1654. X            break;
  1655. X
  1656. X        case MODE_REAL:
  1657. X            prec = qplaces(q);
  1658. X            if ((prec < 0) || (prec > _outdigits_)) {
  1659. X                prec = _outdigits_;
  1660. X                PUTCHAR('~');
  1661. X            }
  1662. X            qprintff(q, 0L, prec);
  1663. X            break;
  1664. X
  1665. X        case MODE_EXP:
  1666. X            if (qiszero(q)) {
  1667. X                PUTCHAR('0');
  1668. X                return;
  1669. X            }
  1670. X            tmpval = *q;
  1671. X            tmpval.num.sign = 0;
  1672. X            exp = qilog10(&tmpval);
  1673. X            if (exp == 0) {        /* in range to output as real */
  1674. X                qprintnum(q, MODE_REAL);
  1675. X                return;
  1676. X            }
  1677. X            tmpval.num = _one_;
  1678. X            tmpval.den = _one_;
  1679. X            if (exp > 0)
  1680. X                ztenpow(exp, &tmpval.den);
  1681. X            else
  1682. X                ztenpow(-exp, &tmpval.num);
  1683. X            q = qmul(q, &tmpval);
  1684. X            zfree(tmpval.num);
  1685. X            zfree(tmpval.den);
  1686. X            qprintnum(q, MODE_REAL);
  1687. X            qfree(q);
  1688. X            PRINTF1("e%ld", exp);
  1689. X            break;
  1690. X
  1691. X        case MODE_HEX:
  1692. X            qprintfx(q, 0L);
  1693. X            break;
  1694. X
  1695. X        case MODE_OCTAL:
  1696. X            qprintfo(q, 0L);
  1697. X            break;
  1698. X
  1699. X        case MODE_BINARY:
  1700. X            qprintfb(q, 0L);
  1701. X            break;
  1702. X
  1703. X        default:
  1704. X            math_error("Bad mode for print");
  1705. X    }
  1706. X}
  1707. X
  1708. X
  1709. X/*
  1710. X * Print a number in floating point representation.
  1711. X * Example:  193.784
  1712. X */
  1713. Xvoid
  1714. Xqprintff(q, width, precision)
  1715. X    NUMBER *q;
  1716. X    long width;
  1717. X    long precision;
  1718. X{
  1719. X    ZVALUE z, z1;
  1720. X
  1721. X    if (precision != scalefactor) {
  1722. X        if (scalenumber.v)
  1723. X            zfree(scalenumber);
  1724. X        ztenpow(precision, &scalenumber);
  1725. X        scalefactor = precision;
  1726. X    }
  1727. X    if (scalenumber.v)
  1728. X        zmul(q->num, scalenumber, &z);
  1729. X    else
  1730. X        z = q->num;
  1731. X    if (qisfrac(q)) {
  1732. X        zquo(z, q->den, &z1);
  1733. X        if (z.v != q->num.v)
  1734. X            zfree(z);
  1735. X        z = z1;
  1736. X    }
  1737. X    if (qisneg(q) && ziszero(z))
  1738. X        PUTCHAR('-');
  1739. X    zprintval(z, precision, width);
  1740. X    if (z.v != q->num.v)
  1741. X        zfree(z);
  1742. X}
  1743. X
  1744. X
  1745. X/*
  1746. X * Print a number in exponential notation.
  1747. X * Example: 4.1856e34
  1748. X */
  1749. X/*ARGSUSED*/
  1750. Xvoid
  1751. Xqprintfe(q, width, precision)
  1752. X    register NUMBER *q;
  1753. X    long width;
  1754. X    long precision;
  1755. X{
  1756. X    long exponent;
  1757. X    NUMBER q2;
  1758. X    ZVALUE num, den, tenpow, tmp;
  1759. X
  1760. X    if (qiszero(q)) {
  1761. X        PUTSTR("0.0");
  1762. X        return;
  1763. X    }
  1764. X    num = q->num;
  1765. X    den = q->den;
  1766. X    num.sign = 0;
  1767. X    exponent = zdigits(num) - zdigits(den);
  1768. X    if (exponent > 0) {
  1769. X        ztenpow(exponent, &tenpow);
  1770. X        zmul(den, tenpow, &tmp);
  1771. X        zfree(tenpow);
  1772. X        den = tmp;
  1773. X    }
  1774. X    if (exponent < 0) {
  1775. X        ztenpow(-exponent, &tenpow);
  1776. X        zmul(num, tenpow, &tmp);
  1777. X        zfree(tenpow);
  1778. X        num = tmp;
  1779. X    }
  1780. X    if (zrel(num, den) < 0) {
  1781. X        zmuli(num, 10L, &tmp);
  1782. X        if (num.v != q->num.v)
  1783. X            zfree(num);
  1784. X        num = tmp;
  1785. X        exponent--;
  1786. X    }
  1787. X    q2.num = num;
  1788. X    q2.den = den;
  1789. X    q2.num.sign = q->num.sign;
  1790. X    qprintff(&q2, 0L, precision);
  1791. X    if (exponent)
  1792. X        PRINTF1("e%ld", exponent);
  1793. X    if (num.v != q->num.v)
  1794. X        zfree(num);
  1795. X    if (den.v != q->den.v)
  1796. X        zfree(den);
  1797. X}
  1798. X
  1799. X
  1800. X/*
  1801. X * Print a number in rational representation.
  1802. X * Example: 397/37
  1803. X */
  1804. Xvoid
  1805. Xqprintfr(q, width, force)
  1806. X    NUMBER *q;
  1807. X    long width;
  1808. X    BOOL force;
  1809. X{
  1810. X    zprintval(q->num, 0L, width);
  1811. X    if (force || qisfrac(q)) {
  1812. X        PUTCHAR('/');
  1813. X        zprintval(q->den, 0L, width);
  1814. X    }
  1815. X}
  1816. X
  1817. X
  1818. X/*
  1819. X * Print a number as an integer (truncating fractional part).
  1820. X * Example: 958421
  1821. X */
  1822. Xvoid
  1823. Xqprintfd(q, width)
  1824. X    NUMBER *q;
  1825. X    long width;
  1826. X{
  1827. X    ZVALUE z;
  1828. X
  1829. X    if (qisfrac(q)) {
  1830. X        zquo(q->num, q->den, &z);
  1831. X        zprintval(z, 0L, width);
  1832. X        zfree(z);
  1833. X    } else
  1834. X        zprintval(q->num, 0L, width);
  1835. X}
  1836. X
  1837. X
  1838. X/*
  1839. X * Print a number in hex.
  1840. X * This prints the numerator and denominator in hex.
  1841. X */
  1842. Xvoid
  1843. Xqprintfx(q, width)
  1844. X    NUMBER *q;
  1845. X    long width;
  1846. X{
  1847. X    zprintx(q->num, width);
  1848. X    if (qisfrac(q)) {
  1849. X        PUTCHAR('/');
  1850. X        zprintx(q->den, 0L);
  1851. X    }
  1852. X}
  1853. X
  1854. X
  1855. X/*
  1856. X * Print a number in binary.
  1857. X * This prints the numerator and denominator in binary.
  1858. X */
  1859. Xvoid
  1860. Xqprintfb(q, width)
  1861. X    NUMBER *q;
  1862. X    long width;
  1863. X{
  1864. X    zprintb(q->num, width);
  1865. X    if (qisfrac(q)) {
  1866. X        PUTCHAR('/');
  1867. X        zprintb(q->den, 0L);
  1868. X    }
  1869. X}
  1870. X
  1871. X
  1872. X/*
  1873. X * Print a number in octal.
  1874. X * This prints the numerator and denominator in octal.
  1875. X */
  1876. Xvoid
  1877. Xqprintfo(q, width)
  1878. X    NUMBER *q;
  1879. X    long width;
  1880. X{
  1881. X    zprinto(q->num, width);
  1882. X    if (qisfrac(q)) {
  1883. X        PUTCHAR('/');
  1884. X        zprinto(q->den, 0L);
  1885. X    }
  1886. X}
  1887. X
  1888. X
  1889. X/*
  1890. X * Convert a string to a number in rational, floating point,
  1891. X * exponential notation, hex, or octal.
  1892. X *    q = atoq(string);
  1893. X */
  1894. XNUMBER *
  1895. Xatoq(s)
  1896. X    register char *s;
  1897. X{
  1898. X    register NUMBER *q;
  1899. X    register char *t;
  1900. X    ZVALUE div, newnum, newden, tmp;
  1901. X    long decimals, exp;
  1902. X    BOOL hex, negexp;
  1903. X
  1904. X    q = qalloc();
  1905. X    decimals = 0;
  1906. X    exp = 0;
  1907. X    negexp = FALSE;
  1908. X    hex = FALSE;
  1909. X    t = s;
  1910. X    if ((*t == '+') || (*t == '-'))
  1911. X        t++;
  1912. X    if ((*t == '0') && ((t[1] == 'x') || (t[1] == 'X'))) {
  1913. X        hex = TRUE;
  1914. X        t += 2;
  1915. X    }
  1916. X    while (((*t >= '0') && (*t <= '9')) || (hex &&
  1917. X        (((*t >= 'a') && (*t <= 'f')) || ((*t >= 'A') && (*t <= 'F')))))
  1918. X            t++;
  1919. X    if (*t == '/') {
  1920. X        t++;
  1921. X        atoz(t, &q->den);
  1922. X    } else if ((*t == '.') || (*t == 'e') || (*t == 'E')) {
  1923. X        if (*t == '.') {
  1924. X            t++;
  1925. X            while ((*t >= '0') && (*t <= '9')) {
  1926. X                t++;
  1927. X                decimals++;
  1928. X            }
  1929. X        }
  1930. X        /*
  1931. X         * Parse exponent if any
  1932. X         */
  1933. X        if ((*t == 'e') || (*t == 'E')) {
  1934. X            t++;
  1935. X            if (*t == '+')
  1936. X                t++;
  1937. X            else if (*t == '-') {
  1938. X                negexp = TRUE;
  1939. X                t++;
  1940. X            }
  1941. X            while ((*t >= '0') && (*t <= '9')) {
  1942. X                exp = (exp * 10) + *t++ - '0';
  1943. X                if (exp > 1000000)
  1944. X                    math_error("Exponent too large");
  1945. X            }
  1946. X        }
  1947. X        ztenpow(decimals, &q->den);
  1948. X    }
  1949. X    atoz(s, &q->num);
  1950. X    if (qiszero(q)) {
  1951. X        qfree(q);
  1952. X        return qlink(&_qzero_);
  1953. X    }
  1954. X    /*
  1955. X     * Apply the exponential if any
  1956. X     */
  1957. X    if (exp) {
  1958. X        ztenpow(exp, &tmp);
  1959. X        if (negexp) {
  1960. X            zmul(q->den, tmp, &newden);
  1961. X            zfree(q->den);
  1962. X            q->den = newden;
  1963. X        } else {
  1964. X            zmul(q->num, tmp, &newnum);
  1965. X            zfree(q->num);
  1966. X            q->num = newnum;
  1967. X        }
  1968. X        zfree(tmp);
  1969. X    }
  1970. X    /*
  1971. X     * Reduce the fraction to lowest terms
  1972. X     */
  1973. X    if (zisunit(q->num) || zisunit(q->den))
  1974. X        return q;
  1975. X    zgcd(q->num, q->den, &div);
  1976. X    if (zisunit(div))
  1977. X        return q;
  1978. X    zquo(q->num, div, &newnum);
  1979. X    zfree(q->num);
  1980. X    zquo(q->den, div, &newden);
  1981. X    zfree(q->den);
  1982. X    q->num = newnum;
  1983. X    q->den = newden;
  1984. X    return q;
  1985. X}
  1986. X
  1987. X
  1988. X/*
  1989. X * Parse a number in any of the various legal forms, and return the count
  1990. X * of characters that are part of a legal number.  Numbers can be either a
  1991. X * decimal integer, possibly two decimal integers separated with a slash, a
  1992. X * floating point or exponential number, a hex number beginning with "0x",
  1993. X * a binary number beginning with "0b", or an octal number beginning with "0".
  1994. X * The flags argument modifies the end of number testing for ease in handling
  1995. X * fractions or complex numbers.  Minus one is returned if the number format
  1996. X * is definitely illegal.
  1997. X */
  1998. Xlong
  1999. Xqparse(cp, flags)
  2000. X    register char *cp;
  2001. X{
  2002. X    char *oldcp;
  2003. X
  2004. X    oldcp = cp;
  2005. X    if ((*cp == '+') || (*cp == '-'))
  2006. X        cp++;
  2007. X    if ((*cp == '+') || (*cp == '-'))
  2008. X        return -1;
  2009. X    if ((*cp == '0') && ((cp[1] == 'x') || (cp[1] == 'X'))) {    /* hex */
  2010. X        cp += 2;
  2011. X        while (((*cp >= '0') && (*cp <= '9')) ||
  2012. X            ((*cp >= 'a') && (*cp <= 'f')) ||
  2013. X            ((*cp >= 'A') && (*cp <= 'F')))
  2014. X                cp++;
  2015. X        goto done;
  2016. X    }
  2017. X    if ((*cp == '0') && ((cp[1] == 'b') || (cp[1] == 'B'))) {    /* binary */
  2018. X        cp += 2;
  2019. X        while ((*cp == '0') || (*cp == '1'))
  2020. X            cp++;
  2021. X        goto done;
  2022. X    }
  2023. X    if ((*cp == '0') && (cp[1] >= '0') && (cp[1] <= '9')) { /* octal */
  2024. X        while ((*cp >= '0') && (*cp <= '7'))
  2025. X            cp++;
  2026. X        goto done;
  2027. X    }
  2028. X    /*
  2029. X     * Number is decimal, but can still be a fraction or real or exponential.
  2030. X     */
  2031. X    while ((*cp >= '0') && (*cp <= '9'))
  2032. X        cp++;
  2033. X    if (*cp == '/' && flags & QPF_SLASH) {    /* fraction */
  2034. X        cp++;
  2035. X        while ((*cp >= '0') && (*cp <= '9'))
  2036. X            cp++;
  2037. X        goto done;
  2038. X    }
  2039. X    if (*cp == '.') {    /* floating point */
  2040. X        cp++;
  2041. X        while ((*cp >= '0') && (*cp <= '9'))
  2042. X            cp++;
  2043. X    }
  2044. X    if ((*cp == 'e') || (*cp == 'E')) {    /* exponential */
  2045. X        cp++;
  2046. X        if ((*cp == '+') || (*cp == '-'))
  2047. X            cp++;
  2048. X        if ((*cp == '+') || (*cp == '-'))
  2049. X            return -1;
  2050. X        while ((*cp >= '0') && (*cp <= '9'))
  2051. X            cp++;
  2052. X    }
  2053. X
  2054. Xdone:
  2055. X    if (((*cp == 'i') || (*cp == 'I')) && (flags & QPF_IMAG))
  2056. X        cp++;
  2057. X    if ((*cp == '.') || ((*cp == '/') && (flags & QPF_SLASH)) ||
  2058. X        ((*cp >= '0') && (*cp <= '9')) ||
  2059. X        ((*cp >= 'a') && (*cp <= 'z')) ||
  2060. X        ((*cp >= 'A') && (*cp <= 'Z')))
  2061. X            return -1;
  2062. X    return (cp - oldcp);
  2063. X}
  2064. X
  2065. X/* END CODE */
  2066. SHAR_EOF
  2067. chmod 0644 calc2.9.0/qio.c || echo "restore of calc2.9.0/qio.c fails"
  2068. set `wc -c calc2.9.0/qio.c`;Sum=$1
  2069. if test "$Sum" != "12895"
  2070. then echo original size 12895, current size $Sum;fi
  2071. echo "x - extracting calc2.9.0/qmath.c (Text)"
  2072. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/qmath.c &&
  2073. X/*
  2074. X * Copyright (c) 1993 David I. Bell
  2075. X * Permission is granted to use, distribute, or modify this source,
  2076. X * provided that this copyright notice remains intact.
  2077. X *
  2078. X * Extended precision rational arithmetic primitive routines
  2079. X */
  2080. X
  2081. X#include "qmath.h"
  2082. X
  2083. X
  2084. XNUMBER _qzero_ =    { { _zeroval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  2085. XNUMBER _qone_ =        { { _oneval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  2086. Xstatic NUMBER _qtwo_ =    { { _twoval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  2087. Xstatic NUMBER _qten_ =    { { _tenval_, 1, 0 }, { _oneval_, 1, 0 }, 1 };
  2088. XNUMBER _qnegone_ =    { { _oneval_, 1, 1 }, { _oneval_, 1, 0 }, 1 };
  2089. XNUMBER _qonehalf_ =    { { _oneval_, 1, 0 }, { _twoval_, 1, 0 }, 1 };
  2090. X
  2091. X
  2092. X/*
  2093. X * Create another copy of a number.
  2094. X *    q2 = qcopy(q1);
  2095. X */
  2096. XNUMBER *
  2097. Xqcopy(q)
  2098. X    register NUMBER *q;
  2099. X{
  2100. X    register NUMBER *r;
  2101. X
  2102. X    r = qalloc();
  2103. X    r->num.sign = q->num.sign;
  2104. X    if (!zisunit(q->num)) {
  2105. X        r->num.len = q->num.len;
  2106. X        r->num.v = alloc(r->num.len);
  2107. X        zcopyval(q->num, r->num);
  2108. X    }
  2109. X    if (!zisunit(q->den)) {
  2110. X        r->den.len = q->den.len;
  2111. X        r->den.v = alloc(r->den.len);
  2112. X        zcopyval(q->den, r->den);
  2113. X    }
  2114. X    return r;
  2115. X}
  2116. X
  2117. X
  2118. X/*
  2119. X * Convert a number to a normal integer.
  2120. X *    i = qtoi(q);
  2121. X */
  2122. Xlong
  2123. Xqtoi(q)
  2124. X    register NUMBER *q;
  2125. X{
  2126. X    long i;
  2127. X    ZVALUE res;
  2128. X
  2129. X    if (qisint(q))
  2130. X        return ztoi(q->num);
  2131. X    zquo(q->num, q->den, &res);
  2132. X    i = ztoi(res);
  2133. X    zfree(res);
  2134. X    return i;
  2135. X}
  2136. X
  2137. X
  2138. X/*
  2139. X * Convert a normal integer into a number.
  2140. X *    q = itoq(i);
  2141. X */
  2142. XNUMBER *
  2143. Xitoq(i)
  2144. X    long i;
  2145. X{
  2146. X    register NUMBER *q;
  2147. X
  2148. X    if ((i >= -1) && (i <= 10)) {
  2149. X        switch ((int) i) {
  2150. X            case 0: q = &_qzero_; break;
  2151. X            case 1: q = &_qone_; break;
  2152. X            case 2: q = &_qtwo_; break;
  2153. X            case 10: q = &_qten_; break;
  2154. X            case -1: q = &_qnegone_; break;
  2155. X            default: q = NULL;
  2156. X        }
  2157. X        if (q)
  2158. X            return qlink(q);
  2159. X    }
  2160. X    q = qalloc();
  2161. X    itoz(i, &q->num);
  2162. X    return q;
  2163. X}
  2164. X
  2165. X
  2166. X/*
  2167. X * Create a number from the given integral numerator and denominator.
  2168. X *    q = iitoq(inum, iden);
  2169. X */
  2170. XNUMBER *
  2171. Xiitoq(inum, iden)
  2172. X    long inum, iden;
  2173. X{
  2174. X    register NUMBER *q;
  2175. X    long d;
  2176. X    BOOL sign;
  2177. X
  2178. X    if (iden == 0)
  2179. X        math_error("Division by zero");
  2180. X    if (inum == 0)
  2181. X        return qlink(&_qzero_);
  2182. X    sign = 0;
  2183. X    if (inum < 0) {
  2184. X        sign = 1;
  2185. X        inum = -inum;
  2186. X    }
  2187. X    if (iden < 0) {
  2188. X        sign = 1 - sign;
  2189. X        iden = -iden;
  2190. X    }
  2191. X    d = iigcd(inum, iden);
  2192. X    inum /= d;
  2193. X    iden /= d;
  2194. X    if (iden == 1)
  2195. X        return itoq(sign ? -inum : inum);
  2196. X    q = qalloc();
  2197. X    if (inum != 1)
  2198. X        itoz(inum, &q->num);
  2199. X    itoz(iden, &q->den);
  2200. X    q->num.sign = sign;
  2201. X    return q;
  2202. X}
  2203. X
  2204. X
  2205. X/*
  2206. X * Add two numbers to each other.
  2207. X *    q3 = qadd(q1, q2);
  2208. X */
  2209. XNUMBER *
  2210. Xqadd(q1, q2)
  2211. X    register NUMBER *q1, *q2;
  2212. X{
  2213. X    NUMBER *r;
  2214. X    ZVALUE t1, t2, temp, d1, d2, vpd1, upd1;
  2215. X
  2216. X    if (qiszero(q1))
  2217. X        return qlink(q2);
  2218. X    if (qiszero(q2))
  2219. X        return qlink(q1);
  2220. X    r = qalloc();
  2221. X    /*
  2222. X     * If either number is an integer, then the result is easy.
  2223. X     */
  2224. X    if (qisint(q1) && qisint(q2)) {
  2225. X        zadd(q1->num, q2->num, &r->num);
  2226. X        return r;
  2227. X    }
  2228. X    if (qisint(q2)) {
  2229. X        zmul(q1->den, q2->num, &temp);
  2230. X        zadd(q1->num, temp, &r->num);
  2231. X        zfree(temp);
  2232. X        zcopy(q1->den, &r->den);
  2233. X        return r;
  2234. X    }
  2235. X    if (qisint(q1)) {
  2236. X        zmul(q2->den, q1->num, &temp);
  2237. X        zadd(q2->num, temp, &r->num);
  2238. X        zfree(temp);
  2239. X        zcopy(q2->den, &r->den);
  2240. X        return r;
  2241. X    }
  2242. X    /*
  2243. X     * Both arguments are true fractions, so we need more work.
  2244. X     * If the denominators are relatively prime, then the answer is the
  2245. X     * straightforward cross product result with no need for reduction.
  2246. X     */
  2247. X    zgcd(q1->den, q2->den, &d1);
  2248. X    if (zisunit(d1)) {
  2249. X        zfree(d1);
  2250. X        zmul(q1->num, q2->den, &t1);
  2251. X        zmul(q1->den, q2->num, &t2);
  2252. X        zadd(t1, t2, &r->num);
  2253. X        zfree(t1);
  2254. X        zfree(t2);
  2255. X        zmul(q1->den, q2->den, &r->den);
  2256. X        return r;
  2257. X    }
  2258. X    /*
  2259. X     * The calculation is now more complicated.
  2260. X     * See Knuth Vol 2 for details.
  2261. X     */
  2262. X    zquo(q2->den, d1, &vpd1);
  2263. X    zquo(q1->den, d1, &upd1);
  2264. X    zmul(q1->num, vpd1, &t1);
  2265. X    zmul(q2->num, upd1, &t2);
  2266. X    zadd(t1, t2, &temp);
  2267. X    zfree(t1);
  2268. X    zfree(t2);
  2269. X    zfree(vpd1);
  2270. X    zgcd(temp, d1, &d2);
  2271. X    zfree(d1);
  2272. X    if (zisunit(d2)) {
  2273. X        zfree(d2);
  2274. X        r->num = temp;
  2275. X        zmul(upd1, q2->den, &r->den);
  2276. X        zfree(upd1);
  2277. X        return r;
  2278. X    }
  2279. X    zquo(temp, d2, &r->num);
  2280. X    zfree(temp);
  2281. X    zquo(q2->den, d2, &temp);
  2282. X    zfree(d2);
  2283. X    zmul(temp, upd1, &r->den);
  2284. X    zfree(temp);
  2285. X    zfree(upd1);
  2286. X    return r;
  2287. X}
  2288. X
  2289. X
  2290. X/*
  2291. X * Subtract one number from another.
  2292. X *    q3 = qsub(q1, q2);
  2293. X */
  2294. XNUMBER *
  2295. Xqsub(q1, q2)
  2296. X    register NUMBER *q1, *q2;
  2297. X{
  2298. X    NUMBER t, *r;
  2299. X
  2300. X    if (q1 == q2)
  2301. X        return qlink(&_qzero_);
  2302. X    if (qiszero(q2))
  2303. X        return qlink(q1);
  2304. X    if (qisint(q1) && qisint(q2)) {
  2305. X        r = qalloc();
  2306. X        zsub(q1->num, q2->num, &r->num);
  2307. X        return r;
  2308. X    }
  2309. X    t = *q2;
  2310. X    t.num.sign = !t.num.sign;
  2311. X    return qadd(q1, &t);
  2312. X}
  2313. X
  2314. X
  2315. X/*
  2316. X * Increment a number by one.
  2317. X */
  2318. XNUMBER *
  2319. Xqinc(q)
  2320. X    NUMBER *q;
  2321. X{
  2322. X    NUMBER *r;
  2323. X
  2324. X    r = qalloc();
  2325. X    if (qisint(q)) {
  2326. X        zadd(q->num, _one_, &r->num);
  2327. X        return r;
  2328. X    }
  2329. X    zadd(q->num, q->den, &r->num);
  2330. X    zcopy(q->den, &r->den);
  2331. X    return r;
  2332. X}
  2333. X
  2334. X
  2335. X/*
  2336. X * Decrement a number by one.
  2337. X */
  2338. XNUMBER *
  2339. Xqdec(q)
  2340. X    NUMBER *q;
  2341. X{
  2342. X    NUMBER *r;
  2343. X
  2344. X    r = qalloc();
  2345. X    if (qisint(q)) {
  2346. X        zsub(q->num, _one_, &r->num);
  2347. X        return r;
  2348. X    }
  2349. X    zsub(q->num, q->den, &r->num);
  2350. X    zcopy(q->den, &r->den);
  2351. X    return r;
  2352. X}
  2353. X
  2354. X
  2355. X/*
  2356. X * Add a normal small integer value to an arbitrary number.
  2357. X */
  2358. XNUMBER *
  2359. Xqaddi(q1, n)
  2360. X    NUMBER *q1;
  2361. X    long n;
  2362. X{
  2363. X    NUMBER addnum;        /* temporary number */
  2364. X    HALF addval[2];        /* value of small number */
  2365. X    BOOL neg;        /* TRUE if number is neg */
  2366. X
  2367. X    if (n == 0)
  2368. X        return qlink(q1);
  2369. X    if (n == 1)
  2370. X        return qinc(q1);
  2371. X    if (n == -1)
  2372. X        return qdec(q1);
  2373. X    if (qiszero(q1))
  2374. X        return itoq(n);
  2375. X    addnum.num.sign = 0;
  2376. X    addnum.num.len = 1;
  2377. X    addnum.num.v = addval;
  2378. X    addnum.den = _one_;
  2379. X    neg = (n < 0);
  2380. X    if (neg)
  2381. X        n = -n;
  2382. X    addval[0] = (HALF) n;
  2383. X    n = (((FULL) n) >> BASEB);
  2384. X    if (n) {
  2385. X        addval[1] = (HALF) n;
  2386. X        addnum.num.len = 2;
  2387. X    }
  2388. X    if (neg)
  2389. X        return qsub(q1, &addnum);
  2390. X    else
  2391. X        return qadd(q1, &addnum);
  2392. X}
  2393. X
  2394. X
  2395. X/*
  2396. X * Multiply two numbers.
  2397. X *    q3 = qmul(q1, q2);
  2398. X */
  2399. XNUMBER *
  2400. Xqmul(q1, q2)
  2401. X    register NUMBER *q1, *q2;
  2402. X{
  2403. X    NUMBER *r;            /* returned value */
  2404. X    ZVALUE n1, n2, d1, d2;        /* numerators and denominators */
  2405. X    ZVALUE tmp;
  2406. X
  2407. X    if (qiszero(q1) || qiszero(q2))
  2408. X        return qlink(&_qzero_);
  2409. X    if (qisone(q1))
  2410. X        return qlink(q2);
  2411. X    if (qisone(q2))
  2412. X        return qlink(q1);
  2413. X    if (qisint(q1) && qisint(q2)) {    /* easy results if integers */
  2414. X        r = qalloc();
  2415. X        zmul(q1->num, q2->num, &r->num);
  2416. X        return r;
  2417. X    }
  2418. X    n1 = q1->num;
  2419. X    n2 = q2->num;
  2420. X    d1 = q1->den;
  2421. X    d2 = q2->den;
  2422. X    if (ziszero(d1) || ziszero(d2))
  2423. X        math_error("Division by zero");
  2424. X    if (ziszero(n1) || ziszero(n2))
  2425. X        return qlink(&_qzero_);
  2426. X    if (!zisunit(n1) && !zisunit(d2)) {    /* possibly reduce */
  2427. X        zgcd(n1, d2, &tmp);
  2428. X        if (!zisunit(tmp)) {
  2429. X            zquo(q1->num, tmp, &n1);
  2430. X            zquo(q2->den, tmp, &d2);
  2431. X        }
  2432. X        zfree(tmp);
  2433. X    }
  2434. X    if (!zisunit(n2) && !zisunit(d1)) {    /* again possibly reduce */
  2435. X        zgcd(n2, d1, &tmp);
  2436. X        if (!zisunit(tmp)) {
  2437. X            zquo(q2->num, tmp, &n2);
  2438. X            zquo(q1->den, tmp, &d1);
  2439. X        }
  2440. X        zfree(tmp);
  2441. X    }
  2442. X    r = qalloc();
  2443. X    zmul(n1, n2, &r->num);
  2444. X    zmul(d1, d2, &r->den);
  2445. X    if (q1->num.v != n1.v)
  2446. X        zfree(n1);
  2447. X    if (q1->den.v != d1.v)
  2448. X        zfree(d1);
  2449. X    if (q2->num.v != n2.v)
  2450. X        zfree(n2);
  2451. X    if (q2->den.v != d2.v)
  2452. X        zfree(d2);
  2453. X    return r;
  2454. X}
  2455. X
  2456. X
  2457. X/*
  2458. X * Multiply a number by a small integer.
  2459. X *    q2 = qmuli(q1, n);
  2460. X */
  2461. XNUMBER *
  2462. Xqmuli(q, n)
  2463. X    NUMBER *q;
  2464. X    long n;
  2465. X{
  2466. X    NUMBER *r;
  2467. X    long d;            /* gcd of multiplier and denominator */
  2468. X    int sign;
  2469. X
  2470. X    if ((n == 0) || qiszero(q))
  2471. X        return qlink(&_qzero_);
  2472. X    if (n == 1)
  2473. X        return qlink(q);
  2474. X    r = qalloc();
  2475. X    if (qisint(q)) {
  2476. X        zmuli(q->num, n, &r->num);
  2477. X        return r;
  2478. X    }
  2479. X    sign = 1;
  2480. X    if (n < 0) {
  2481. X        n = -n;
  2482. X        sign = -1;
  2483. X    }
  2484. X    d = zmodi(q->den, n);
  2485. X    d = iigcd(d, n);
  2486. X    zmuli(q->num, (n * sign) / d, &r->num);
  2487. X    (void) zdivi(q->den, d, &r->den);
  2488. X    return r;
  2489. X}
  2490. X
  2491. X
  2492. X/*
  2493. X * Divide two numbers (as fractions).
  2494. X *    q3 = qdiv(q1, q2);
  2495. X */
  2496. XNUMBER *
  2497. Xqdiv(q1, q2)
  2498. X    register NUMBER *q1, *q2;
  2499. X{
  2500. X    NUMBER temp;
  2501. X
  2502. X    if (qiszero(q2))
  2503. X        math_error("Division by zero");
  2504. X    if ((q1 == q2) || !qcmp(q1, q2))
  2505. X        return qlink(&_qone_);
  2506. X    if (qisone(q1))
  2507. X        return qinv(q2);
  2508. X    temp.num = q2->den;
  2509. X    temp.den = q2->num;
  2510. X    temp.num.sign = temp.den.sign;
  2511. X    temp.den.sign = 0;
  2512. X    temp.links = 1;
  2513. X    return qmul(q1, &temp);
  2514. X}
  2515. X
  2516. X
  2517. X/*
  2518. X * Divide a number by a small integer.
  2519. X *    q2 = qdivi(q1, n);
  2520. X */
  2521. XNUMBER *
  2522. Xqdivi(q, n)
  2523. X    NUMBER *q;
  2524. X    long n;
  2525. X{
  2526. X    NUMBER *r;
  2527. X    long d;            /* gcd of divisor and numerator */
  2528. X    int sign;
  2529. X
  2530. X    if (n == 0)
  2531. X        math_error("Division by zero");
  2532. X    if ((n == 1) || qiszero(q))
  2533. X        return qlink(q);
  2534. X    sign = 1;
  2535. X    if (n < 0) {
  2536. X        n = -n;
  2537. X        sign = -1;
  2538. X    }
  2539. X    r = qalloc();
  2540. X    d = zmodi(q->num, n);
  2541. X    d = iigcd(d, n);
  2542. X    (void) zdivi(q->num, d * sign, &r->num);
  2543. X    zmuli(q->den, n / d, &r->den);
  2544. X    return r;
  2545. X}
  2546. X
  2547. X
  2548. X/*
  2549. X * Return the quotient when one number is divided by another.
  2550. X * This works for fractional values also, and in all cases:
  2551. X *    qquo(q1, q2) = int(q1 / q2).
  2552. X */
  2553. XNUMBER *
  2554. Xqquo(q1, q2)
  2555. X    register NUMBER *q1, *q2;
  2556. X{
  2557. X    ZVALUE num, den, res;
  2558. X    NUMBER *q;
  2559. X
  2560. X    if (zisunit(q1->num))
  2561. X        num = q2->den;
  2562. X    else if (zisunit(q2->den))
  2563. X        num = q1->num;
  2564. X    else
  2565. X        zmul(q1->num, q2->den, &num);
  2566. X    if (zisunit(q1->den))
  2567. X        den = q2->num;
  2568. X    else if (zisunit(q2->num))
  2569. X        den = q1->den;
  2570. X    else
  2571. X        zmul(q1->den, q2->num, &den);
  2572. X    zquo(num, den, &res);
  2573. X    if ((num.v != q2->den.v) && (num.v != q1->num.v))
  2574. X        zfree(num);
  2575. X    if ((den.v != q2->num.v) && (den.v != q1->den.v))
  2576. X        zfree(den);
  2577. X    if (ziszero(res)) {
  2578. X        zfree(res);
  2579. X        return qlink(&_qzero_);
  2580. X    }
  2581. X    res.sign = (q1->num.sign != q2->num.sign);
  2582. X    if (zisunit(res)) {
  2583. X        q = (res.sign ? &_qnegone_ : &_qone_);
  2584. X        zfree(res);
  2585. X        return qlink(q);
  2586. X    }
  2587. X    q = qalloc();
  2588. X    q->num = res;
  2589. X    return q;
  2590. X}
  2591. X
  2592. X
  2593. X/*
  2594. X * Return the absolute value of a number.
  2595. X *    q2 = qabs(q1);
  2596. X */
  2597. XNUMBER *
  2598. Xqabs(q)
  2599. X    register NUMBER *q;
  2600. X{
  2601. X    register NUMBER *r;
  2602. X
  2603. X    if (q->num.sign == 0)
  2604. X        return qlink(q);
  2605. X    r = qalloc();
  2606. X    if (!zisunit(q->num))
  2607. X        zcopy(q->num, &r->num);
  2608. X    if (!zisunit(q->den))
  2609. X        zcopy(q->den, &r->den);
  2610. X    r->num.sign = 0;
  2611. X    return r;
  2612. X}
  2613. X
  2614. X
  2615. X/*
  2616. X * Negate a number.
  2617. X *    q2 = qneg(q1);
  2618. X */
  2619. XNUMBER *
  2620. Xqneg(q)
  2621. X    register NUMBER *q;
  2622. X{
  2623. X    register NUMBER *r;
  2624. X
  2625. X    if (qiszero(q))
  2626. X        return qlink(&_qzero_);
  2627. X    r = qalloc();
  2628. X    if (!zisunit(q->num))
  2629. X        zcopy(q->num, &r->num);
  2630. X    if (!zisunit(q->den))
  2631. X        zcopy(q->den, &r->den);
  2632. X    r->num.sign = !q->num.sign;
  2633. X    return r;
  2634. X}
  2635. X
  2636. X
  2637. X/*
  2638. X * Return the sign of a number (-1, 0 or 1)
  2639. X */
  2640. XNUMBER *
  2641. Xqsign(q)
  2642. X    NUMBER *q;
  2643. X{
  2644. X    if (qiszero(q))
  2645. X        return qlink(&_qzero_);
  2646. X    if (qisneg(q))
  2647. X        return qlink(&_qnegone_);
  2648. X    return qlink(&_qone_);
  2649. X}
  2650. X
  2651. X
  2652. X/*
  2653. X * Invert a number.
  2654. X *    q2 = qinv(q1);
  2655. X */
  2656. XNUMBER *
  2657. Xqinv(q)
  2658. X    register NUMBER *q;
  2659. X{
  2660. X    register NUMBER *r;
  2661. X
  2662. X    if (qisunit(q)) {
  2663. X        r = (qisneg(q) ? &_qnegone_ : &_qone_);
  2664. X        return qlink(r);
  2665. X    }
  2666. X    if (qiszero(q))
  2667. X        math_error("Division by zero");
  2668. X    r = qalloc();
  2669. X    if (!zisunit(q->num))
  2670. X        zcopy(q->num, &r->den);
  2671. X    if (!zisunit(q->den))
  2672. X        zcopy(q->den, &r->num);
  2673. X    r->num.sign = q->num.sign;
  2674. X    r->den.sign = 0;
  2675. X    return r;
  2676. X}
  2677. X
  2678. X
  2679. X/*
  2680. X * Return just the numerator of a number.
  2681. X *    q2 = qnum(q1);
  2682. X */
  2683. XNUMBER *
  2684. Xqnum(q)
  2685. X    register NUMBER *q;
  2686. X{
  2687. X    register NUMBER *r;
  2688. X
  2689. X    if (qisint(q))
  2690. X        return qlink(q);
  2691. X    if (zisunit(q->num)) {
  2692. X        r = (qisneg(q) ? &_qnegone_ : &_qone_);
  2693. X        return qlink(r);
  2694. X    }
  2695. X    r = qalloc();
  2696. X    zcopy(q->num, &r->num);
  2697. X    return r;
  2698. X}
  2699. X
  2700. X
  2701. X/*
  2702. X * Return just the denominator of a number.
  2703. X *    q2 = qden(q1);
  2704. X */
  2705. XNUMBER *
  2706. Xqden(q)
  2707. X    register NUMBER *q;
  2708. X{
  2709. X    register NUMBER *r;
  2710. X
  2711. X    if (qisint(q))
  2712. X        return qlink(&_qone_);
  2713. X    r = qalloc();
  2714. X    zcopy(q->den, &r->num);
  2715. X    return r;
  2716. X}
  2717. X
  2718. X
  2719. X/*
  2720. X * Return the fractional part of a number.
  2721. X *    q2 = qfrac(q1);
  2722. X */
  2723. XNUMBER *
  2724. Xqfrac(q)
  2725. X    register NUMBER *q;
  2726. X{
  2727. X    register NUMBER *r;
  2728. X
  2729. X    if (qisint(q))
  2730. X        return qlink(&_qzero_);
  2731. X    if ((q->num.len < q->den.len) || ((q->num.len == q->den.len) &&
  2732. X        (q->num.v[q->num.len - 1] < q->den.v[q->num.len - 1])))
  2733. X            return qlink(q);
  2734. X    r = qalloc();
  2735. X    zmod(q->num, q->den, &r->num);
  2736. X    zcopy(q->den, &r->den);
  2737. X    r->num.sign = q->num.sign;
  2738. X    return r;
  2739. X}
  2740. X
  2741. X
  2742. X/*
  2743. X * Return the integral part of a number.
  2744. X *    q2 = qint(q1);
  2745. X */
  2746. XNUMBER *
  2747. Xqint(q)
  2748. X    register NUMBER *q;
  2749. X{
  2750. X    register NUMBER *r;
  2751. X
  2752. X    if (qisint(q))
  2753. X        return qlink(q);
  2754. X    if ((q->num.len < q->den.len) || ((q->num.len == q->den.len) &&
  2755. X        (q->num.v[q->num.len - 1] < q->den.v[q->num.len - 1])))
  2756. SHAR_EOF
  2757. echo "End of part 8"
  2758. echo "File calc2.9.0/qmath.c is continued in part 9"
  2759. echo "9" > s2_seq_.tmp
  2760. exit 0
  2761.