home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / BENCHMARKS / paranoia.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  57KB  |  2,172 lines

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