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

📄 ieetst.c

📁 linux下用PCMCIA无线网卡虚拟无线AP的程序源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Floating point to ASCII input and output string test program.
 *
 * Numbers in the native machine data structure are converted
 * to e type, then to and from decimal ASCII strings.  Native
 * printf() and scanf() functions are also used to produce
 * and read strings.  The resulting e type binary values
 * are compared, with diagnostic printouts of any discrepancies.
 *
 * Steve Moshier, 16 Dec 88
 * last revision: 16 May 92
 */

#include "ehead.h"
#include "mconf.h"

/* Include tests of 80-bit long double precision: */
#define LDOUBLE 0
/* Abort subtest after getting this many errors: */
#define MAXERR 5
/* Number of random arguments to try (set as large as you have
 * patience for): */
#define NRAND 100
/* Perform internal consistency test: */
#define CHKINTERNAL 0

static unsigned short fullp[NE], rounded[NE];
float prec24, sprec24, ssprec24;
double prec53, sprec53, ssprec53;
#if LDOUBLE
long double prec64, sprec64, ssprec64;
#endif

static unsigned short rprint[NE], rscan[NE];
static unsigned short q1[NE], q2[NE], q5[NE];
static unsigned short e1[NE], e2[NE], e3[NE];
static double d1, d2;
static int errprint = 0;
static int errscan = 0;
static int identerr = 0;
static int errtot = 0;
static int count = 0;
static char str0[80], str1[80], str2[80], str3[80];
static unsigned short eten[NE], maxm[NE];

int m, n, k2, mprec, SPREC;

char *Ten = "10.0";
char tformat[10];
char *format24 = "%.8e";
#ifdef DEC
char *format53 = "%.17e";
#else
char *format53 = "%.16e";
#endif
char *fformat24 = "%e";
char *fformat53 = "%le";
char *pct = "%";
char *quo = "\042";
#if LDOUBLE
char *format64 = "%.20Le";
char *fformat64 = "%Le";
#endif
char *format;
char *fformat;
char *toomany = "Too many errors; aborting this test.\n";

static int mnrflag;
static int etrflag;
void chkit(), printerr(), mnrand(), etrand(), shownoncrit();
void chkid(), pvec();

