⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paranoia.c

📁 lcc,一个可变目标c语言编译器的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
			V, V0, UfThold);		}	/*=============================================*/	Milestone = 175;	/*=============================================*/	printf("\n");	for(Indx = 1; Indx <= 3; ++Indx) {		switch (Indx)  {			case 1: Z = UfThold; break;			case 2: Z = E0; break;			case 3: Z = PseudoZero; break;			}		if (Z != Zero) {			V9 = SQRT(Z);			Y = V9 * V9;			if (Y / (One - Radix * E9) < Z			   || Y > (One + Radix * E9) * Z) { /* dgh: + E9 --> * E9 */				if (V9 > U1) BadCond(Serious, "");				else BadCond(Defect, "");				printf("Comparison alleges that what prints as Z = %.17e\n", Z);				printf(" is too far from sqrt(Z) ^ 2 = %.17e .\n", Y);				}			}		}	/*=============================================*/	Milestone = 180;	/*=============================================*/	for(Indx = 1; Indx <= 2; ++Indx) {		if (Indx == 1) Z = V;		else Z = V0;		V9 = SQRT(Z);		X = (One - Radix * E9) * V9;		V9 = V9 * X;		if (((V9 < (One - Two * Radix * E9) * Z) || (V9 > Z))) {			Y = V9;			if (X < W) BadCond(Serious, "");			else BadCond(Defect, "");			printf("Comparison alleges that Z = %17e\n", Z);			printf(" is too far from sqrt(Z) ^ 2 (%.17e) .\n", Y);			}		}	/*=============================================*/	/*SPLIT	}#include "paranoia.h"part8(){*/	Milestone = 190;	/*=============================================*/	Pause();	X = UfThold * V;	Y = Radix * Radix;	if (X*Y < One || X > Y) {		if (X * Y < U1 || X > Y/U1) BadCond(Defect, "Badly");		else BadCond(Flaw, "");					printf(" unbalanced range; UfThold * V = %.17e\n\t%s\n",			X, "is too far from 1.\n");		}	/*=============================================*/	Milestone = 200;	/*=============================================*/	for (Indx = 1; Indx <= 5; ++Indx)  {		X = F9;		switch (Indx)  {			case 2: X = One + U2; break;			case 3: X = V; break;			case 4: X = UfThold; break;			case 5: X = Radix;			}		Y = X;		sigsave = sigfpe;		if (setjmp(ovfl_buf))			printf("  X / X  traps when X = %g\n", X);		else {			V9 = (Y / X - Half) - Half;			if (V9 == Zero) continue;			if (V9 == - U1 && Indx < 5) BadCond(Flaw, "");			else BadCond(Serious, "");			printf("  X / X differs from 1 when X = %.17e\n", X);			printf("  instead, X / X - 1/2 - 1/2 = %.17e .\n", V9);			}		sigsave = 0;		}	/*=============================================*/	Milestone = 210;	/*=============================================*/	MyZero = Zero;	printf("\n");	printf("What message and/or values does Division by Zero produce?\n") ;#ifndef NOPAUSE	printf("This can interupt your program.  You can ");	printf("skip this part if you wish.\n");	printf("Do you wish to compute 1 / 0? ");	fflush(stdout);	read (KEYBOARD, ch, 8);	if ((ch[0] == 'Y') || (ch[0] == 'y')) {#endif		sigsave = sigfpe;		printf("    Trying to compute 1 / 0 produces ...");		if (!setjmp(ovfl_buf)) printf("  %.7e .\n", One / MyZero);		sigsave = 0;#ifndef NOPAUSE		}	else printf("O.K.\n");	printf("\nDo you wish to compute 0 / 0? ");	fflush(stdout);	read (KEYBOARD, ch, 80);	if ((ch[0] == 'Y') || (ch[0] == 'y')) {#endif		sigsave = sigfpe;		printf("\n    Trying to compute 0 / 0 produces ...");		if (!setjmp(ovfl_buf)) printf("  %.7e .\n", Zero / MyZero);		sigsave = 0;#ifndef NOPAUSE		}	else printf("O.K.\n");#endif	/*=============================================*/	Milestone = 220;	/*=============================================*/	Pause();	printf("\n");	{		static char *msg[] = {			"FAILUREs  encountered =",			"SERIOUS DEFECTs  discovered =",			"DEFECTs  discovered =",			"FLAWs  discovered =" };		int i;		for(i = 0; i < 4; i++) if (ErrCnt[i])			printf("The number of  %-29s %d.\n",				msg[i], ErrCnt[i]);		}	printf("\n");	if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[Defect]			+ ErrCnt[Flaw]) > 0) {		if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[			Defect] == 0) && (ErrCnt[Flaw] > 0)) {			printf("The arithmetic diagnosed seems ");			printf("Satisfactory though flawed.\n");			}		if ((ErrCnt[Failure] + ErrCnt[Serious] == 0)			&& ( ErrCnt[Defect] > 0)) {			printf("The arithmetic diagnosed may be Acceptable\n");			printf("despite inconvenient Defects.\n");			}		if ((ErrCnt[Failure] + ErrCnt[Serious]) > 0) {			printf("The arithmetic diagnosed has ");			printf("unacceptable Serious Defects.\n");			}		if (ErrCnt[Failure] > 0) {			printf("Potentially fatal FAILURE may have spoiled this");			printf(" program's subsequent diagnoses.\n");			}		}	else {		printf("No failures, defects nor flaws have been discovered.\n");		if (! ((RMult == Rounded) && (RDiv == Rounded)			&& (RAddSub == Rounded) && (RSqrt == Rounded))) 			printf("The arithmetic diagnosed seems Satisfactory.\n");		else {			if (StickyBit >= One &&				(Radix - Two) * (Radix - Nine - One) == Zero) {				printf("Rounding appears to conform to ");				printf("the proposed IEEE standard P");				if ((Radix == Two) &&					 ((Precision - Four * Three * Two) *					  ( Precision - TwentySeven -					   TwentySeven + One) == Zero)) 					printf("754");				else printf("854");				if (IEEE) printf(".\n");				else {					printf(",\nexcept for possibly Double Rounding");					printf(" during Gradual Underflow.\n");					}				}			printf("The arithmetic diagnosed appears to be Excellent!\n");			}		}	if (fpecount)		printf("\nA total of %d floating point exceptions were registered.\n",			fpecount);	printf("END OF TEST.\n");	return 0;	}/*SPLIT subs.c#include "paranoia.h"*//* Sign */FLOAT Sign (X)FLOAT X;{ return X >= 0. ? 1.0 : -1.0; }/* Pause */Pause(){#ifndef NOPAUSE	char ch[8];	printf("\nTo continue, press RETURN");	fflush(stdout);	read(KEYBOARD, ch, 8);#endif	printf("\nDiagnosis resumes after milestone Number %d", Milestone);	printf("          Page: %d\n\n", PageNo);	++Milestone;	++PageNo;	} /* TstCond */TstCond (K, Valid, T)int K, Valid;char *T;{ if (! Valid) { BadCond(K,T); printf(".\n"); } }BadCond(K, T)int K;char *T;{	static char *msg[] = { "FAILURE", "SERIOUS DEFECT", "DEFECT", "FLAW" };	ErrCnt [K] = ErrCnt [K] + 1;	printf("%s:  %s", msg[K], T);	}/* Random *//*  Random computes     X = (Random1 + Random9)^5     Random1 = X - FLOOR(X) + 0.000005 * X;   and returns the new value of Random1*/FLOAT Random(){	FLOAT X, Y;		X = Random1 + Random9;	Y = X * X;	Y = Y * Y;	X = X * Y;	Y = X - FLOOR(X);	Random1 = Y + X * 0.000005;	return(Random1);	}/* SqXMinX */SqXMinX (ErrKind)int ErrKind;{	FLOAT XA, XB;		XB = X * BInvrse;	XA = X - XB;	SqEr = ((SQRT(X * X) - XB) - XA) / OneUlp;	if (SqEr != Zero) {		if (SqEr < MinSqEr) MinSqEr = SqEr;		if (SqEr > MaxSqEr) MaxSqEr = SqEr;		J = J + 1.0;		BadCond(ErrKind, "\n");		printf("sqrt( %.17e) - %.17e  = %.17e\n", X * X, X, OneUlp * SqEr);		printf("\tinstead of correct value 0 .\n");		}	}/* NewD */NewD(){	X = Z1 * Q;	X = FLOOR(Half - X / Radix) * Radix + X;	Q = (Q - X * Z) / Radix + X * X * (D / Radix);	Z = Z - Two * X * D;	if (Z <= Zero) {		Z = - Z;		Z1 = - Z1;		}	D = Radix * D;	}/* SR3750 */SR3750(){	if (! ((X - Radix < Z2 - Radix) || (X - Z2 > W - Z2))) {		I = I + 1;		X2 = SQRT(X * D);		Y2 = (X2 - Z2) - (Y - Z2);		X2 = X8 / (Y - Half);		X2 = X2 - Half * X2 * X2;		SqEr = (Y2 + Half) + (Half - X2);		if (SqEr < MinSqEr) MinSqEr = SqEr;		SqEr = Y2 - X2;		if (SqEr > MaxSqEr) MaxSqEr = SqEr;		}	}/* IsYeqX */IsYeqX(){	if (Y != X) {		if (N <= 0) {			if (Z == Zero && Q <= Zero)				printf("WARNING:  computing\n");			else BadCond(Defect, "computing\n");			printf("\t(%.17e) ^ (%.17e)\n", Z, Q);			printf("\tyielded %.17e;\n", Y);			printf("\twhich compared unequal to correct %.17e ;\n",				X);			printf("\t\tthey differ by %.17e .\n", Y - X);			}		N = N + 1; /* ... count discrepancies. */		}	}/* SR3980 */SR3980(){	do {		Q = (FLOAT) I;		Y = POW(Z, Q);		IsYeqX();		if (++I > M) break;		X = Z * X;		} while ( X < W );	}/* PrintIfNPositive */PrintIfNPositive(){	if (N > 0) printf("Similar discrepancies have occurred %d times.\n", N);	}/* TstPtUf */TstPtUf(){	N = 0;	if (Z != Zero) {		printf("Since comparison denies Z = 0, evaluating ");		printf("(Z + Z) / Z should be safe.\n");		sigsave = sigfpe;		if (setjmp(ovfl_buf)) goto very_serious;		Q9 = (Z + Z) / Z;		printf("What the machine gets for (Z + Z) / Z is  %.17e .\n",			Q9);		if (FABS(Q9 - Two) < Radix * U2) {			printf("This is O.K., provided Over/Underflow");			printf(" has NOT just been signaled.\n");			}		else {			if ((Q9 < One) || (Q9 > Two)) {very_serious:				N = 1;				ErrCnt [Serious] = ErrCnt [Serious] + 1;				printf("This is a VERY SERIOUS DEFECT!\n");				}			else {				N = 1;				ErrCnt [Defect] = ErrCnt [Defect] + 1;				printf("This is a DEFECT!\n");				}			}		sigsave = 0;		V9 = Z * One;		Random1 = V9;		V9 = One * Z;		Random2 = V9;		V9 = Z / One;		if ((Z == Random1) && (Z == Random2) && (Z == V9)) {			if (N > 0) Pause();			}		else {			N = 1;			BadCond(Defect, "What prints as Z = ");			printf("%.17e\n\tcompares different from  ", Z);			if (Z != Random1) printf("Z * 1 = %.17e ", Random1);			if (! ((Z == Random2)				|| (Random2 == Random1)))				printf("1 * Z == %g\n", Random2);			if (! (Z == V9)) printf("Z / 1 = %.17e\n", V9);			if (Random2 != Random1) {				ErrCnt [Defect] = ErrCnt [Defect] + 1;				BadCond(Defect, "Multiplication does not commute!\n");				printf("\tComparison alleges that 1 * Z = %.17e\n",					Random2);				printf("\tdiffers from Z * 1 = %.17e\n", Random1);				}			Pause();			}		}	}notify(s)char *s;{	printf("%s test appears to be inconsistent...\n", s);	printf("   PLEASE NOTIFY KARPINKSI!\n");	}/*SPLIT msgs.c *//* Instructions */msglist(s)char **s;{ while(*s) printf("%s\n", *s++); }Instructions(){  static char *instr[] = {	"Lest this program stop prematurely, i.e. before displaying\n",	"    `END OF TEST',\n",	"try to persuade the computer NOT to terminate execution when an",	"error like Over/Underflow or Division by Zero occurs, but rather",	"to persevere with a surrogate value after, perhaps, displaying some",	"warning.  If persuasion avails naught, don't despair but run this",	"program anyway to see how many milestones it passes, and then",	"amend it to make further progress.\n",	"Answer questions with Y, y, N or n (unless otherwise indicated).\n",	0};	msglist(instr);	}/* Heading */Heading(){  static char *head[] = {	"Users are invited to help debug and augment this program so it will",	"cope with unanticipated and newly uncovered arithmetic pathologies.\n",	"Please send suggestions and interesting results to",	"\tRichard Karpinski",	"\tComputer Center U-76",	"\tUniversity of California",	"\tSan Francisco, CA 94143-0704, USA\n",	"In doing so, please include the following information:",#ifdef Single	"\tPrecision:\tsingle;",#else	"\tPrecision:\tdouble;",#endif	"\tVersion:\t10 February 1989;",	"\tComputer:\n",	"\tCompiler:\n",	"\tOptimization level:\n",	"\tOther relevant compiler options:",	0};	msglist(head);	}/* Characteristics */Characteristics(){	static char *chars[] = {	 "Running this program should reveal these characteristics:",	"     Radix = 1, 2, 4, 8, 10, 16, 100, 256 ...",	"     Precision = number of significant digits carried.",	"     U2 = Radix/Radix^Precision = One Ulp",	"\t(OneUlpnit in the Last Place) of 1.000xxx .",	"     U1 = 1/Radix^Precision = One Ulp of numbers a little less than 1.0 .",	"     Adequacy of guard digits for Mult., Div. and Subt.",	"     Whether arithmetic is chopped, correctly rounded, or something else",	"\tfor Mult., Div., Add/Subt. and Sqrt.",	"     Whether a Sticky Bit used correctly for rounding.",	"     UnderflowThreshold = an underflow threshold.",	"     E0 and PseudoZero tell whether underflow is abrupt, gradual, or fuzzy.",	"     V = an overflow threshold, roughly.",	"     V0  tells, roughly, whether  Infinity  is represented.",	"     Comparisions are checked for consistency with subtraction",	"\tand for contamination with pseudo-zeros.",	"     Sqrt is tested.  Y^X is not tested.",	"     Extra-precise subexpressions are revealed but NOT YET tested.",	"     Decimal-Binary conversion is NOT YET tested for accuracy.",	0};	msglist(chars);	}History(){ /* History */ /* Converted from Brian Wichmann's Pascal version to C by Thos Sumner,	with further massaging by David M. Gay. */  static char *hist[] = {	"The program attempts to discriminate among",	"   FLAWs, like lack of a sticky bit,",	"   Serious DEFECTs, like lack of a guard digit, and",	"   FAILUREs, like 2+2 == 5 .",	"Failures may confound subsequent diagnoses.\n",	"The diagnostic capabilities of this program go beyond an earlier",	"program called `MACHAR', which can be found at the end of the",	"book  `Software Manual for the Elementary Functions' (1980) by",	"W. J. Cody and W. Waite. Although both programs try to discover",	"the Radix, Precision and range (over/underflow thresholds)",	"of the arithmetic, this program tries to cope with a wider variety",	"of pathologies, and to say how well the arithmetic is implemented.",	"\nThe program is based upon a conventional radix representation for",	"floating-point numbers, but also allows logarithmic encoding",	"as used by certain early WANG machines.\n",	"BASIC version of this program (C) 1983 by Prof. W. M. Kahan;",	"see source comments for more history.",	0};	msglist(hist);	}doublepow(x, y) /* return x ^ y (exponentiation) */double x, y;{	extern double exp(), frexp(), ldexp(), log(), modf();	double xy, ye;	long i;	int ex, ey = 0, flip = 0;	if (!y) return 1.0;	if ((y < -1100. || y > 1100.) && x != -1.) return exp(y * log(x));	if (y < 0.) { y = -y; flip = 1; }	y = modf(y, &ye);	if (y) xy = exp(y * log(x));	else xy = 1.0;	/* next several lines assume >= 32 bit integers */	x = frexp(x, &ex);	if (i = ye) for(;;) {		if (i & 1) { xy *= x; ey += ex; }		if (!(i >>= 1)) break;		x *= x;		ex *= 2;		if (x < .5) { x *= 2.; ex -= 1; }		}	if (flip) { xy = 1. / xy; ey = -ey; }	return ldexp(xy, ey);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -