lparanoi.c 57 KB

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