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

📄 testtinytrig.c

📁 CD_高级PALM编程
💻 C
字号:
/********************************************************************* FILE:				TinyTrig.c** DESCRIPTION:	A program to calculate various trigonometric functions.** VERSION:		1.0**********************************************************************/#undef COUNTRY		// Because of OS 3.1 & OS 3.5 includes#include	<PalmOS.h>#include 	<PalmCompatibility.h>#include	"TestTinyTrig.h"#include	"TestTinyTrigRsc.h"#include	"Utils.h"#include	"UTilsRsc.h"#include	"MathLib.h"#include	"TinyTrig.h"/*********************************************************************** * FUNCTION:    	TestTinyTrig * * DESCRIPTION: 	Calculate some trig functions. * * RETURNED:    	nothing ***********************************************************************/ static void TestTinyTrig(	void){	Err			error = 0;	UInt		useCount = 0;// Open MathLib as a shared library.		error = OpenSharedLibrary ( MathLibName, MathLibCreator, &MathLibRef );	if ( error == 0 ) {		error = MathLibOpen ( MathLibRef, MathLibVersion );		if ( error != 0 ) 			FrmAlert ( MathLibOpenErrAlert );	}	// Test the trig functions			if ( error == 0 ) {		UInt8		testNum = 1;	// Write results headers		WinDrawChars ( head1, StrLen ( head1 ), TICK1_CNT_LOC, LINE_Y ( 0 ));		WinDrawChars ( head2, StrLen ( head2 ), TICK2_CNT_LOC, LINE_Y ( 0 ));		WinDrawChars ( head3, StrLen ( head3 ), CNT_CNT_LOC, LINE_Y ( 0 ));// Test each library and output the results				TTTestFunc ( testNum++, TT_MIN_VALUE, TT_MAX_VALUE, 1.2, "sqrt", &sqrt, &_sqrt, MULT, TT_ACCURACY );		TTTestFunc ( testNum++, ( -3.0*PI ), (+3.0*PI ), ( PI / 36.0 ), "sin", &sin, &_sin, ADD, TT_ACCURACY );		TTTestFunc ( testNum++, ( -3.0*PI ), (+3.0*PI ), ( PI / 36.0 ), "cos", &cos, &_cos, ADD, TT_ACCURACY );		TTTestFunc ( testNum++, (-HALF_PI + 0.075 ), ( HALF_PI - 0.075 ), ( PI / 240.0 ), "tan", &tan, &_tan, ADD, TT_ACCURACY );		TTTestFunc ( testNum++, -1.0, +1.0, 0.0125, "asin", &asin, &_asin, ADD,  ( TT_TAB_ACCURACY * 10.0 ));		TTTestFunc ( testNum++, -1.0, +1.0, 0.0125, "acos", &acos, &_acos, ADD, ( TT_TAB_ACCURACY * 10.0 ));		TTTestFunc ( testNum++, ( TAN_MIN_VALUE / 2.0 ), ( TAN_MAX_VALUE / 2.0 ), ( TAN_MAX_VALUE * 0.005 ), "atan", &atan, &_atan, ADD, ( TT_TAB_ACCURACY * 20.0 ) );		TTTestFunc ( testNum++, ( - 3.0 * PI ), (+ 3.0 * PI ), ( PI / 36.0 ), "sin2", &sin, &_sin2, ADD, TT_TAB_ACCURACY );		TTTestFunc ( testNum++, ( - 3.0 * PI ), (+ 3.0 * PI ), ( PI / 36.0 ), "cos2", &cos, &_cos2, ADD, TT_TAB_ACCURACY );		TTTestFunc ( testNum++, ( - HALF_PI + 0.075 ), ( HALF_PI - 0.075 ), ( PI / 240.0 ), "tan2", &tan, &_tan2, ADD, TT_TAB_ACCURACY  * 20.0);// Close MathLib				error = MathLibClose ( MathLibRef,   &useCount );		if (( error == 0 ) && ( useCount == 0 )) {			error = CloseSharedLibrary ( MathLibRef );			if ( error != 0 )				FrmAlert ( MathLibCloseErrAlert );		}	}}/*********************************************************************** * FUNCTION:    	DispTestResults * * DESCRIPTION: 	Output the test results for a TinyTrig function test. * * RETURNED:    	nothing ***********************************************************************/  static void DispTestResults ( 	double	speed1,		// ( in ) ticks for Mathlib test 	double	speed2,		// ( in ) ticks for NewMath test 	UInt8		dispRow,	// ( in ) row to display the info 	UInt16		cnt				// ( in ) # of test iterations ) { 		char		tickStr [ 25 ];// DIsplay the first tick count ( Mathlib )	StrIToA (  tickStr, ( long ) speed1 );	WinDrawChars ( tickStr, StrLen ( tickStr ), TICK1_CNT_LOC, LINE_Y ( dispRow ));// Display the second tick count (PAKA)		StrIToA (  tickStr, ( long ) speed2 );	WinDrawChars ( tickStr, StrLen ( tickStr ), TICK2_CNT_LOC, LINE_Y ( dispRow ));	// Display the # of test iterations	StrIToA (  tickStr, ( long ) cnt );	WinDrawChars ( tickStr, StrLen ( tickStr ), CNT_CNT_LOC, LINE_Y ( dispRow ));}/*********************************************************************** * FUNCTION:    	TTTestFunc * * DESCRIPTION: 	Test a TinyTrig function against MathLib. *							A non-fatal error is displayed if a TinyTrig result differs *							by more than the required accuracy. * * RETURNED:    	nothing ***********************************************************************/ static void 		TTTestFunc(	UInt8			lineNum,								// ( in ) order of this test in the full suite	double		testMin,								// ( in ) starting test value	double		testMax,								// ( in ) ending value	double		testIncr,								// ( in ) test sequencing increment	char *			testMsg,								// ( in ) test starting message	 double		(*testFunc1) ( double ),	// ( in ) Mathlib testing function address	 double		(*testFunc2) ( double ),	// ( in ) TinyTrig testing function address	 enum			TEST_TYPE  tType,				// ( in ) add or multiply increment	 double		testAccr								// ( in ) required test accuracy){	double		result1, result2, diff;	UInt16			cnt = 0;	ULong			time1, time2, time3, speed1 = 0, speed2 = 0;		double		x = testMin;	// Draw the starting message	WinDrawChars ( testMsg, StrLen ( testMsg ), LEFT_MARGIN, LINE_Y ( lineNum ));// Calculate function pairs until the max test value reached		while ( x < testMax )	{	// Calculate the two results, bracketed by tick counts		time1		= TimGetTicks ();			result1 	= testFunc1 ( x );		time2		= TimGetTicks ();		result2 	= testFunc2 ( x );		time3 		= TimGetTicks ();		// Check to see if we've exceeded the accuracy threshold		diff = _abs ( result1  -result2 );		ErrNonFatalDisplayIf ( diff > testAccr, "TTTestFunc error" );		// Get 	the cumulative ticks for reporting overall speeds later		speed1	= speed1 + time2 - time1;		speed2	= speed2 + time3 - time2;		// Prep for the next loop increment		if ( tType == ADD )			x	= x + testIncr;		else if ( tType == MULT )			x 	= x * testIncr;		cnt++;	}	// All done, display the final test results	DispTestResults ( speed1, speed2, ( UInt8 ) ( lineNum ), cnt );}#pragma mark ----------------/*********************************************************************** * FUNCTION:    	MainFormInit * * DESCRIPTION: 	This routine initializes the MainForm form. * * RETURNED:    	nothing ***********************************************************************/ static void MainFormInit (	FormPtr /*frmP*/	// ( in ) Pointer to the MainForm form.){// Insert code as appropriate}/*********************************************************************** * FUNCTION:    	MainFormDraw * * DESCRIPTION: 	This routine draws the non-form contents of the main form. * * RETURNED:    	nothing ***********************************************************************/static void MainFormDraw(	FormPtr	frmP		// ( in ) Pointer to the main form.){	if ( frmP != NULL ) 	{	// Insert code as appropriate	}}/*********************************************************************** * FUNCTION:    	MainFormDone * * DESCRIPTION: 	This routine cleans up after the main form. * * RETURNED:    	nothing ***********************************************************************/static void MainFormDone(	FormPtr	frmP		//  ( in) Pointer to the main form){	if ( frmP != NULL )	{	// Insert application-specific code first// Then do this as the very last thing		FrmEraseForm	( frmP );		FrmDeleteForm	( frmP );	}}/*********************************************************************** * FUNCTION:    	MainFormDoMenuCommand * * DESCRIPTION: 	This routine handles menu commands. * * RETURNED:    	event handled? ***********************************************************************/ static Boolean MainFormDoMenuCommand	// ( out ) event handled? (	UInt16 	command		// ( in ) The ID of menu command to do){	Boolean 	handled = false;	FormPtr	frmP;	switch ( command )	{// The Calculate command - do some Trig Calculations.		case MainOptionsTestTinyTrig:					TestTinyTrig ();			handled = true;			break;	// The About Command - show the About box.		case MainOptionsAboutTestTinyTrig:					MenuEraseStatus ( 0 );			frmP = FrmInitForm ( AboutForm );			FrmDoDialog ( frmP );			FrmDeleteForm ( frmP );			handled = true;			break;	}	return handled;}/*********************************************************************** * FUNCTION:    	MainFormHandleEvent * * DESCRIPTION: 	This routine is the event handler for MainForm. * * RETURNED:    	true if the event is handled. ***********************************************************************/ static Boolean MainFormHandleEvent(	EventPtr eventP		// ( in ) Pointer to the event to handle){	Boolean 	handled = false;	FormPtr	frmP = FrmGetActiveForm ();	switch ( eventP->eType ) 	{	// Form events		case frmOpenEvent:				MainFormInit ( frmP );	// ***** WARNING  - falls thru to next case *****					case frmUpdateEvent:			FrmDrawForm ( frmP );			MainFormDraw ( frmP );			handled = true;			break; 								case frmCloseEvent:			MainFormDone ( frmP );			handled = true;			break;// Menu events					case menuEvent:			handled = MainFormDoMenuCommand ( eventP->data.menu.itemID );			break;	}	return handled;}#pragma mark ----------------/*********************************************************************** * FUNCTION:    	AppHandleEvent * * DESCRIPTION: 	Loads a form's resources and set its event handler. * * RETURNED:    	true if the event is handled. ***********************************************************************/ static Boolean AppHandleEvent	// ( out ) event handled? (	EventPtr	eventP	// ( in ) Pointer to the event to handle){	    Boolean 	handled = false;	if ( eventP->eType == frmLoadEvent )	{	// Form load event--initialize the form and make it the active form.		UInt16 	formId = eventP->data.frmLoad.formID;		FormPtr	frmP = FrmInitForm ( formId );		FrmSetActiveForm ( frmP );// Set the event handler for the form.		switch (formId)		{			case MainForm:							FrmSetEventHandler ( frmP, MainFormHandleEvent );				handled = true;				break;//	Insert other cases as needed for other forms.		}		handled = true;	}	return handled;}/*********************************************************************** * FUNCTION:    	AppEventLoop * * DESCRIPTION: 	The main event loop for the application.   * * RETURNED:    	nothing ***********************************************************************/ static void AppEventLoop(	void){	UInt16 		error;	EventType 	event;	do {		EvtGetEvent ( &event, evtWaitForever );		if (! SysHandleEvent ( &event))			if (! MenuHandleEvent ( 0, &event, &error ))				if (! AppHandleEvent ( &event ))					FrmDispatchEvent ( &event );	} while ( event.eType != appStopEvent );}/*********************************************************************** * FUNCTION:     	AppStart * * DESCRIPTION:  	Do whatever is necessary to get started. * * RETURNED:     	Err value or 0 if nothing went wrong ***********************************************************************/ static UInt32 AppStart	// ( out ) error or zero (no error) (	void){	return 0;}/*********************************************************************** * FUNCTION:    	AppStop * * DESCRIPTION: 	Do whatever you need to do, like save the preferences. * * RETURNED:    	nothing ***********************************************************************/static void AppStop(	void){}/*********************************************************************** * FUNCTION:    PilotMain * * DESCRIPTION: The application main entry point. * * RETURNED:    0 if launch & execution are successful, non-zero otherwise. ***********************************************************************/ UInt32 PilotMain		// ( out ) error code or zero(	UInt16		cmd,						//  ( in ) The launch code	MemPtr	/* cmdPBP*/,			// ( in ) Pointer to the launch code structure	UInt16		launchFlags			// ( in ) Extra launch info){	UInt32 error = 0;// Check the ROM version for compatibility.	error = RomVersionCompatible ( MIN_ROM_VERSION, launchFlags);	if ( error == 0 ){// ROM OK, check for the various launch codes as needed.			switch (cmd)		{			case sysAppLaunchCmdNormalLaunch:							error = AppStart ();				if ( error == 0 )				{					FrmGotoForm ( MainForm );					AppEventLoop ();					AppStop ();				}				break;		}	}	return error;}

⌨️ 快捷键说明

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