main()
{
int i, iprec;

printf( "Steve Moshier's printf/scanf tester, version 0.2.\n\n" );
#ifdef DEC
 /* DEC PDP-11/VAX single precision not yet implemented */
for( iprec = 1; iprec<2; iprec++ )
#else
for( iprec = 0; iprec<3; iprec++ )
#endif
	{
	errscan = 0;
	identerr = 0;
	errprint = 0;
	eclear( rprint );
	eclear( rscan );

switch( iprec )
	{
	case 0:
		SPREC = 8; /* # digits after the decimal point */
		mprec = 24; /* # bits in the significand */
		m = 9; /* max # decimal digits for correct rounding */
		n = 13; /* max power of ten for correct rounding */
		k2 = -125; /* underflow beyond 2^-k2 */
		format = format24; /* printf format string */
		fformat = fformat24; /* scanf format string */
		mnrflag = 1; /* sets interval for random numbers */
		etrflag = 1;
		printf( "Testing FLOAT precision.\n" );
		break;

	case 1:
#ifdef DEC
		SPREC = 17;
		mprec = 56;
		m = 17;
		n = 27;
		k2 = -125;
		format = format53;
		fformat = fformat53;
		mnrflag = 2;
		etrflag = 1;
		printf( "Testing DEC DOUBLE precision.\n" );
		break;
#else
		SPREC = 16;
		mprec = 53;
		m = 17;
		n = 27;
		k2 = -1021;
		format = format53;
		fformat = fformat53;
		mnrflag = 2;
		etrflag = 2;
		printf( "Testing DOUBLE precision.\n" );
		break;
#endif
	case 2:
#if LDOUBLE
		SPREC = 20;
		mprec = 64;
		m = 20;
		n = 34;
		k2 = -16382;
		format = format64;
		fformat = fformat64;
		mnrflag = 3;
		etrflag = 3;
		printf( "Testing LONG DOUBLE precision.\n" );
		break;
#else
		goto nodenorm;
#endif
	}

	asctoe( Ten, eten );
/* 10^m - 1 */
	d2 = m;
	e53toe( &d2, e1 );
	epow( eten, e1, maxm );
	esub( eone, maxm, maxm );

/* test 1 */
	printf( "1. Checking 10^n - 1 for n = %d to %d.\n", -m, m );
	emov( eone, q5 );
	for( count=0; count<=m; count++ )
		{
		esub( eone, q5, fullp );
		chkit( 1 );
		ediv( q5, eone, q2 );
		esub( eone, q2, fullp );
		chkit( 1 );
		emul( eten, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end1;
			}
		}
end1:
	printerr();


/* test 2 */
	printf( "2. Checking powers of 10 from 10^-%d to 10^%d.\n", n, n );
	emov( eone, q5 );
	for( count=0; count<=n; count++ )
		{
		emov( q5, fullp );
		chkit( 2 );
		ediv( q5, eone, fullp );
		chkit( 2 );
		emul( eten, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end2;
			}
		}
end2:
	printerr();

/* test 3 */
	printf( "3. Checking (10^%d-1)*10^n from n = -%d to %d.\n", m, n, n );
	emov( eone, q5 );
	for( count= -n; count<=n; count++ )
		{
		emul( maxm, q5, fullp );
		chkit( 3 );
		emov( q5, fullp );
		ediv( fullp, eone, fullp );
		emul( maxm, fullp, fullp );
		chkit( 3 );
		emul( eten, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end3;
			}
		}
end3:
	printerr();



/* test 4 */
	printf( "4. Checking powers of 2 from 2^-24 to 2^+56.\n" );
	d1 = -24.0;
	e53toe( &d1, q1 );
	epow( etwo, q1, q5 );

	for( count = -24; count <= 56; count++ )
		{
		emov( q5, fullp );
		chkit( 4 );
		emul( etwo, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end4;
			}
		}
end4:
	printerr();


/* test 5 */
	printf( "5. Checking 2^n - 1 for n = 0 to %d.\n", mprec );
	emov( eone, q5 );
	for( count=0; count<=mprec; count++ )
		{
		esub( eone, q5, fullp );
		chkit( 5 );
		emul( etwo, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end5;
			}
		}
end5:
	printerr();

/* test 6 */
	printf( "6. Checking 2^n + 1 for n = 0 to %d.\n", mprec );
	emov( eone, q5 );
	for( count=0; count<=mprec; count++ )
		{
		eadd( eone, q5, fullp );
		chkit( 6 );
		emul( etwo, q5, q5 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end6;
			}
		}
end6:
	printerr();

/* test 7 */
	printf(
	 "7. Checking %d values M * 10^N with random integer M and N,\n",
	 NRAND );
	printf("  1 <= M <= 10^%d - 1  and  -%d <= N <= +%d.\n", m, n, n );
	for( i=0; i<NRAND; i++ )
		{
		mnrand( fullp );
		chkit( 7 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end7;
			}
		}
end7:
	printerr();

/* test 8 */
	printf("8. Checking critical rounding cases.\n" );
	for( i=0; i<20; i++ )
		{
		mnrand( fullp );
		eabs( fullp );
		if( ecmp( fullp, eone ) < 0 )
			ediv( fullp, eone, fullp );
		efloor( fullp, fullp );
		eadd( ehalf, fullp, fullp );
		chkit( 8 );
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto end8;
			}
		}
end8:
	printerr();



/* test 9 */
	printf("9. Testing on %d random non-denormal values.\n", NRAND );
	for( i=0; i<NRAND; i++ )
		{
		etrand( fullp );
		chkit( 9 );
		}
	printerr();
	shownoncrit();

/* test 10 */
	printf(
	"Do you want to check denormal numbers in this precision ? (y/n) " );
	gets( str0 );
	if( str0[0] != 'y' )
		goto nodenorm;

	printf( "10. Checking denormal numbers.\n" );

/* Form 2^-starting power */
	d1 = k2;
	e53toe( &d1, q1 );
	epow( etwo, q1, e1 );

/* Find 2^-mprec less than starting power */
	d1 = -mprec + 4;
	e53toe( &d1, q1 );
	epow( etwo, q1, e3 );
	emul( e1, e3, e3 );
	emov( e3, e2 );
	ediv( etwo, e2, e2 );

	while( ecmp(e1,e2) != 0 )
		{
		eadd( e1, e2, fullp );
		switch( mprec )
			{
#if LDOUBLE
			case 64:
			etoe64( e1, &sprec64 );
			e64toe( &sprec64, q1 );
			etoe64( fullp, &prec64 );
			e64toe( &prec64, q2 );
			break;
#endif
#ifdef DEC
			case 56:
#endif
			case 53:
			etoe53( e1, &sprec53 );
			e53toe( &sprec53, q1 );
			etoe53( fullp, &prec53 );
			e53toe( &prec53, q2 );
			break;

			case 24:
			etoe24( e1, &sprec24 );
			e24toe( &sprec24, q1 );
			etoe24( fullp, &prec24 );
			e24toe( &prec24, q2 );
			break;
			}
		if( ecmp( q2, ezero ) == 0 )
			goto maxden;
		chkit(10);
		if( ecmp(q1,q2) == 0 )
			{
			ediv( etwo, e1, e1 );
			emov( e3, e2 );
			}
		if( errtot >= MAXERR )
			{
			printf( "%s", toomany );
			goto maxden;
			}
		ediv( etwo, e2, e2 );
		}
maxden:
	printerr();
nodenorm:
	printf( "\n" );
	} /* loop on precision */
printf( "End of test.\n" );
}

#if CHKINTERNAL
long double xprec64;
double xprec53;
float xprec24;

/* Check binary -> printf -> scanf -> binary identity
 * of internal routines
 */
void chkinternal( ref, tst, string )
unsigned short ref[], tst[];
char *string;
{

if( ecmp(ref,tst) != 0 )
	{
	printf( "internal identity compare error!\n" );
	chkid( ref, tst, string );
	}
}
#endif


/* Check binary -> printf -> scanf -> binary identity
 */
void chkid( print, scan, string )
unsigned short print[], scan[];
char *string;
{

⌨️ 快捷键说明

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