home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2240 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  59.1 KB

  1. From: glenn@extro.ucc.su.OZ.AU (Glenn Geers)
  2. Newsgroups: alt.sources
  3. Subject: 80386 alternative math library part03/04
  4. Message-ID: <1990Dec7.215849.668@metro.ucc.su.OZ.AU>
  5. Date: 7 Dec 90 21:58:49 GMT
  6.  
  7. Submitted-by: glenn@trantor
  8. Archive-name: libfpu/part03
  9.  
  10. #!/bin/sh
  11. # This is part 03 of libfpu
  12. # ============= paranoia.c ==============
  13. if test -f 'paranoia.c' -a X"$1" != X"-c"; then
  14.     echo 'x - skipping paranoia.c (File already exists)'
  15. else
  16. echo 'x - extracting paranoia.c (Text)'
  17. sed 's/^X//' << 'SHAR_EOF' > 'paranoia.c' &&
  18. /*    A C version of Kahan's Floating Point Test "Paranoia"
  19. X
  20. X            Thos Sumner, UCSF, Feb. 1985
  21. X            David Gay, BTL, Jan. 1986
  22. X
  23. X    This is a rewrite from the Pascal version by
  24. X
  25. X            B. A. Wichmann, 18 Jan. 1985
  26. X
  27. X    (and does NOT exhibit good C programming style).
  28. X
  29. (C) Apr 19 1983 in BASIC version by:
  30. X    Professor W. M. Kahan,
  31. X    567 Evans Hall
  32. X    Electrical Engineering & Computer Science Dept.
  33. X    University of California
  34. X    Berkeley, California 94720
  35. X    USA
  36. X
  37. converted to Pascal by:
  38. X    B. A. Wichmann
  39. X    National Physical Laboratory
  40. X    Teddington Middx
  41. X    TW11 OLW
  42. X    UK
  43. X
  44. converted to C by:
  45. X
  46. X    David M. Gay        and    Thos Sumner
  47. X    AT&T Bell Labs            Computer Center, Rm. U-76
  48. X    600 Mountain Avenue        University of California
  49. X    Murray Hill, NJ 07974        San Francisco, CA 94143
  50. X    USA                USA
  51. X
  52. with simultaneous corrections to the Pascal source (reflected
  53. in the Pascal source available over netlib).
  54. [A couple of bug fixes from dgh = sun!dhough incorporated 31 July 1986.]
  55. X
  56. Reports of results on various systems from all the versions
  57. of Paranoia are being collected by Richard Karpinski at the
  58. same address as Thos Sumner.  This includes sample outputs,
  59. bug reports, and criticisms.
  60. X
  61. You may copy this program freely if you acknowledge its source.
  62. Comments on the Pascal version to NPL, please.
  63. X
  64. X
  65. The C version catches signals from floating-point exceptions.
  66. If signal(SIGFPE,...) is unavailable in your environment, you may
  67. #define NOSIGNAL to comment out the invocations of signal.
  68. X
  69. This source file is too big for some C compilers, but may be split
  70. into pieces.  Comments containing "SPLIT" suggest convenient places
  71. for this splitting.  At the end of these comments is an "ed script"
  72. (for the UNIX(tm) editor ed) that will do this splitting.
  73. X
  74. By #defining Single when you compile this source, you may obtain
  75. a single-precision C version of Paranoia.
  76. X
  77. X
  78. The following is from the introductory commentary from Wichmann's work:
  79. X
  80. The BASIC program of Kahan is written in Microsoft BASIC using many
  81. facilities which have no exact analogy in Pascal.  The Pascal
  82. version below cannot therefore be exactly the same.  Rather than be
  83. a minimal transcription of the BASIC program, the Pascal coding
  84. follows the conventional style of block-structured languages.  Hence
  85. the Pascal version could be useful in producing versions in other
  86. structured languages.
  87. X
  88. Rather than use identifiers of minimal length (which therefore have
  89. little mnemonic significance), the Pascal version uses meaningful
  90. identifiers as follows [Note: A few changes have been made for C]:
  91. X
  92. X
  93. BASIC   C               BASIC   C               BASIC   C               
  94. X
  95. X   A                       J                       S    StickyBit
  96. X   A1   AInverse           J0   NoErrors           T
  97. X   B    Radix                    [Failure]         T0   Underflow
  98. X   B1   BInverse           J1   NoErrors           T2   ThirtyTwo
  99. X   B2   RadixD2                  [SeriousDefect]   T5   OneAndHalf
  100. X   B9   BMinusU2           J2   NoErrors           T7   TwentySeven
  101. X   C                             [Defect]          T8   TwoForty
  102. X   C1   CInverse           J3   NoErrors           U    OneUlp
  103. X   D                             [Flaw]            U0   UnderflowThreshold
  104. X   D4   FourD              K    PageNo             U1
  105. X   E0                      L    Milestone          U2
  106. X   E1                      M                       V
  107. X   E2   Exp2               N                       V0
  108. X   E3                      N1                      V8
  109. X   E5   MinSqEr            O    Zero               V9
  110. X   E6   SqEr               O1   One                W
  111. X   E7   MaxSqEr            O2   Two                X
  112. X   E8                      O3   Three              X1
  113. X   E9                      O4   Four               X8
  114. X   F1   MinusOne           O5   Five               X9   Random1
  115. X   F2   Half               O8   Eight              Y
  116. X   F3   Third              O9   Nine               Y1
  117. X   F6                      P    Precision          Y2
  118. X   F9                      Q                       Y9   Random2
  119. X   G1   GMult              Q8                      Z
  120. X   G2   GDiv               Q9                      Z0   PseudoZero
  121. X   G3   GAddSub            R                       Z1
  122. X   H                       R1   RMult              Z2
  123. X   H1   HInverse           R2   RDiv               Z9
  124. X   I                       R3   RAddSub
  125. X   IO   NoTrials           R4   RSqrt
  126. X   I3   IEEE               R9   Random9
  127. X
  128. X   SqRWrng
  129. X
  130. All the variables in BASIC are true variables and in consequence,
  131. the program is more difficult to follow since the "constants" must
  132. be determined (the glossary is very helpful).  The Pascal version
  133. uses Real constants, but checks are added to ensure that the values
  134. are correctly converted by the compiler.
  135. X
  136. The major textual change to the Pascal version apart from the
  137. identifiersis that named procedures are used, inserting parameters
  138. wherehelpful.  New procedures are also introduced.  The
  139. correspondence is as follows:
  140. X
  141. X
  142. BASIC       Pascal
  143. lines 
  144. X
  145. X  90- 140   Pause
  146. X 170- 250   Instructions
  147. X 380- 460   Heading
  148. X 480- 670   Characteristics
  149. X 690- 870   History
  150. 2940-2950   Random
  151. 3710-3740   NewD
  152. 4040-4080   DoesYequalX
  153. 4090-4110   PrintIfNPositive
  154. 4640-4850   TestPartialUnderflow
  155. X
  156. =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  157. X
  158. Below is an "ed script" that splits para.c into 10 files
  159. of the form part[1-8].c, subs.c, and msgs.c, plus a header
  160. file, paranoia.h, that these files require.
  161. X
  162. r paranoia.c
  163. $
  164. ?SPLIT
  165. +,$w msgs.c
  166. X .,$d
  167. ?SPLIT
  168. X .d
  169. +d
  170. -,$w subs.c
  171. -,$d
  172. ?part8
  173. +d
  174. ?include
  175. X .,$w part8.c
  176. X .,$d
  177. -d
  178. ?part7
  179. +d
  180. ?include
  181. X .,$w part7.c
  182. X .,$d
  183. -d
  184. ?part6
  185. +d
  186. ?include
  187. X .,$w part6.c
  188. X .,$d
  189. -d
  190. ?part5
  191. +d
  192. ?include
  193. X .,$w part5.c
  194. X .,$d
  195. -d
  196. ?part4
  197. +d
  198. ?include
  199. X .,$w part4.c
  200. X .,$d
  201. -d
  202. ?part3
  203. +d
  204. ?include
  205. X .,$w part3.c
  206. X .,$d
  207. -d
  208. ?part2
  209. +d
  210. ?include
  211. X .,$w part2.c
  212. X .,$d
  213. ?SPLIT
  214. X .d
  215. 1,/^#include/-1d
  216. 1,$w part1.c
  217. /Computed constants/,$d
  218. 1,$s/^int/extern &/
  219. 1,$s/^FLOAT/extern &/
  220. 1,$s/^char/extern &/
  221. 1,$s! = .*!;!
  222. /^Guard/,/^Round/s/^/extern /
  223. /^jmp_buf/s/^/extern /
  224. /^Sig_type/s/^/extern /
  225. s/$/\
  226. extern void sigfpe();/
  227. w paranoia.h
  228. q
  229. X
  230. */
  231. X
  232. #include <stdio.h>
  233. #ifndef NOSIGNAL
  234. #include <signal.h>
  235. #endif
  236. #include <setjmp.h>
  237. X
  238. extern double fabs(), floor(), log(), pow();
  239. #ifdef TEST
  240. extern double sqrtp();
  241. #else
  242. extern double sqrt();
  243. #endif
  244. X
  245. #ifdef Single
  246. #define FLOAT float
  247. #define FABS(x) (float)fabs((double)(x))
  248. #define FLOOR(x) (float)floor((double)(x))
  249. #define LOG(x) (float)log((double)(x))
  250. #define POW(x,y) (float)pow((double)(x),(double)(y))
  251. #ifdef TEST
  252. #define SQRT(x) (float)sqrtp((double)(x))
  253. #else
  254. #define SQRT(x) (float)sqrt((double)(x))
  255. #endif
  256. #else
  257. #define FLOAT double
  258. #define FABS(x) fabs(x)
  259. #define FLOOR(x) floor(x)
  260. #define LOG(x) log(x)
  261. #define POW(x,y) pow(x,y)
  262. #ifdef TEST
  263. #define SQRT(x) sqrtp(x)
  264. #else
  265. #define SQRT(x) sqrt(x)
  266. #endif
  267. #endif
  268. X
  269. jmp_buf ovfl_buf;
  270. typedef void (*Sig_type)();
  271. Sig_type sigsave;
  272. X
  273. #define KEYBOARD 0
  274. X
  275. FLOAT Radix, BInvrse, RadixD2, BMinusU2;
  276. FLOAT Sign(), Random();
  277. X
  278. /*Small floating point constants.*/
  279. FLOAT Zero = 0.0;
  280. FLOAT Half = 0.5;
  281. FLOAT One = 1.0;
  282. FLOAT Two = 2.0;
  283. FLOAT Three = 3.0;
  284. FLOAT Four = 4.0;
  285. FLOAT Five = 5.0;
  286. FLOAT Eight = 8.0;
  287. FLOAT Nine = 9.0;
  288. FLOAT TwentySeven = 27.0;
  289. FLOAT ThirtyTwo = 32.0;
  290. FLOAT TwoForty = 240.0;
  291. FLOAT MinusOne = -1.0;
  292. FLOAT OneAndHalf = 1.5;
  293. /*Integer constants*/
  294. int NoTrials = 20; /*Number of tests for commutativity. */
  295. #define False 0
  296. #define True 1
  297. X
  298. /* Definitions for declared types 
  299. X    Guard == (Yes, No);
  300. X    Rounding == (Chopped, Rounded, Other);
  301. X    Message == packed array [1..40] of char;
  302. X    Class == (Flaw, Defect, Serious, Failure);
  303. X      */
  304. #define Yes 1
  305. #define No  0
  306. #define Chopped 2
  307. #define Rounded 1
  308. #define Other   0
  309. #define Flaw    3
  310. #define Defect  2
  311. #define Serious 1
  312. #define Failure 0
  313. typedef int Guard, Rounding, Class;
  314. typedef char Message;
  315. X
  316. /* Declarations of Variables */
  317. int Indx;
  318. char ch[8];
  319. FLOAT AInvrse, A1;
  320. FLOAT C, CInvrse;
  321. FLOAT D, FourD;
  322. FLOAT E0, E1, Exp2, E3, MinSqEr;
  323. FLOAT SqEr, MaxSqEr, E9;
  324. FLOAT Third;
  325. FLOAT F6, F9;
  326. FLOAT H, HInvrse;
  327. int I;
  328. FLOAT StickyBit, J;
  329. FLOAT MyZero;
  330. FLOAT Precision;
  331. FLOAT Q, Q9;
  332. FLOAT R, Random9;
  333. FLOAT T, Underflow, S;
  334. FLOAT OneUlp, UfThold, U1, U2;
  335. FLOAT V, V0, V9;
  336. FLOAT W;
  337. FLOAT X, X1, X2, X8, Random1;
  338. FLOAT Y, Y1, Y2, Random2;
  339. FLOAT Z, PseudoZero, Z1, Z2, Z9;
  340. int ErrCnt[4];
  341. int fpecount;
  342. int Milestone;
  343. int PageNo;
  344. int M, N, N1;
  345. Guard GMult, GDiv, GAddSub;
  346. Rounding RMult, RDiv, RAddSub, RSqrt;
  347. int Break, Done, NotMonot, Monot, Anomaly, IEEE,
  348. X        SqRWrng, UfNGrad;
  349. /* Computed constants. */
  350. /*U1  gap below 1.0, i.e, 1.0-U1 is next number below 1.0 */
  351. /*U2  gap above 1.0, i.e, 1.0+U2 is next number above 1.0 */
  352. X
  353. /* floating point exception receiver */
  354. X void
  355. sigfpe()
  356. {
  357. X    fpecount++;
  358. X    printf("\n* * * FLOATING-POINT ERROR * * *\n");
  359. X    fflush(stdout);
  360. X    if (sigsave) {
  361. #ifndef NOSIGNAL
  362. X        signal(SIGFPE, sigsave);
  363. #endif
  364. X        sigsave = 0;
  365. X        longjmp(ovfl_buf, 1);
  366. X        }
  367. X    abort();
  368. }
  369. X
  370. main()
  371. {
  372. /*
  373. ** Modified by G. Geers - glenn@qed.physics.su.oz.au
  374. **
  375. ** Define TEST if you want to include my code
  376. **
  377. */
  378. X
  379. #ifdef TEST
  380. X    setcont(0x127f);
  381. #endif
  382. X
  383. X    /* First two assignments use integer right-hand sides. */
  384. X    Zero = 0;
  385. X    One = 1;
  386. X    Two = One + One;
  387. X    Three = Two + One;
  388. X    Four = Three + One;
  389. X    Five = Four + One;
  390. X    Eight = Four + Four;
  391. X    Nine = Three * Three;
  392. X    TwentySeven = Nine * Three;
  393. X    ThirtyTwo = Four * Eight;
  394. X    TwoForty = Four * Five * Three * Four;
  395. X    MinusOne = -One;
  396. X    Half = One / Two;
  397. X    OneAndHalf = One + Half;
  398. X    ErrCnt[Failure] = 0;
  399. X    ErrCnt[Serious] = 0;
  400. X    ErrCnt[Defect] = 0;
  401. X    ErrCnt[Flaw] = 0;
  402. X    PageNo = 1;
  403. X    /*=============================================*/
  404. X    Milestone = 0;
  405. X    /*=============================================*/
  406. #ifndef NOSIGNAL
  407. X    signal(SIGFPE, sigfpe);
  408. #endif
  409. X    Instructions();
  410. X    Pause();
  411. X    Heading();
  412. X    Pause();
  413. X    Characteristics();
  414. X    Pause();
  415. X    History();
  416. X    Pause();
  417. X    /*=============================================*/
  418. X    Milestone = 7;
  419. X    /*=============================================*/
  420. X    printf("Program is now RUNNING tests on small integers:\n");
  421. X    
  422. X    TstCond (Failure, (Zero + Zero == Zero) && (One - One == Zero)
  423. X           && (One > Zero) && (One + One == Two),
  424. X            "0+0 != 0, 1-1 != 0, 1 <= 0, or 1+1 != 2");
  425. X    Z = - Zero;
  426. X    if (Z != 0.0) {
  427. X        ErrCnt[Failure] = ErrCnt[Failure] + 1;
  428. X        printf("Comparison alleges that -0.0 is Non-zero!\n");
  429. X        U1 = 0.001;
  430. X        Radix = 1;
  431. X        TstPtUf();
  432. X        }
  433. X    TstCond (Failure, (Three == Two + One) && (Four == Three + One)
  434. X           && (Four + Two * (- Two) == Zero)
  435. X           && (Four - Three - One == Zero),
  436. X           "3 != 2+1, 4 != 3+1, 4+2*(-2) != 0, or 4-3-1 != 0");
  437. X    TstCond (Failure, (MinusOne == (0 - One))
  438. X           && (MinusOne + One == Zero ) && (One + MinusOne == Zero)
  439. X           && (MinusOne + FABS(One) == Zero)
  440. X           && (MinusOne + MinusOne * MinusOne == Zero),
  441. X           "-1+1 != 0, (-1)+abs(1) != 0, or -1+(-1)*(-1) != 0");
  442. X    TstCond (Failure, Half + MinusOne + Half == Zero,
  443. X          "1/2 + (-1) + 1/2 != 0");
  444. X    /*=============================================*/
  445. X    /*SPLIT
  446. X    part2();
  447. X    part3();
  448. X    part4();
  449. X    part5();
  450. X    part6();
  451. X    part7();
  452. X    part8();
  453. X
  454. X    }
  455. #include "paranoia.h"
  456. part2(){
  457. */
  458. X    Milestone = 10;
  459. X    /*=============================================*/
  460. X    TstCond (Failure, (Nine == Three * Three)
  461. X           && (TwentySeven == Nine * Three) && (Eight == Four + Four)
  462. X           && (ThirtyTwo == Eight * Four)
  463. X           && (ThirtyTwo - TwentySeven - Four - One == Zero),
  464. X           "9 != 3*3, 27 != 9*3, 32 != 8*4, or 32-27-4-1 != 0");
  465. X    TstCond (Failure, (Five == Four + One) &&
  466. X            (TwoForty == Four * Five * Three * Four)
  467. X           && (TwoForty / Three - Four * Four * Five == Zero)
  468. X           && ( TwoForty / Four - Five * Three * Four == Zero)
  469. X           && ( TwoForty / Five - Four * Three * Four == Zero),
  470. X          "5 != 4+1, 240/3 != 80, 240/4 != 60, or 240/5 != 48");
  471. X    if (ErrCnt[Failure] == 0) {
  472. X        printf("-1, 0, 1/2, 1, 2, 3, 4, 5, 9, 27, 32 & 240 are O.K.\n");
  473. X        printf("\n");
  474. X        }
  475. X    printf("Searching for Radix and Precision.\n");
  476. X    W = One;
  477. X    do  {
  478. X        W = W + W;
  479. X        Y = W + One;
  480. X        Z = Y - W;
  481. X        Y = Z - One;
  482. X        } while (MinusOne + FABS(Y) < Zero);
  483. X    /*.. now W is just big enough that |((W+1)-W)-1| >= 1 ...*/
  484. X    Precision = Zero;
  485. X    Y = One;
  486. X    do  {
  487. X        Radix = W + Y;
  488. X        Y = Y + Y;
  489. X        Radix = Radix - W;
  490. X        } while ( Radix == Zero);
  491. X    if (Radix < Two) Radix = One;
  492. X    printf("Radix = %f .\n", Radix);
  493. X    if (Radix != 1) {
  494. X        W = One;
  495. X        do  {
  496. X            Precision = Precision + One;
  497. X            W = W * Radix;
  498. X            Y = W + One;
  499. X            } while ((Y - W) == One);
  500. X        }
  501. X    /*... now W == Radix^Precision is barely too big to satisfy (W+1)-W == 1
  502. X                                          ...*/
  503. X    U1 = One / W;
  504. X    U2 = Radix * U1;
  505. X    printf("Closest relative separation found is U1 = %.7e .\n\n", U1);
  506. X    printf("Recalculating radix and precision\n ");
  507. X    
  508. X    /*save old values*/
  509. X    E0 = Radix;
  510. X    E1 = U1;
  511. X    E9 = U2;
  512. X    E3 = Precision;
  513. X    
  514. X    X = Four / Three;
  515. X    Third = X - One;
  516. X    F6 = Half - Third;
  517. X    X = F6 + F6;
  518. X    X = FABS(X - Third);
  519. X    if (X < U2) X = U2;
  520. X    
  521. X    /*... now X = (unknown no.) ulps of 1+...*/
  522. X    do  {
  523. X        U2 = X;
  524. X        Y = Half * U2 + ThirtyTwo * U2 * U2;
  525. X        Y = One + Y;
  526. X        X = Y - One;
  527. X        } while ( ! ((U2 <= X) || (X <= Zero)));
  528. X    
  529. X    /*... now U2 == 1 ulp of 1 + ... */
  530. X    X = Two / Three;
  531. X    F6 = X - Half;
  532. X    Third = F6 + F6;
  533. X    X = Third - Half;
  534. X    X = FABS(X + F6);
  535. X    if (X < U1) X = U1;
  536. X    
  537. X    /*... now  X == (unknown no.) ulps of 1 -... */
  538. X    do  {
  539. X        U1 = X;
  540. X        Y = Half * U1 + ThirtyTwo * U1 * U1;
  541. X        Y = Half - Y;
  542. X        X = Half + Y;
  543. X        Y = Half - X;
  544. X        X = Half + Y;
  545. X        } while ( ! ((U1 <= X) || (X <= Zero)));
  546. X    /*... now U1 == 1 ulp of 1 - ... */
  547. X    if (U1 == E1) printf("confirms closest relative separation U1 .\n");
  548. X    else printf("gets better closest relative separation U1 = %.7e .\n", U1);
  549. X    W = One / U1;
  550. X    F9 = (Half - U1) + Half;
  551. X    Radix = FLOOR(0.01 + U2 / U1);
  552. X    if (Radix == E0) printf("Radix confirmed.\n");
  553. X    else printf("MYSTERY: recalculated Radix = %.7e .\n", Radix);
  554. X    TstCond (Defect, Radix <= Eight + Eight,
  555. X           "Radix is too big: roundoff problems");
  556. X    TstCond (Flaw, (Radix == Two) || (Radix == 10)
  557. X           || (Radix == One), "Radix is not as good as 2 or 10");
  558. X    /*=============================================*/
  559. X    Milestone = 20;
  560. X    /*=============================================*/
  561. X    TstCond (Failure, F9 - Half < Half,
  562. X           "(1-U1)-1/2 < 1/2 is FALSE, prog. fails?");
  563. X    X = F9;
  564. X    I = 1;
  565. X    Y = X - Half;
  566. X    Z = Y - Half;
  567. X    TstCond (Failure, (X != One)
  568. X           || (Z == Zero), "Comparison is fuzzy,X=1 but X-1/2-1/2 != 0");
  569. X    X = One + U2;
  570. X    I = 0;
  571. X    /*=============================================*/
  572. X    Milestone = 25;
  573. X    /*=============================================*/
  574. X    /*... BMinusU2 = nextafter(Radix, 0) */
  575. X    BMinusU2 = Radix - One;
  576. X    BMinusU2 = (BMinusU2 - U2) + One;
  577. X    /* Purify Integers */
  578. X    if (Radix != One)  {
  579. X        X = - TwoForty * LOG(U1) / LOG(Radix);
  580. X        Y = FLOOR(Half + X);
  581. X        if (FABS(X - Y) * Four < One) X = Y;
  582. X        Precision = X / TwoForty;
  583. X        Y = FLOOR(Half + Precision);
  584. X        if (FABS(Precision - Y) * TwoForty < Half) Precision = Y;
  585. X        }
  586. X    if ((Precision != FLOOR(Precision)) || (Radix == One)) {
  587. X        printf("Precision cannot be characterized by an Integer number\n");
  588. X        printf("of significant digits but, by itself, this is a minor flaw.\n");
  589. X        }
  590. X    if (Radix == One) 
  591. X        printf("logarithmic encoding has precision characterized solely by U1.\n");
  592. X    else printf("The number of significant digits of the Radix is %f .\n",
  593. X            Precision);
  594. X    TstCond (Serious, U2 * Nine * Nine * TwoForty < One,
  595. X           "Precision worse than 5 decimal figures  ");
  596. X    /*=============================================*/
  597. X    Milestone = 30;
  598. X    /*=============================================*/
  599. X    /* Test for extra-precise subepressions */
  600. X    X = FABS(((Four / Three - One) - One / Four) * Three - One / Four);
  601. X    do  {
  602. X        Z2 = X;
  603. X        X = (One + (Half * Z2 + ThirtyTwo * Z2 * Z2)) - One;
  604. X        } while ( ! ((Z2 <= X) || (X <= Zero)));
  605. X    X = Y = Z = FABS((Three / Four - Two / Three) * Three - One / Four);
  606. X    do  {
  607. X        Z1 = Z;
  608. X        Z = (One / Two - ((One / Two - (Half * Z1 + ThirtyTwo * Z1 * Z1))
  609. X            + One / Two)) + One / Two;
  610. X        } while ( ! ((Z1 <= Z) || (Z <= Zero)));
  611. X    do  {
  612. X        do  {
  613. X            Y1 = Y;
  614. X            Y = (Half - ((Half - (Half * Y1 + ThirtyTwo * Y1 * Y1)) + Half
  615. X                )) + Half;
  616. X            } while ( ! ((Y1 <= Y) || (Y <= Zero)));
  617. X        X1 = X;
  618. X        X = ((Half * X1 + ThirtyTwo * X1 * X1) - F9) + F9;
  619. X        } while ( ! ((X1 <= X) || (X <= Zero)));
  620. X    if ((X1 != Y1) || (X1 != Z1)) {
  621. X        BadCond(Serious, "Disagreements among the values X1, Y1, Z1,\n");
  622. X        printf("respectively  %.7e,  %.7e,  %.7e,\n", X1, Y1, Z1);
  623. X        printf("are symptoms of inconsistencies introduced\n");
  624. X        printf("by extra-precise evaluation of arithmetic subexpressions.\n");
  625. X        notify("Possibly some part of this");
  626. X        if ((X1 == U1) || (Y1 == U1) || (Z1 == U1))  printf(
  627. X            "That feature is not tested further by this program.\n") ;
  628. X        }
  629. X    else  {
  630. X        if ((Z1 != U1) || (Z2 != U2)) {
  631. X            if ((Z1 >= U1) || (Z2 >= U2)) {
  632. X                BadCond(Failure, "");
  633. X                notify("Precision");
  634. X                printf("\tU1 = %.7e, Z1 - U1 = %.7e\n",U1,Z1-U1);
  635. X                printf("\tU2 = %.7e, Z2 - U2 = %.7e\n",U2,Z2-U2);
  636. X                }
  637. X            else {
  638. X                if ((Z1 <= Zero) || (Z2 <= Zero)) {
  639. X                    printf("Because of unusual Radix = %f", Radix);
  640. X                    printf(", or exact rational arithmetic a result\n");
  641. X                    printf("Z1 = %.7e, or Z2 = %.7e ", Z1, Z2);
  642. X                    notify("of an\nextra-precision");
  643. X                    }
  644. X                if (Z1 != Z2 || Z1 > Zero) {
  645. X                    X = Z1 / U1;
  646. X                    Y = Z2 / U2;
  647. X                    if (Y > X) X = Y;
  648. X                    Q = - LOG(X);
  649. X                    printf("Some subexpressions appear to be calculated extra\n");
  650. X                    printf("precisely with about %g extra B-digits, i.e.\n",
  651. X                        (Q / LOG(Radix)));
  652. X                    printf("roughly %g extra significant decimals.\n",
  653. X                        Q / LOG(10.));
  654. X                    }
  655. X                printf("That feature is not tested further by this program.\n");
  656. X                }
  657. X            }
  658. X        }
  659. X    Pause();
  660. X    /*=============================================*/
  661. X    /*SPLIT
  662. X    }
  663. #include "paranoia.h"
  664. part3(){
  665. */
  666. X    Milestone = 35;
  667. X    /*=============================================*/
  668. X    if (Radix >= Two) {
  669. X        X = W / (Radix * Radix);
  670. X        Y = X + One;
  671. X        Z = Y - X;
  672. X        T = Z + U2;
  673. X        X = T - Z;
  674. X        TstCond (Failure, X == U2,
  675. X            "Subtraction is not normalized X=Y,X+Z != Y+Z!");
  676. X        if (X == U2) printf(
  677. X            "Subtraction appears to be normalized, as it should be.");
  678. X        }
  679. X    printf("\nChecking for guard digit in *, /, and -.\n");
  680. X    Y = F9 * One;
  681. X    Z = One * F9;
  682. X    X = F9 - Half;
  683. X    Y = (Y - Half) - X;
  684. X    Z = (Z - Half) - X;
  685. X    X = One + U2;
  686. X    T = X * Radix;
  687. X    R = Radix * X;
  688. X    X = T - Radix;
  689. X    X = X - Radix * U2;
  690. X    T = R - Radix;
  691. X    T = T - Radix * U2;
  692. X    X = X * (Radix - One);
  693. X    T = T * (Radix - One);
  694. X    if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)) GMult = Yes;
  695. X    else {
  696. X        GMult = No;
  697. X        TstCond (Serious, False,
  698. X            "* lacks a Guard Digit, so 1*X != X");
  699. X        }
  700. X    Z = Radix * U2;
  701. X    X = One + Z;
  702. X    Y = FABS((X + Z) - X * X) - U2;
  703. X    X = One - U2;
  704. X    Z = FABS((X - U2) - X * X) - U1;
  705. X    TstCond (Failure, (Y <= Zero)
  706. X           && (Z <= Zero), "* gets too many final digits wrong.\n");
  707. X    Y = One - U2;
  708. X    X = One + U2;
  709. X    Z = One / Y;
  710. X    Y = Z - X;
  711. X    X = One / Three;
  712. X    Z = Three / Nine;
  713. X    X = X - Z;
  714. X    T = Nine / TwentySeven;
  715. X    Z = Z - T;
  716. X    TstCond(Defect, X == Zero && Y == Zero && Z == Zero,
  717. X        "Division lacks a Guard Digit, so error can exceed 1 ulp\n\
  718. or  1/3  and  3/9  and  9/27 may disagree");
  719. X    Y = F9 / One;
  720. X    X = F9 - Half;
  721. X    Y = (Y - Half) - X;
  722. X    X = One + U2;
  723. X    T = X / One;
  724. X    X = T - X;
  725. X    if ((X == Zero) && (Y == Zero) && (Z == Zero)) GDiv = Yes;
  726. X    else {
  727. X        GDiv = No;
  728. X        TstCond (Serious, False,
  729. X            "Division lacks a Guard Digit, so X/1 != X");
  730. X        }
  731. X    X = One / (One + U2);
  732. X    Y = X - Half - Half;
  733. X    TstCond (Serious, Y < Zero,
  734. X           "Computed value of 1/1.000..1 >= 1");
  735. X    X = One - U2;
  736. X    Y = One + Radix * U2;
  737. X    Z = X * Radix;
  738. X    T = Y * Radix;
  739. X    R = Z / Radix;
  740. X    StickyBit = T / Radix;
  741. X    X = R - X;
  742. X    Y = StickyBit - Y;
  743. X    TstCond (Failure, X == Zero && Y == Zero,
  744. X            "* and/or / gets too many last digits wrong");
  745. X    Y = One - U1;
  746. X    X = One - F9;
  747. X    Y = One - Y;
  748. X    T = Radix - U2;
  749. X    Z = Radix - BMinusU2;
  750. X    T = Radix - T;
  751. X    if ((X == U1) && (Y == U1) && (Z == U2) && (T == U2)) GAddSub = Yes;
  752. X    else {
  753. X        GAddSub = No;
  754. X        TstCond (Serious, False,
  755. X            "- lacks Guard Digit, so cancellation is obscured");
  756. X        }
  757. X    if (F9 != One && F9 - One >= Zero) {
  758. X        BadCond(Serious, "comparison alleges  (1-U1) < 1  although\n");
  759. X        printf("  subtraction yields  (1-U1) - 1 = 0 , thereby vitiating\n");
  760. X        printf("  such precautions against division by zero as\n");
  761. X        printf("  ...  if (X == 1.0) {.....} else {.../(X-1.0)...}\n");
  762. X        }
  763. X    if (GMult == Yes && GDiv == Yes && GAddSub == Yes) printf(
  764. X        "     *, /, and - appear to have guard digits, as they should.\n");
  765. X    /*=============================================*/
  766. X    Milestone = 40;
  767. X    /*=============================================*/
  768. X    Pause();
  769. X    printf("Checking rounding on multiply, divide and add/subtract.\n");
  770. X    RMult = Other;
  771. X    RDiv = Other;
  772. X    RAddSub = Other;
  773. X    RadixD2 = Radix / Two;
  774. X    A1 = Two;
  775. X    Done = False;
  776. X    do  {
  777. X        AInvrse = Radix;
  778. X        do  {
  779. X            X = AInvrse;
  780. X            AInvrse = AInvrse / A1;
  781. X            } while ( ! (FLOOR(AInvrse) != AInvrse));
  782. X        Done = (X == One) || (A1 > Three);
  783. X        if (! Done) A1 = Nine + One;
  784. X        } while ( ! (Done));
  785. X    if (X == One) A1 = Radix;
  786. X    AInvrse = One / A1;
  787. X    X = A1;
  788. X    Y = AInvrse;
  789. X    Done = False;
  790. X    do  {
  791. X        Z = X * Y - Half;
  792. X        TstCond (Failure, Z == Half,
  793. X            "X * (1/X) differs from 1");
  794. X        Done = X == Radix;
  795. X        X = Radix;
  796. X        Y = One / X;
  797. X        } while ( ! (Done));
  798. X    Y2 = One + U2;
  799. X    Y1 = One - U2;
  800. X    X = OneAndHalf - U2;
  801. X    Y = OneAndHalf + U2;
  802. X    Z = (X - U2) * Y2;
  803. X    T = Y * Y1;
  804. X    Z = Z - X;
  805. X    T = T - X;
  806. X    X = X * Y2;
  807. X    Y = (Y + U2) * Y1;
  808. X    X = X - OneAndHalf;
  809. X    Y = Y - OneAndHalf;
  810. X    if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T <= Zero)) {
  811. X        X = (OneAndHalf + U2) * Y2;
  812. X        Y = OneAndHalf - U2 - U2;
  813. X        Z = OneAndHalf + U2 + U2;
  814. X        T = (OneAndHalf - U2) * Y1;
  815. X        X = X - (Z + U2);
  816. X        StickyBit = Y * Y1;
  817. X        S = Z * Y2;
  818. X        T = T - Y;
  819. X        Y = (U2 - Y) + StickyBit;
  820. X        Z = S - (Z + U2 + U2);
  821. X        StickyBit = (Y2 + U2) * Y1;
  822. X        Y1 = Y2 * Y1;
  823. X        StickyBit = StickyBit - Y2;
  824. X        Y1 = Y1 - Half;
  825. X        if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)
  826. X            && ( StickyBit == Zero) && (Y1 == Half)) {
  827. X            RMult = Rounded;
  828. X            printf("Multiplication appears to round correctly.\n");
  829. X            }
  830. X        else    if ((X + U2 == Zero) && (Y < Zero) && (Z + U2 == Zero)
  831. X                && (T < Zero) && (StickyBit + U2 == Zero)
  832. X                && (Y1 < Half)) {
  833. X                RMult = Chopped;
  834. X                printf("Multiplication appears to chop.\n");
  835. X                }
  836. X            else printf("* is neither chopped nor correctly rounded.\n");
  837. X        if ((RMult == Rounded) && (GMult == No)) notify("Multiplication");
  838. X        }
  839. X    else printf("* is neither chopped nor correctly rounded.\n");
  840. X    /*=============================================*/
  841. X    Milestone = 45;
  842. X    /*=============================================*/
  843. X    Y2 = One + U2;
  844. X    Y1 = One - U2;
  845. X    Z = OneAndHalf + U2 + U2;
  846. X    X = Z / Y2;
  847. X    T = OneAndHalf - U2 - U2;
  848. X    Y = (T - U2) / Y1;
  849. X    Z = (Z + U2) / Y2;
  850. X    X = X - OneAndHalf;
  851. X    Y = Y - T;
  852. X    T = T / Y1;
  853. X    Z = Z - (OneAndHalf + U2);
  854. X    T = (U2 - OneAndHalf) + T;
  855. X    if (! ((X > Zero) || (Y > Zero) || (Z > Zero) || (T > Zero))) {
  856. X        X = OneAndHalf / Y2;
  857. X        Y = OneAndHalf - U2;
  858. X        Z = OneAndHalf + U2;
  859. X        X = X - Y;
  860. X        T = OneAndHalf / Y1;
  861. X        Y = Y / Y1;
  862. X        T = T - (Z + U2);
  863. X        Y = Y - Z;
  864. X        Z = Z / Y2;
  865. X        Y1 = (Y2 + U2) / Y2;
  866. X        Z = Z - OneAndHalf;
  867. X        Y2 = Y1 - Y2;
  868. X        Y1 = (F9 - U1) / F9;
  869. X        if ((X == Zero) && (Y == Zero) && (Z == Zero) && (T == Zero)
  870. X            && (Y2 == Zero) && (Y2 == Zero)
  871. X            && (Y1 - Half == F9 - Half )) {
  872. X            RDiv = Rounded;
  873. X            printf("Division appears to round correctly.\n");
  874. X            if (GDiv == No) notify("Division");
  875. X            }
  876. X        else if ((X < Zero) && (Y < Zero) && (Z < Zero) && (T < Zero)
  877. X            && (Y2 < Zero) && (Y1 - Half < F9 - Half)) {
  878. X            RDiv = Chopped;
  879. X            printf("Division appears to chop.\n");
  880. X            }
  881. X        }
  882. X    if (RDiv == Other) printf("/ is neither chopped nor correctly rounded.\n");
  883. X    BInvrse = One / Radix;
  884. X    TstCond (Failure, (BInvrse * Radix - Half == Half),
  885. X           "Radix * ( 1 / Radix ) differs from 1");
  886. X    /*=============================================*/
  887. X    /*SPLIT
  888. X    }
  889. #include "paranoia.h"
  890. part4(){
  891. */
  892. X    Milestone = 50;
  893. X    /*=============================================*/
  894. X    TstCond (Failure, ((F9 + U1) - Half == Half)
  895. X           && ((BMinusU2 + U2 ) - One == Radix - One),
  896. X           "Incomplete carry-propagation in Addition");
  897. X    X = One - U1 * U1;
  898. X    Y = One + U2 * (One - U2);
  899. X    Z = F9 - Half;
  900. X    X = (X - Half) - Z;
  901. X    Y = Y - One;
  902. X    if ((X == Zero) && (Y == Zero)) {
  903. X        RAddSub = Chopped;
  904. X        printf("Add/Subtract appears to be chopped.\n");
  905. X        }
  906. X    if (GAddSub == Yes) {
  907. X        X = (Half + U2) * U2;
  908. X        Y = (Half - U2) * U2;
  909. X        X = One + X;
  910. X        Y = One + Y;
  911. X        X = (One + U2) - X;
  912. X        Y = One - Y;
  913. X        if ((X == Zero) && (Y == Zero)) {
  914. X            X = (Half + U2) * U1;
  915. X            Y = (Half - U2) * U1;
  916. X            X = One - X;
  917. X            Y = One - Y;
  918. X            X = F9 - X;
  919. X            Y = One - Y;
  920. X            if ((X == Zero) && (Y == Zero)) {
  921. X                RAddSub = Rounded;
  922. X                printf("Addition/Subtraction appears to round correctly.\n");
  923. X                if (GAddSub == No) notify("Add/Subtract");
  924. X                }
  925. X            else printf("Addition/Subtraction neither rounds nor chops.\n");
  926. X            }
  927. X        else printf("Addition/Subtraction neither rounds nor chops.\n");
  928. X        }
  929. X    else printf("Addition/Subtraction neither rounds nor chops.\n");
  930. X    S = One;
  931. X    X = One + Half * (One + Half);
  932. X    Y = (One + U2) * Half;
  933. X    Z = X - Y;
  934. X    T = Y - X;
  935. X    StickyBit = Z + T;
  936. X    if (StickyBit != Zero) {
  937. X        S = Zero;
  938. X        BadCond(Flaw, "(X - Y) + (Y - X) is non zero!\n");
  939. X        }
  940. X    StickyBit = Zero;
  941. X    if ((GMult == Yes) && (GDiv == Yes) && (GAddSub == Yes)
  942. X        && (RMult == Rounded) && (RDiv == Rounded)
  943. X        && (RAddSub == Rounded) && (FLOOR(RadixD2) == RadixD2)) {
  944. X        printf("Checking for sticky bit.\n");
  945. X        X = (Half + U1) * U2;
  946. X        Y = Half * U2;
  947. X        Z = One + Y;
  948. X        T = One + X;
  949. X        if ((Z - One <= Zero) && (T - One >= U2)) {
  950. X            Z = T + Y;
  951. X            Y = Z - X;
  952. X            if ((Z - T >= U2) && (Y - T == Zero)) {
  953. X                X = (Half + U1) * U1;
  954. X                Y = Half * U1;
  955. X                Z = One - Y;
  956. X                T = One - X;
  957. X                if ((Z - One == Zero) && (T - F9 == Zero)) {
  958. X                    Z = (Half - U1) * U1;
  959. X                    T = F9 - Z;
  960. X                    Q = F9 - Y;
  961. X                    if ((T - F9 == Zero) && (F9 - U1 - Q == Zero)) {
  962. X                        Z = (One + U2) * OneAndHalf;
  963. X                        T = (OneAndHalf + U2) - Z + U2;
  964. X                        X = One + Half / Radix;
  965. X                        Y = One + Radix * U2;
  966. X                        Z = X * Y;
  967. X                        if (T == Zero && X + Radix * U2 - Z == Zero) {
  968. X                            if (Radix != Two) {
  969. X                                X = Two + U2;
  970. X                                Y = X / Two;
  971. X                                if ((Y - One == Zero)) StickyBit = S;
  972. X                                }
  973. X                            else StickyBit = S;
  974. X                            }
  975. X                        }
  976. X                    }
  977. X                }
  978. X            }
  979. X        }
  980. X    if (StickyBit == One) printf("Sticky bit apparently used correctly.\n");
  981. X    else printf("Sticky bit used incorrectly or not at all.\n");
  982. X    TstCond (Flaw, !(GMult == No || GDiv == No || GAddSub == No ||
  983. X            RMult == Other || RDiv == Other || RAddSub == Other),
  984. X        "lack(s) of guard digits or failure(s) to correctly round or chop\n\
  985. (noted above) count as one flaw in the final tally below");
  986. X    /*=============================================*/
  987. X    Milestone = 60;
  988. X    /*=============================================*/
  989. X    printf("\n");
  990. X    printf("Does Multiplication commute?  ");
  991. X    printf("Testing on %d random pairs.\n", NoTrials);
  992. X    Random9 = SQRT(3.0);
  993. X    Random1 = Third;
  994. X    I = 1;
  995. X    do  {
  996. X        X = Random();
  997. X        Y = Random();
  998. X        Z9 = Y * X;
  999. X        Z = X * Y;
  1000. X        Z9 = Z - Z9;
  1001. X        I = I + 1;
  1002. X        } while ( ! ((I > NoTrials) || (Z9 != Zero)));
  1003. X    if (I == NoTrials) {
  1004. X        Random1 = One + Half / Three;
  1005. X        Random2 = (U2 + U1) + One;
  1006. X        Z = Random1 * Random2;
  1007. X        Y = Random2 * Random1;
  1008. X        Z9 = (One + Half / Three) * ((U2 + U1) + One) - (One + Half /
  1009. X            Three) * ((U2 + U1) + One);
  1010. X        }
  1011. X    if (! ((I == NoTrials) || (Z9 == Zero)))
  1012. X        BadCond(Defect, "X * Y == Y * X trial fails.\n");
  1013. X    else printf("     No failures found in %d integer pairs.\n", NoTrials);
  1014. X    /*=============================================*/
  1015. X    Milestone = 70;
  1016. X    /*=============================================*/
  1017. X    printf("\nRunning test of square root(x).\n");
  1018. X    TstCond (Failure, (Zero == SQRT(Zero))
  1019. X           && (- Zero == SQRT(- Zero))
  1020. X           && (One == SQRT(One)), "Square root of 0.0, -0.0 or 1.0 wrong");
  1021. X    MinSqEr = Zero;
  1022. X    MaxSqEr = Zero;
  1023. X    J = Zero;
  1024. X    X = Radix;
  1025. X    OneUlp = U2;
  1026. X    SqXMinX (Serious);
  1027. X    X = BInvrse;
  1028. X    OneUlp = BInvrse * U1;
  1029. X    SqXMinX (Serious);
  1030. X    X = U1;
  1031. X    OneUlp = U1 * U1;
  1032. X    SqXMinX (Serious);
  1033. X    if (J != Zero) Pause();
  1034. X    printf("Testing if sqrt(X * X) == X for %d Integers X.\n", NoTrials);
  1035. X    J = Zero;
  1036. X    X = Two;
  1037. X    Y = Radix;
  1038. X    if ((Radix != One)) do  {
  1039. X        X = Y;
  1040. X        Y = Radix * Y;
  1041. X        } while ( ! ((Y - X >= NoTrials)));
  1042. X    OneUlp = X * U2;
  1043. X    I = 1;
  1044. X    while (I <= NoTrials) {
  1045. X        X = X + One;
  1046. X        SqXMinX (Defect);
  1047. X        if (J > Zero) break;
  1048. X        I = I + 1;
  1049. X        }
  1050. X    printf("Test for sqrt monotonicity.\n");
  1051. X    I = - 1;
  1052. X    X = BMinusU2;
  1053. X    Y = Radix;
  1054. X    Z = Radix + Radix * U2;
  1055. X    NotMonot = False;
  1056. X    Monot = False;
  1057. X    while ( ! (NotMonot || Monot)) {
  1058. X        I = I + 1;
  1059. X        X = SQRT(X);
  1060. X        Q = SQRT(Y);
  1061. X        Z = SQRT(Z);
  1062. X        if ((X > Q) || (Q > Z)) NotMonot = True;
  1063. X        else {
  1064. X            Q = FLOOR(Q + Half);
  1065. X            if ((I > 0) || (Radix == Q * Q)) Monot = True;
  1066. X            else if (I > 0) {
  1067. X            if (I > 1) Monot = True;
  1068. X            else {
  1069. X                Y = Y * BInvrse;
  1070. X                X = Y - U1;
  1071. X                Z = Y + U1;
  1072. X                }
  1073. X            }
  1074. X            else {
  1075. X                Y = Q;
  1076. X                X = Y - U2;
  1077. X                Z = Y + U2;
  1078. X                }
  1079. X            }
  1080. X        }
  1081. X    if (Monot) printf("sqrt has passed a test for Monotonicity.\n");
  1082. X    else {
  1083. X        BadCond(Defect, "");
  1084. X        printf("sqrt(X) is non-monotonic for X near %.7e .\n", Y);
  1085. X        }
  1086. X    /*=============================================*/
  1087. X    /*SPLIT
  1088. X    }
  1089. #include "paranoia.h"
  1090. part5(){
  1091. */
  1092. X    Milestone = 80;
  1093. X    /*=============================================*/
  1094. X    MinSqEr = MinSqEr + Half;
  1095. X    MaxSqEr = MaxSqEr - Half;
  1096. X    Y = (SQRT(One + U2) - One) / U2;
  1097. X    SqEr = (Y - One) + U2 / Eight;
  1098. X    if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1099. X    SqEr = Y + U2 / Eight;
  1100. X    if (SqEr < MinSqEr) MinSqEr = SqEr;
  1101. X    Y = ((SQRT(F9) - U2) - (One - U2)) / U1;
  1102. X    SqEr = Y + U1 / Eight;
  1103. X    if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1104. X    SqEr = (Y + One) + U1 / Eight;
  1105. X    if (SqEr < MinSqEr) MinSqEr = SqEr;
  1106. X    OneUlp = U2;
  1107. X    X = OneUlp;
  1108. X    for( Indx = 1; Indx <= 3; ++Indx) {
  1109. X        Y = SQRT((X + U1 + X) + F9);
  1110. X        Y = ((Y - U2) - ((One - U2) + X)) / OneUlp;
  1111. X        Z = ((U1 - X) + F9) * Half * X * X / OneUlp;
  1112. X        SqEr = (Y + Half) + Z;
  1113. X        if (SqEr < MinSqEr) MinSqEr = SqEr;
  1114. X        SqEr = (Y - Half) + Z;
  1115. X        if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1116. X        if (((Indx == 1) || (Indx == 3))) 
  1117. X            X = OneUlp * Sign (X) * FLOOR(Eight / (Nine * SQRT(OneUlp)));
  1118. X        else {
  1119. X            OneUlp = U1;
  1120. X            X = - OneUlp;
  1121. X            }
  1122. X        }
  1123. X    /*=============================================*/
  1124. X    Milestone = 85;
  1125. X    /*=============================================*/
  1126. X    SqRWrng = False;
  1127. X    Anomaly = False;
  1128. X    RSqrt = Other; /* ~dgh */
  1129. X    if (Radix != One) {
  1130. X        printf("Testing whether sqrt is rounded or chopped.\n");
  1131. X        D = FLOOR(Half + POW(Radix, One + Precision - FLOOR(Precision)));
  1132. X    /* ... == Radix^(1 + fract) if (Precision == Integer + fract. */
  1133. X        X = D / Radix;
  1134. X        Y = D / A1;
  1135. X        if ((X != FLOOR(X)) || (Y != FLOOR(Y))) {
  1136. X            Anomaly = True;
  1137. X            }
  1138. X        else {
  1139. X            X = Zero;
  1140. X            Z2 = X;
  1141. X            Y = One;
  1142. X            Y2 = Y;
  1143. X            Z1 = Radix - One;
  1144. X            FourD = Four * D;
  1145. X            do  {
  1146. X                if (Y2 > Z2) {
  1147. X                    Q = Radix;
  1148. X                    Y1 = Y;
  1149. X                    do  {
  1150. X                        X1 = FABS(Q + FLOOR(Half - Q / Y1) * Y1);
  1151. X                        Q = Y1;
  1152. X                        Y1 = X1;
  1153. X                        } while ( ! (X1 <= Zero));
  1154. X                    if (Q <= One) {
  1155. X                        Z2 = Y2;
  1156. X                        Z = Y;
  1157. X                        }
  1158. X                    }
  1159. X                Y = Y + Two;
  1160. X                X = X + Eight;
  1161. X                Y2 = Y2 + X;
  1162. X                if (Y2 >= FourD) Y2 = Y2 - FourD;
  1163. X                } while ( ! (Y >= D));
  1164. X            X8 = FourD - Z2;
  1165. X            Q = (X8 + Z * Z) / FourD;
  1166. X            X8 = X8 / Eight;
  1167. X            if (Q != FLOOR(Q)) Anomaly = True;
  1168. X            else {
  1169. X                Break = False;
  1170. X                do  {
  1171. X                    X = Z1 * Z;
  1172. X                    X = X - FLOOR(X / Radix) * Radix;
  1173. X                    if (X == One) 
  1174. X                        Break = True;
  1175. X                    else
  1176. X                        Z1 = Z1 - One;
  1177. X                    } while ( ! (Break || (Z1 <= Zero)));
  1178. X                if ((Z1 <= Zero) && (! Break)) Anomaly = True;
  1179. X                else {
  1180. X                    if (Z1 > RadixD2) Z1 = Z1 - Radix;
  1181. X                    do  {
  1182. X                        NewD();
  1183. X                        } while ( ! (U2 * D >= F9));
  1184. X                    if (D * Radix - D != W - D) Anomaly = True;
  1185. X                    else {
  1186. X                        Z2 = D;
  1187. X                        I = 0;
  1188. X                        Y = D + (One + Z) * Half;
  1189. X                        X = D + Z + Q;
  1190. X                        SR3750();
  1191. X                        Y = D + (One - Z) * Half + D;
  1192. X                        X = D - Z + D;
  1193. X                        X = X + Q + X;
  1194. X                        SR3750();
  1195. X                        NewD();
  1196. X                        if (D - Z2 != W - Z2) Anomaly = True;
  1197. X                        else {
  1198. X                            Y = (D - Z2) + (Z2 + (One - Z) * Half);
  1199. X                            X = (D - Z2) + (Z2 - Z + Q);
  1200. X                            SR3750();
  1201. X                            Y = (One + Z) * Half;
  1202. X                            X = Q;
  1203. X                            SR3750();
  1204. X                            if (I == 0) Anomaly = True;
  1205. X                            }
  1206. X                        }
  1207. X                    }
  1208. X                }
  1209. X            }
  1210. X        if ((I == 0) || Anomaly) {
  1211. X            BadCond(Failure, "Anomalous arithmetic with Integer < ");
  1212. X            printf("Radix^Precision = %.7e\n", W);
  1213. X            printf(" fails test whether sqrt rounds or chops.\n");
  1214. X            SqRWrng = True;
  1215. X            }
  1216. X        }
  1217. X    if (! Anomaly) {
  1218. X        if (! ((MinSqEr < Zero) || (MaxSqEr > Zero))) {
  1219. X            RSqrt = Rounded;
  1220. X            printf("Square root appears to be correctly rounded.\n");
  1221. X            }
  1222. X        else  {
  1223. X            if ((MaxSqEr + U2 > U2 - Half) || (MinSqEr > Half)
  1224. X                || (MinSqEr + Radix < Half)) SqRWrng = True;
  1225. X            else {
  1226. X                RSqrt = Chopped;
  1227. X                printf("Square root appears to be chopped.\n");
  1228. X                }
  1229. X            }
  1230. X        }
  1231. X    if (SqRWrng) {
  1232. X        printf("Square root is neither chopped nor correctly rounded.\n");
  1233. X        printf("Observed errors run from %.7e ", MinSqEr - Half);
  1234. X        printf("to %.7e ulps.\n", Half + MaxSqEr);
  1235. X        TstCond (Serious, MaxSqEr - MinSqEr < Radix * Radix,
  1236. X            "sqrt gets too many last digits wrong");
  1237. X        }
  1238. X    /*=============================================*/
  1239. X    Milestone = 90;
  1240. X    /*=============================================*/
  1241. X    Pause();
  1242. X    printf("Testing powers Z^i for small Integers Z and i.\n");
  1243. X    N = 0;
  1244. X    /* ... test powers of zero. */
  1245. X    I = 0;
  1246. X    Z = -Zero;
  1247. X    M = 3.0;
  1248. X    Break = False;
  1249. X    do  {
  1250. X        X = One;
  1251. X        SR3980();
  1252. X        if (I <= 10) {
  1253. X            I = 1023;
  1254. X            SR3980();
  1255. X            }
  1256. X        if (Z == MinusOne) Break = True;
  1257. X        else {
  1258. X            Z = MinusOne;
  1259. X            PrintIfNPositive();
  1260. X            N = 0;
  1261. X            /* .. if(-1)^N is invalid, replace MinusOne by One. */
  1262. X            I = - 4;
  1263. X            }
  1264. X        } while ( ! Break);
  1265. X    PrintIfNPositive();
  1266. X    N1 = N;
  1267. X    N = 0;
  1268. X    Z = A1;
  1269. X    M = FLOOR(Two * LOG(W) / LOG(A1));
  1270. X    Break = False;
  1271. X    do  {
  1272. X        X = Z;
  1273. X        I = 1;
  1274. X        SR3980();
  1275. X        if (Z == AInvrse) Break = True;
  1276. X        else Z = AInvrse;
  1277. X        } while ( ! (Break));
  1278. X    /*=============================================*/
  1279. X        Milestone = 100;
  1280. X    /*=============================================*/
  1281. X    /*  Powers of Radix have been tested, */
  1282. X    /*         next try a few primes     */
  1283. X    M = NoTrials;
  1284. X    Z = Three;
  1285. X    do  {
  1286. X        X = Z;
  1287. X        I = 1;
  1288. X        SR3980();
  1289. X        do  {
  1290. X            Z = Z + Two;
  1291. X            } while ( Three * FLOOR(Z / Three) == Z );
  1292. X        } while ( Z < Eight * Three );
  1293. X    if (N > 0) {
  1294. X        printf("Errors like this may invalidate financial calculations\n");
  1295. X        printf("\tinvolving interest rates.\n");
  1296. X        }
  1297. X    PrintIfNPositive();
  1298. X    N += N1;
  1299. X    if (N == 0) printf("... no discrepancis found.\n");
  1300. X    if (N > 0) Pause();
  1301. X    else printf("\n");
  1302. X    /*=============================================*/
  1303. X    /*SPLIT
  1304. X    }
  1305. #include "paranoia.h"
  1306. part6(){
  1307. */
  1308. X    Milestone = 110;
  1309. X    /*=============================================*/
  1310. X    printf("Seeking Underflow thresholds UfThold and E0.\n");
  1311. X    D = U1;
  1312. X    if (Precision != FLOOR(Precision)) {
  1313. X        D = BInvrse;
  1314. X        X = Precision;
  1315. X        do  {
  1316. X            D = D * BInvrse;
  1317. X            X = X - One;
  1318. X            } while ( X > Zero);
  1319. X        }
  1320. X    Y = One;
  1321. X    Z = D;
  1322. X    /* ... D is power of 1/Radix < 1. */
  1323. X    do  {
  1324. X        C = Y;
  1325. X        Y = Z;
  1326. X        Z = Y * Y;
  1327. X        } while ((Y > Z) && (Z + Z > Z));
  1328. X    Y = C;
  1329. X    Z = Y * D;
  1330. X    do  {
  1331. X        C = Y;
  1332. X        Y = Z;
  1333. X        Z = Y * D;
  1334. X        } while ((Y > Z) && (Z + Z > Z));
  1335. X    if (Radix < Two) HInvrse = Two;
  1336. X    else HInvrse = Radix;
  1337. X    H = One / HInvrse;
  1338. X    /* ... 1/HInvrse == H == Min(1/Radix, 1/2) */
  1339. X    CInvrse = One / C;
  1340. X    E0 = C;
  1341. X    Z = E0 * H;
  1342. X    /* ...1/Radix^(BIG Integer) << 1 << CInvrse == 1/C */
  1343. X    do  {
  1344. X        Y = E0;
  1345. X        E0 = Z;
  1346. X        Z = E0 * H;
  1347. X        } while ((E0 > Z) && (Z + Z > Z));
  1348. X    UfThold = E0;
  1349. X    E1 = Zero;
  1350. X    Q = Zero;
  1351. X    E9 = U2;
  1352. X    S = One + E9;
  1353. X    D = C * S;
  1354. X    if (D <= C) {
  1355. X        E9 = Radix * U2;
  1356. X        S = One + E9;
  1357. X        D = C * S;
  1358. X        if (D <= C) {
  1359. X            BadCond(Failure, "multiplication gets too many last digits wrong.\n");
  1360. X            Underflow = E0;
  1361. X            Y1 = Zero;
  1362. X            PseudoZero = Z;
  1363. X            Pause();
  1364. X            }
  1365. X        }
  1366. X    else {
  1367. X        Underflow = D;
  1368. X        PseudoZero = Underflow * H;
  1369. X        UfThold = Zero;
  1370. X        do  {
  1371. X            Y1 = Underflow;
  1372. X            Underflow = PseudoZero;
  1373. X            if (E1 + E1 <= E1) {
  1374. X                Y2 = Underflow * HInvrse;
  1375. X                E1 = FABS(Y1 - Y2);
  1376. X                Q = Y1;
  1377. X                if ((UfThold == Zero) && (Y1 != Y2)) UfThold = Y1;
  1378. X                }
  1379. X            PseudoZero = PseudoZero * H;
  1380. X            } while ((Underflow > PseudoZero)
  1381. X                && (PseudoZero + PseudoZero > PseudoZero));
  1382. X        }
  1383. X    /* Comment line 4530 .. 4560 */
  1384. X    if (PseudoZero != Zero) {
  1385. X        printf("\n");
  1386. X        Z = PseudoZero;
  1387. X    /* ... Test PseudoZero for "phoney- zero" violates */
  1388. X    /* ... PseudoZero < Underflow or PseudoZero < PseudoZero + PseudoZero
  1389. X           ... */
  1390. X        if (PseudoZero <= Zero) {
  1391. X            BadCond(Failure, "Positive expressions can underflow to an\n");
  1392. X            printf("allegedly negative value\n");
  1393. X            printf("PseudoZero that prints out as: %g .\n", PseudoZero);
  1394. X            X = - PseudoZero;
  1395. X            if (X <= Zero) {
  1396. X                printf("But -PseudoZero, which should be\n");
  1397. X                printf("positive, isn't; it prints out as  %g .\n", X);
  1398. X                }
  1399. X            }
  1400. X        else {
  1401. X            BadCond(Flaw, "Underflow can stick at an allegedly positive\n");
  1402. X            printf("value PseudoZero that prints out as %g .\n", PseudoZero);
  1403. X            }
  1404. X        TstPtUf();
  1405. X        }
  1406. X    /*=============================================*/
  1407. X    Milestone = 120;
  1408. X    /*=============================================*/
  1409. X    if (CInvrse * Y > CInvrse * Y1) {
  1410. X        S = H * S;
  1411. X        E0 = Underflow;
  1412. X        }
  1413. X    if (! ((E1 == Zero) || (E1 == E0))) {
  1414. X        BadCond(Defect, "");
  1415. X        if (E1 < E0) {
  1416. X            printf("Products underflow at a higher");
  1417. X            printf(" threshold than differences.\n");
  1418. X            if (PseudoZero == Zero) 
  1419. X            E0 = E1;
  1420. X            }
  1421. X        else {
  1422. X            printf("Difference underflows at a higher");
  1423. X            printf(" threshold than products.\n");
  1424. X            }
  1425. X        }
  1426. X    printf("Smallest strictly positive number found is E0 = %g .\n", E0);
  1427. X    Z = E0;
  1428. X    TstPtUf();
  1429. X    Underflow = E0;
  1430. X    if (N == 1) Underflow = Y;
  1431. X    I = 4;
  1432. X    if (E1 == Zero) I = 3;
  1433. X    if (UfThold == Zero) I = I - 2;
  1434. X    UfNGrad = True;
  1435. X    switch (I)  {
  1436. X        case    1:
  1437. X        UfThold = Underflow;
  1438. X        if ((CInvrse * Q) != ((CInvrse * Y) * S)) {
  1439. X            UfThold = Y;
  1440. X            BadCond(Failure, "Either accuracy deteriorates as numbers\n");
  1441. X            printf("approach a threshold = %.17e\n", UfThold);;
  1442. X            printf(" coming down from %.17e\n", C);
  1443. X            printf(" or else multiplication gets too many last digits wrong.\n");
  1444. X            }
  1445. X        Pause();
  1446. X        break;
  1447. X    
  1448. X        case    2:
  1449. X        BadCond(Failure, "Underflow confuses Comparison, which alleges that\n");
  1450. X        printf("Q == Y while denying that |Q - Y| == 0; these values\n");
  1451. X        printf("print out as Q = %.17e, Y = %.17e .\n", Q, Y2);
  1452. X        printf ("|Q - Y| = %.17e .\n" , FABS(Q - Y2));
  1453. X        UfThold = Q;
  1454. X        break;
  1455. X    
  1456. X        case    3:
  1457. X        X = X;
  1458. X        break;
  1459. X    
  1460. X        case    4:
  1461. X        if ((Q == UfThold) && (E1 == E0)
  1462. X            && (FABS( UfThold - E1 / E9) <= E1)) {
  1463. X            UfNGrad = False;
  1464. X            printf("Underflow is gradual; it incurs Absolute Error =\n");
  1465. X            printf("(roundoff in UfThold) < E0.\n");
  1466. X            Y = E0 * CInvrse;
  1467. X            Y = Y * (OneAndHalf + U2);
  1468. X            X = CInvrse * (One + U2);
  1469. X            Y = Y / X;
  1470. X            IEEE = (Y == E0);
  1471. X            }
  1472. X        }
  1473. X    if (UfNGrad) {
  1474. X        printf("\n");
  1475. X        sigsave = sigfpe;
  1476. X        if (setjmp(ovfl_buf)) {
  1477. X            printf("Underflow / UfThold failed!\n");
  1478. X            R = H + H;
  1479. X            }
  1480. X        else R = SQRT(Underflow / UfThold);
  1481. X        sigsave = 0;
  1482. X        if (R <= H) {
  1483. X            Z = R * UfThold;
  1484. X            X = Z * (One + R * H * (One + H));
  1485. X            }
  1486. X        else {
  1487. X            Z = UfThold;
  1488. X            X = Z * (One + H * H * (One + H));
  1489. X            }
  1490. X        if (! ((X == Z) || (X - Z != Zero))) {
  1491. X            BadCond(Flaw, "");
  1492. X            printf("X = %.17e\n\tis not equal to Z = %.17e .\n", X, Z);
  1493. X            Z9 = X - Z;
  1494. X            printf("yet X - Z yields %.17e .\n", Z9);
  1495. X            printf("    Should this NOT signal Underflow, ");
  1496. X            printf("this is a SERIOUS DEFECT\nthat causes ");
  1497. X            printf("confusion when innocent statements like\n");;
  1498. X            printf("    if (X == Z)  ...  else");
  1499. X            printf("  ... (f(X) - f(Z)) / (X - Z) ...\n");
  1500. X            printf("encounter Division by Zero although actually\n");
  1501. X            sigsave = sigfpe;
  1502. X            if (setjmp(ovfl_buf)) printf("X / Z fails!\n");
  1503. X            else printf("X / Z = 1 + %g .\n", (X / Z - Half) - Half);
  1504. X            sigsave = 0;
  1505. X            }
  1506. X        }
  1507. X    printf("The Underflow threshold is %.17e, %s\n", UfThold,
  1508. X           " below which");
  1509. X    printf("calculation may suffer larger Relative error than ");
  1510. X    printf("merely roundoff.\n");
  1511. X    Y2 = U1 * U1;
  1512. X    Y = Y2 * Y2;
  1513. X    Y2 = Y * U1;
  1514. X    if (Y2 <= UfThold) {
  1515. X        if (Y > E0) {
  1516. X            BadCond(Defect, "");
  1517. X            I = 5;
  1518. X            }
  1519. X        else {
  1520. X            BadCond(Serious, "");
  1521. X            I = 4;
  1522. X            }
  1523. X        printf("Range is too narrow; U1^%d Underflows.\n", I);
  1524. X        }
  1525. X    /*=============================================*/
  1526. X    /*SPLIT
  1527. X    }
  1528. #include "paranoia.h"
  1529. part7(){
  1530. */
  1531. X    Milestone = 130;
  1532. X    /*=============================================*/
  1533. X    Y = - FLOOR(Half - TwoForty * LOG(UfThold) / LOG(HInvrse)) / TwoForty;
  1534. X    Y2 = Y + Y;
  1535. X    printf("Since underflow occurs below the threshold\n");
  1536. X    printf("UfThold = (%.17e) ^ (%.17e)\nonly underflow ", HInvrse, Y);
  1537. X    printf("should afflict the expression\n\t(%.17e) ^ (%.17e);\n", HInvrse, Y);
  1538. X    V9 = POW(HInvrse, Y2);
  1539. X    printf("actually calculating yields: %.17e .\n", V9);
  1540. X    if (! ((V9 >= Zero) && (V9 <= (Radix + Radix + E9) * UfThold))) {
  1541. X        BadCond(Serious, "this is not between 0 and underflow\n");
  1542. X        printf("   threshold = %.17e .\n", UfThold);
  1543. X        }
  1544. X    else if (! (V9 > UfThold * (One + E9)))
  1545. X        printf("This computed value is O.K.\n");
  1546. X    else {
  1547. X        BadCond(Defect, "this is not between 0 and underflow\n");
  1548. X        printf("   threshold = %.17e .\n", UfThold);
  1549. X        }
  1550. X    /*=============================================*/
  1551. X    Milestone = 140;
  1552. X    /*=============================================*/
  1553. X    printf("\n");
  1554. X    /* ...calculate Exp2 == exp(2) == 7.389056099... */
  1555. X    X = Zero;
  1556. X    I = 2;
  1557. X    Y = Two * Three;
  1558. X    Q = Zero;
  1559. X    N = 0;
  1560. X    do  {
  1561. X        Z = X;
  1562. X        I = I + 1;
  1563. X        Y = Y / (I + I);
  1564. X        R = Y + Q;
  1565. X        X = Z + R;
  1566. X        Q = (Z - X) + R;
  1567. X        } while(X > Z);
  1568. X    Z = (OneAndHalf + One / Eight) + X / (OneAndHalf * ThirtyTwo);
  1569. X    X = Z * Z;
  1570. X    Exp2 = X * X;
  1571. X    X = F9;
  1572. X    Y = X - U1;
  1573. X    printf("Testing X^((X + 1) / (X - 1)) vs. exp(2) = %.17e as X -> 1.\n",
  1574. X        Exp2);
  1575. X    for(I = 1;;) {
  1576. X        Z = X - BInvrse;
  1577. X        Z = (X + One) / (Z - (One - BInvrse));
  1578. X        Q = POW(X, Z) - Exp2;
  1579. X        if (FABS(Q) > TwoForty * U2) {
  1580. X            N = 1;
  1581. X             V9 = (X - BInvrse) - (One - BInvrse);
  1582. X            BadCond(Defect, "Calculated");
  1583. X            printf(" %.17e for\n", POW(X,Z));
  1584. X            printf("\t(1 + (%.17e) ^ (%.17e);\n", V9, Z);
  1585. X            printf("\tdiffers from correct value by %.17e .\n", Q);
  1586. X            printf("\tThis much error may spoil financial\n");
  1587. X            printf("\tcalculations involving tiny interest rates.\n");
  1588. X            break;
  1589. X            }
  1590. X        else {
  1591. X            Z = (Y - X) * Two + Y;
  1592. X            X = Y;
  1593. X            Y = Z;
  1594. X            Z = One + (X - F9)*(X - F9);
  1595. X            if (Z > One && I < NoTrials) I++;
  1596. X            else  {
  1597. X                if (X > One) {
  1598. X                    if (N == 0)
  1599. X                       printf("Accuracy seems adequate.\n");
  1600. X                    break;
  1601. X                    }
  1602. X                else {
  1603. X                    X = One + U2;
  1604. X                    Y = U2 + U2;
  1605. X                    Y += X;
  1606. X                    I = 1;
  1607. X                    }
  1608. X                }
  1609. X            }
  1610. X        }
  1611. X    /*=============================================*/
  1612. X    Milestone = 150;
  1613. X    /*=============================================*/
  1614. X    printf("Testing powers Z^Q at four nearly extreme values.\n");
  1615. X    N = 0;
  1616. X    Z = A1;
  1617. X    Q = FLOOR(Half - LOG(C) / LOG(A1));
  1618. X    Break = False;
  1619. X    do  {
  1620. X        X = CInvrse;
  1621. X        Y = POW(Z, Q);
  1622. X        IsYeqX();
  1623. X        Q = - Q;
  1624. X        X = C;
  1625. X        Y = POW(Z, Q);
  1626. X        IsYeqX();
  1627. X        if (Z < One) Break = True;
  1628. X        else Z = AInvrse;
  1629. X        } while ( ! (Break));
  1630. X    PrintIfNPositive();
  1631. X    if (N == 0) printf(" ... no discrepancies found.\n");
  1632. X    printf("\n");
  1633. X    
  1634. X    /*=============================================*/
  1635. X    Milestone = 160;
  1636. X    /*=============================================*/
  1637. X    Pause();
  1638. X    printf("Searching for Overflow threshold:\n");
  1639. X    printf("This may generate an error.\n");
  1640. X    Y = - CInvrse;
  1641. X    V9 = HInvrse * Y;
  1642. X    sigsave = sigfpe;
  1643. X    if (setjmp(ovfl_buf)) { I = 0; V9 = Y; goto overflow; }
  1644. X    do {
  1645. X        V = Y;
  1646. X        Y = V9;
  1647. X        V9 = HInvrse * Y;
  1648. X        } while(V9 < Y);
  1649. X    I = 1;
  1650. overflow:
  1651. X    sigsave = 0;
  1652. X    Z = V9;
  1653. X    printf("Can `Z = -Y' overflow?\n");
  1654. X    printf("Trying it on Y = %.17e .\n", Y);
  1655. X    V9 = - Y;
  1656. X    V0 = V9;
  1657. X    if (V - Y == V + V0) printf("Seems O.K.\n");
  1658. X    else {
  1659. X        printf("finds a ");
  1660. X        BadCond(Flaw, "-(-Y) differs from Y.\n");
  1661. X        }
  1662. X    if (Z != Y) {
  1663. X        BadCond(Serious, "");
  1664. X        printf("overflow past %.17e\n\tshrinks to %.17e .\n", Y, Z);
  1665. X        }
  1666. X    if (I) {
  1667. X        Y = V * (HInvrse * U2 - HInvrse);
  1668. X        Z = Y + ((One - HInvrse) * U2) * V;
  1669. X        if (Z < V0) Y = Z;
  1670. X        if (Y < V0) V = Y;
  1671. X        if (V0 - V < V0) V = V0;
  1672. X        }
  1673. X    else {
  1674. X        V = Y * (HInvrse * U2 - HInvrse);
  1675. X        V = V + ((One - HInvrse) * U2) * Y;
  1676. X        }
  1677. X    printf("Overflow threshold is V  = %.17e .\n", V);
  1678. X    if (I) printf("Overflow saturates at V0 = %.17e .\n", V0);
  1679. X    else printf("There is no saturation value because \
  1680. the system traps on overflow.\n");
  1681. X    V9 = V * One;
  1682. X    printf("No Overflow should be signaled for V * 1 = %.17e\n", V9);
  1683. X    V9 = V / One;
  1684. X    printf("                           nor for V / 1 = %.17e .\n", V9);
  1685. X    printf("Any overflow signal separating this * from the one\n");
  1686. X    printf("above is a DEFECT.\n");
  1687. X    /*=============================================*/
  1688. X    Milestone = 170;
  1689. X    /*=============================================*/
  1690. X    if (!(-V < V && -V0 < V0 && -UfThold < V && UfThold < V)) {
  1691. X        BadCond(Failure, "Comparisons involving ");
  1692. X        printf("+-%g, +-%g\nand +-%g are confused by Overflow.",
  1693. X            V, V0, UfThold);
  1694. X        }
  1695. X    /*=============================================*/
  1696. X    Milestone = 175;
  1697. X    /*=============================================*/
  1698. X    printf("\n");
  1699. X    for(Indx = 1; Indx <= 3; ++Indx) {
  1700. X        switch (Indx)  {
  1701. X            case 1: Z = UfThold; break;
  1702. X            case 2: Z = E0; break;
  1703. X            case 3: Z = PseudoZero; break;
  1704. X            }
  1705. X        if (Z != Zero) {
  1706. X            V9 = SQRT(Z);
  1707. X            Y = V9 * V9;
  1708. X            if (Y / (One - Radix * E9) < Z
  1709. X               || Y > (One + Radix * E9) * Z) { /* dgh: + E9 --> * E9 */
  1710. X                if (V9 > U1) BadCond(Serious, "");
  1711. X                else BadCond(Defect, "");
  1712. X                printf("Comparison alleges that what prints as Z = %.17e\n", Z);
  1713. X                printf(" is too far from sqrt(Z) ^ 2 = %.17e .\n", Y);
  1714. X                }
  1715. X            }
  1716. X        }
  1717. X    /*=============================================*/
  1718. X    Milestone = 180;
  1719. X    /*=============================================*/
  1720. X    for(Indx = 1; Indx <= 2; ++Indx) {
  1721. X        if (Indx == 1) Z = V;
  1722. X        else Z = V0;
  1723. X        V9 = SQRT(Z);
  1724. X        X = (One - Radix * E9) * V9;
  1725. X        V9 = V9 * X;
  1726. X        if (((V9 < (One - Two * Radix * E9) * Z) || (V9 > Z))) {
  1727. X            Y = V9;
  1728. X            if (X < W) BadCond(Serious, "");
  1729. X            else BadCond(Defect, "");
  1730. X            printf("Comparison alleges that Z = %17e\n", Z);
  1731. X            printf(" is too far from sqrt(Z) ^ 2 (%.17e) .\n", Y);
  1732. X            }
  1733. X        }
  1734. X    /*=============================================*/
  1735. X    /*SPLIT
  1736. X    }
  1737. #include "paranoia.h"
  1738. part8(){
  1739. */
  1740. X    Milestone = 190;
  1741. X    /*=============================================*/
  1742. X    Pause();
  1743. X    X = UfThold * V;
  1744. X    Y = Radix * Radix;
  1745. X    if (X*Y < One || X > Y) {
  1746. X        if (X * Y < U1 || X > Y/U1) BadCond(Defect, "Badly");
  1747. X        else BadCond(Flaw, "");
  1748. X            
  1749. X        printf(" unbalanced range; UfThold * V = %.17e\n\t%s\n",
  1750. X            X, "is too far from 1.\n");
  1751. X        }
  1752. X    /*=============================================*/
  1753. X    Milestone = 200;
  1754. X    /*=============================================*/
  1755. X    for (Indx = 1; Indx <= 5; ++Indx)  {
  1756. X        X = F9;
  1757. X        switch (Indx)  {
  1758. X            case 2: X = One + U2; break;
  1759. X            case 3: X = V; break;
  1760. X            case 4: X = UfThold; break;
  1761. X            case 5: X = Radix;
  1762. X            }
  1763. X        Y = X;
  1764. X        sigsave = sigfpe;
  1765. X        if (setjmp(ovfl_buf))
  1766. X            printf("  X / X  traps when X = %g\n", X);
  1767. X        else {
  1768. X            V9 = (Y / X - Half) - Half;
  1769. X            if (V9 == Zero) continue;
  1770. X            if (V9 == - U1 && Indx < 5) BadCond(Flaw, "");
  1771. X            else BadCond(Serious, "");
  1772. X            printf("  X / X differs from 1 when X = %.17e\n", X);
  1773. X            printf("  instead, X / X - 1/2 - 1/2 = %.17e .\n", V9);
  1774. X            }
  1775. X        sigsave = 0;
  1776. X        }
  1777. X    /*=============================================*/
  1778. X    Milestone = 210;
  1779. X    /*=============================================*/
  1780. X    MyZero = Zero;
  1781. X    printf("\n");
  1782. X    printf("What message and/or values does Division by Zero produce?\n") ;
  1783. #ifndef NOPAUSE
  1784. X    printf("This can interupt your program.  You can ");
  1785. X    printf("skip this part if you wish.\n");
  1786. X    printf("Do you wish to compute 1 / 0? ");
  1787. X    fflush(stdout);
  1788. X    read (KEYBOARD, ch, 8);
  1789. X    if ((ch[0] == 'Y') || (ch[0] == 'y')) {
  1790. #endif
  1791. X        sigsave = sigfpe;
  1792. X        printf("    Trying to compute 1 / 0 produces ...");
  1793. X        if (!setjmp(ovfl_buf)) printf("  %.7e .\n", One / MyZero);
  1794. X        sigsave = 0;
  1795. #ifndef NOPAUSE
  1796. X        }
  1797. X    else printf("O.K.\n");
  1798. X    printf("\nDo you wish to compute 0 / 0? ");
  1799. X    fflush(stdout);
  1800. X    read (KEYBOARD, ch, 80);
  1801. X    if ((ch[0] == 'Y') || (ch[0] == 'y')) {
  1802. #endif
  1803. X        sigsave = sigfpe;
  1804. X        printf("\n    Trying to compute 0 / 0 produces ...");
  1805. X        if (!setjmp(ovfl_buf)) printf("  %.7e .\n", Zero / MyZero);
  1806. X        sigsave = 0;
  1807. #ifndef NOPAUSE
  1808. X        }
  1809. X    else printf("O.K.\n");
  1810. #endif
  1811. X    /*=============================================*/
  1812. X    Milestone = 220;
  1813. X    /*=============================================*/
  1814. X    Pause();
  1815. X    printf("\n");
  1816. X    {
  1817. X        static char *msg[] = {
  1818. X            "FAILUREs  encountered =",
  1819. X            "SERIOUS DEFECTs  discovered =",
  1820. X            "DEFECTs  discovered =",
  1821. X            "FLAWs  discovered =" };
  1822. X        int i;
  1823. X        for(i = 0; i < 4; i++) if (ErrCnt[i])
  1824. X            printf("The number of  %-29s %d.\n",
  1825. X                msg[i], ErrCnt[i]);
  1826. X        }
  1827. X    printf("\n");
  1828. X    if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[Defect]
  1829. X            + ErrCnt[Flaw]) > 0) {
  1830. X        if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[
  1831. X            Defect] == 0) && (ErrCnt[Flaw] > 0)) {
  1832. X            printf("The arithmetic diagnosed seems ");
  1833. X            printf("Satisfactory though flawed.\n");
  1834. X            }
  1835. X        if ((ErrCnt[Failure] + ErrCnt[Serious] == 0)
  1836. X            && ( ErrCnt[Defect] > 0)) {
  1837. X            printf("The arithmetic diagnosed may be Acceptable\n");
  1838. X            printf("despite inconvenient Defects.\n");
  1839. X            }
  1840. X        if ((ErrCnt[Failure] + ErrCnt[Serious]) > 0) {
  1841. X            printf("The arithmetic diagnosed has ");
  1842. X            printf("unacceptable Serious Defects.\n");
  1843. X            }
  1844. X        if (ErrCnt[Failure] > 0) {
  1845. X            printf("Potentially fatal FAILURE may have spoiled this");
  1846. X            printf(" program's subsequent diagnoses.\n");
  1847. X            }
  1848. X        }
  1849. X    else {
  1850. X        printf("No failures, defects nor flaws have been discovered.\n");
  1851. X        if (! ((RMult == Rounded) && (RDiv == Rounded)
  1852. X            && (RAddSub == Rounded) && (RSqrt == Rounded))) 
  1853. X            printf("The arithmetic diagnosed seems Satisfactory.\n");
  1854. X        else {
  1855. X            if (StickyBit >= One &&
  1856. X                (Radix - Two) * (Radix - Nine - One) == Zero) {
  1857. X                printf("Rounding appears to conform to ");
  1858. X                printf("the proposed IEEE standard P");
  1859. X                if ((Radix == Two) &&
  1860. X                     ((Precision - Four * Three * Two) *
  1861. X                      ( Precision - TwentySeven -
  1862. X                       TwentySeven + One) == Zero)) 
  1863. X                    printf("754");
  1864. X                else printf("854");
  1865. X                if (IEEE) printf(".\n");
  1866. X                else {
  1867. X                    printf(",\nexcept for possibly Double Rounding");
  1868. X                    printf(" during Gradual Underflow.\n");
  1869. X                    }
  1870. X                }
  1871. X            printf("The arithmetic diagnosed appears to be Excellent!\n");
  1872. X            }
  1873. X        }
  1874. X    if (fpecount)
  1875. X        printf("\nA total of %d floating point exceptions were registered.\n",
  1876. X            fpecount);
  1877. X    printf("END OF TEST.\n");
  1878. X
  1879. #ifdef TEST
  1880. X    ieee_retrospective((FILE *)NULL);
  1881. #endif
  1882. X    asm("fnclex");
  1883. X        return(0);
  1884. X    }
  1885. X
  1886. /*SPLIT subs.c
  1887. #include "paranoia.h"
  1888. */
  1889. X
  1890. /* Sign */
  1891. X
  1892. FLOAT Sign (X)
  1893. FLOAT X;
  1894. { return X >= 0. ? 1.0 : -1.0; }
  1895. X
  1896. /* Pause */
  1897. X
  1898. Pause()
  1899. {
  1900. #ifndef NOPAUSE
  1901. X    char ch[8];
  1902. X
  1903. X    printf("\nTo continue, press RETURN");
  1904. X    fflush(stdout);
  1905. X    read(KEYBOARD, ch, 8);
  1906. #endif
  1907. X    printf("\nDiagnosis resumes after milestone Number %d", Milestone);
  1908. X    printf("          Page: %d\n\n", PageNo);
  1909. X    ++Milestone;
  1910. X    ++PageNo;
  1911. X    }
  1912. X
  1913. X /* TstCond */
  1914. X
  1915. TstCond (K, Valid, T)
  1916. int K, Valid;
  1917. char *T;
  1918. { if (! Valid) { BadCond(K,T); printf(".\n"); } }
  1919. X
  1920. BadCond(K, T)
  1921. int K;
  1922. char *T;
  1923. {
  1924. X    static char *msg[] = { "FAILURE", "SERIOUS DEFECT", "DEFECT", "FLAW" };
  1925. X
  1926. X    ErrCnt [K] = ErrCnt [K] + 1;
  1927. X    printf("%s:  %s", msg[K], T);
  1928. X    }
  1929. X
  1930. /* Random */
  1931. /*  Random computes
  1932. X     X = (Random1 + Random9)^5
  1933. X     Random1 = X - FLOOR(X) + 0.000005 * X;
  1934. X   and returns the new value of Random1
  1935. */
  1936. X
  1937. FLOAT Random()
  1938. {
  1939. X    FLOAT X, Y;
  1940. X    
  1941. X    X = Random1 + Random9;
  1942. X    Y = X * X;
  1943. X    Y = Y * Y;
  1944. X    X = X * Y;
  1945. X    Y = X - FLOOR(X);
  1946. X    Random1 = Y + X * 0.000005;
  1947. X    return(Random1);
  1948. X    }
  1949. X
  1950. /* SqXMinX */
  1951. X
  1952. SqXMinX (ErrKind)
  1953. int ErrKind;
  1954. {
  1955. X    FLOAT XA, XB;
  1956. X    
  1957. X    XB = X * BInvrse;
  1958. X    XA = X - XB;
  1959. X    SqEr = ((SQRT(X * X) - XB) - XA) / OneUlp;
  1960. X    if (SqEr != Zero) {
  1961. X        if (SqEr < MinSqEr) MinSqEr = SqEr;
  1962. X        if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1963. X        J = J + 1.0;
  1964. X        BadCond(ErrKind, "\n");
  1965. X        printf("sqrt( %.17e) - %.17e  = %.17e\n", X * X, X, OneUlp * SqEr);
  1966. X        printf("\tinstead of correct value 0 .\n");
  1967. X        }
  1968. X    }
  1969. X
  1970. /* NewD */
  1971. X
  1972. NewD()
  1973. {
  1974. X    X = Z1 * Q;
  1975. X    X = FLOOR(Half - X / Radix) * Radix + X;
  1976. X    Q = (Q - X * Z) / Radix + X * X * (D / Radix);
  1977. X    Z = Z - Two * X * D;
  1978. X    if (Z <= Zero) {
  1979. X        Z = - Z;
  1980. X        Z1 = - Z1;
  1981. X        }
  1982. X    D = Radix * D;
  1983. X    }
  1984. X
  1985. /* SR3750 */
  1986. X
  1987. SR3750()
  1988. {
  1989. X    if (! ((X - Radix < Z2 - Radix) || (X - Z2 > W - Z2))) {
  1990. X        I = I + 1;
  1991. X        X2 = SQRT(X * D);
  1992. X        Y2 = (X2 - Z2) - (Y - Z2);
  1993. X        X2 = X8 / (Y - Half);
  1994. X        X2 = X2 - Half * X2 * X2;
  1995. X        SqEr = (Y2 + Half) + (Half - X2);
  1996. X        if (SqEr < MinSqEr) MinSqEr = SqEr;
  1997. X        SqEr = Y2 - X2;
  1998. X        if (SqEr > MaxSqEr) MaxSqEr = SqEr;
  1999. X        }
  2000. X    }
  2001. X
  2002. /* IsYeqX */
  2003. X
  2004. IsYeqX()
  2005. {
  2006. X    if (Y != X) {
  2007. X        if (N <= 0) {
  2008. X            if (Z == Zero && Q <= Zero)
  2009. X                printf("WARNING:  computing\n");
  2010. X            else BadCond(Defect, "computing\n");
  2011. X            printf("\t(%.17e) ^ (%.17e)\n", Z, Q);
  2012. X            printf("\tyielded %.17e;\n", Y);
  2013. X            printf("\twhich compared unequal to correct %.17e ;\n",
  2014. X                X);
  2015. X            printf("\t\tthey differ by %.17e .\n", Y - X);
  2016. X            }
  2017. X        N = N + 1; /* ... count discrepancies. */
  2018. X        }
  2019. X    }
  2020. X
  2021. /* SR3980 */
  2022. X
  2023. SR3980()
  2024. {
  2025. X    do {
  2026. X        Q = (FLOAT) I;
  2027. X        Y = POW(Z, Q);
  2028. X        IsYeqX();
  2029. X        if (++I > M) break;
  2030. X        X = Z * X;
  2031. X        } while ( X < W );
  2032. X    }
  2033. X
  2034. /* PrintIfNPositive */
  2035. X
  2036. PrintIfNPositive()
  2037. {
  2038. X    if (N > 0) printf("Similar discrepancies have occurred %d times.\n", N);
  2039. X    }
  2040. X
  2041. /* TstPtUf */
  2042. X
  2043. TstPtUf()
  2044. {
  2045. X    N = 0;
  2046. X    if (Z != Zero) {
  2047. X        printf("Since comparison denies Z = 0, evaluating ");
  2048. X        printf("(Z + Z) / Z should be safe.\n");
  2049. X        sigsave = sigfpe;
  2050. X        if (setjmp(ovfl_buf)) goto very_serious;
  2051. X        Q9 = (Z + Z) / Z;
  2052. X        printf("What the machine gets for (Z + Z) / Z is  %.17e .\n",
  2053. X            Q9);
  2054. X        if (FABS(Q9 - Two) < Radix * U2) {
  2055. X            printf("This is O.K., provided Over/Underflow");
  2056. X            printf(" has NOT just been signaled.\n");
  2057. X            }
  2058. X        else {
  2059. X            if ((Q9 < One) || (Q9 > Two)) {
  2060. very_serious:
  2061. X                N = 1;
  2062. X                ErrCnt [Serious] = ErrCnt [Serious] + 1;
  2063. X                printf("This is a VERY SERIOUS DEFECT!\n");
  2064. X                }
  2065. X            else {
  2066. X                N = 1;
  2067. X                ErrCnt [Defect] = ErrCnt [Defect] + 1;
  2068. X                printf("This is a DEFECT!\n");
  2069. X                }
  2070. X            }
  2071. X        sigsave = 0;
  2072. X        V9 = Z * One;
  2073. X        Random1 = V9;
  2074. X        V9 = One * Z;
  2075. X        Random2 = V9;
  2076. X        V9 = Z / One;
  2077. X        if ((Z == Random1) && (Z == Random2) && (Z == V9)) {
  2078. X            if (N > 0) Pause();
  2079. X            }
  2080. X        else {
  2081. X            N = 1;
  2082. X            BadCond(Defect, "What prints as Z = ");
  2083. X            printf("%.17e\n\tcompares different from  ", Z);
  2084. X            if (Z != Random1) printf("Z * 1 = %.17e ", Random1);
  2085. X            if (! ((Z == Random2)
  2086. X                || (Random2 == Random1)))
  2087. X                printf("1 * Z == %g\n", Random2);
  2088. X            if (! (Z == V9)) printf("Z / 1 = %.17e\n", V9);
  2089. X            if (Random2 != Random1) {
  2090. X                ErrCnt [Defect] = ErrCnt [Defect] + 1;
  2091. X                BadCond(Defect, "Multiplication does not commute!\n");
  2092. X                printf("\tComparison alleges that 1 * Z = %.17e\n",
  2093. X                    Random2);
  2094. X                printf("\tdiffers from Z * 1 = %.17e\n", Random1);
  2095. X                }
  2096. X            Pause();
  2097. X            }
  2098. X        }
  2099. X    }
  2100. X
  2101. notify(s)
  2102. char *s;
  2103. {
  2104. X    printf("%s test appears to be inconsistent...\n", s);
  2105. X    printf("   PLEASE NOTIFY KARPINKSI!\n");
  2106. X    }
  2107. X
  2108. /*SPLIT msgs.c */
  2109. X
  2110. /* Instructions */
  2111. X
  2112. msglist(s)
  2113. char **s;
  2114. { while(*s) printf("%s\n", *s++); }
  2115. X
  2116. Instructions()
  2117. {
  2118. X  static char *instr[] = {
  2119. X    "Lest this program stop prematurely, i.e. before displaying\n",
  2120. X    "    `END OF TEST',\n",
  2121. X    "try to persuade the computer NOT to terminate execution when an",
  2122. X    "error like Over/Underflow or Division by Zero occurs, but rather",
  2123. X    "to persevere with a surrogate value after, perhaps, displaying some",
  2124. X    "warning.  If persuasion avails naught, don't despair but run this",
  2125. X    "program anyway to see how many milestones it passes, and then",
  2126. X    "amend it to make further progress.\n",
  2127. X    "Answer questions with Y, y, N or n (unless otherwise indicated).\n",
  2128. X    0};
  2129. X
  2130. X    msglist(instr);
  2131. X    }
  2132. X
  2133. /* Heading */
  2134. X
  2135. Heading()
  2136. {
  2137. X  static char *head[] = {
  2138. X    "Users are invited to help debug and augment this program so it will",
  2139. X    "cope with unanticipated and newly uncovered arithmetic pathologies.\n",
  2140. X    "Please send suggestions and interesting results to",
  2141. X    "\tRichard Karpinski",
  2142. X    "\tComputer Center U-76",
  2143. X    "\tUniversity of California",
  2144. X    "\tSan Francisco, CA 94143-0704, USA\n",
  2145. X    "In doing so, please include the following information:",
  2146. #ifdef Single
  2147. X    "\tPrecision:\tsingle;",
  2148. #else
  2149. X    "\tPrecision:\tdouble;",
  2150. #endif
  2151. X    "\tVersion:\t10 February 1989;",
  2152. X    "\tComputer:\n",
  2153. X    "\tCompiler:\n",
  2154. X    "\tOptimization level:\n",
  2155. X    "\tOther relevant compiler options:",
  2156. X    0};
  2157. X
  2158. X    msglist(head);
  2159. X    }
  2160. X
  2161. /* Characteristics */
  2162. X
  2163. Characteristics()
  2164. {
  2165. X    static char *chars[] = {
  2166. X     "Running this program should reveal these characteristics:",
  2167. X    "     Radix = 1, 2, 4, 8, 10, 16, 100, 256 ...",
  2168. X    "     Precision = number of significant digits carried.",
  2169. X    "     U2 = Radix/Radix^Precision = One Ulp",
  2170. X    "\t(OneUlpnit in the Last Place) of 1.000xxx .",
  2171. X    "     U1 = 1/Radix^Precision = One Ulp of numbers a little less than 1.0 .",
  2172. X    "     Adequacy of guard digits for Mult., Div. and Subt.",
  2173. X    "     Whether arithmetic is chopped, correctly rounded, or something else",
  2174. X    "\tfor Mult., Div., Add/Subt. and Sqrt.",
  2175. X    "     Whether a Sticky Bit used correctly for rounding.",
  2176. X    "     UnderflowThreshold = an underflow threshold.",
  2177. X    "     E0 and PseudoZero tell whether underflow is abrupt, gradual, or fuzzy.",
  2178. X    "     V = an overflow threshold, roughly.",
  2179. X    "     V0  tells, roughly, whether  Infinity  is represented.",
  2180. X    "     Comparisions are checked for consistency with subtraction",
  2181. X    "\tand for contamination with pseudo-zeros.",
  2182. X    "     Sqrt is tested.  Y^X is not tested.",
  2183. X    "     Extra-precise subexpressions are revealed but NOT YET tested.",
  2184. X    "     Decimal-Binary conversion is NOT YET tested for accuracy.",
  2185. X    0};
  2186. X
  2187. X    msglist(chars);
  2188. X    }
  2189. X
  2190. History()
  2191. X
  2192. { /* History */
  2193. X /* Converted from Brian Wichmann's Pascal version to C by Thos Sumner,
  2194. X    with further massaging by David M. Gay. */
  2195. X
  2196. X  static char *hist[] = {
  2197. X    "The program attempts to discriminate among",
  2198. X    "   FLAWs, like lack of a sticky bit,",
  2199. X    "   Serious DEFECTs, like lack of a guard digit, and",
  2200. X    "   FAILUREs, like 2+2 == 5 .",
  2201. X    "Failures may confound subsequent diagnoses.\n",
  2202. X    "The diagnostic capabilities of this program go beyond an earlier",
  2203. X    "program called `MACHAR', which can be found at the end of the",
  2204. X    "book  `Software Manual for the Elementary Functions' (1980) by",
  2205. X    "W. J. Cody and W. Waite. Although both programs try to discover",
  2206. X    "the Radix, Precision and range (over/underflow thresholds)",
  2207. X    "of the arithmetic, this program tries to cope with a wider variety",
  2208. X    "of pathologies, and to say how well the arithmetic is implemented.",
  2209. X    "\nThe program is based upon a conventional radix representation for",
  2210. X    "floating-point numbers, but also allows logarithmic encoding",
  2211. X    "as used by certain early WANG machines.\n",
  2212. X    "BASIC version of this program (C) 1983 by Prof. W. M. Kahan;",
  2213. X    "see source comments for more history.",
  2214. X    0};
  2215. X
  2216. X    msglist(hist);
  2217. X    }
  2218. SHAR_EOF
  2219. chmod 0644 paranoia.c ||
  2220. echo 'restore of paranoia.c failed'
  2221. Wc_c="`wc -c < 'paranoia.c'`"
  2222. test 57545 -eq "$Wc_c" ||
  2223.     echo 'paranoia.c: original size 57545, current size' "$Wc_c"
  2224. fi
  2225. true || echo 'restore of pow.s failed'
  2226. echo End of part 3, continue with part 4
  2227. exit 0
  2228. --
  2229. Glenn Geers                       | "So when it's over, we're back to people.
  2230. Department of Theoretical Physics |  Just to prove that human touch can have
  2231. The University of Sydney          |  no equal."
  2232. Sydney NSW 2006 Australia         |  - Basia Trzetrzelewska, 'Prime Time TV'
  2233.