home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Title : Make table
- * System : WinAppLib */
- #define Version "1.0"
- /* Copyright : (c) John Winters
- * Date : 19th January, 1992
- * Author : John H. Winters
- *
- * Function : Create a look-up table for calculating
- * SINs and COSs.
- *
- *
- * Modification history.
- *
- * Version : (Reflect in header IDENT)
- * Date :
- * Author :
- * Changes :
- *
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
-
- /*
- *==========================================================================
- *
- * Hash defines.
- *
- *==========================================================================
- */
-
- #define PI 3.14159265358979324
-
- /*
- *==========================================================================
- *
- * Forward declarations.
- *
- *==========================================================================
- */
-
- static void calc_ratio (
- unsigned int i,
- FILE *handle) ;
-
- /*
- *==========================================================================
- *
- * Externally visible routines.
- *
- *==========================================================================
- */
-
- int main (
- unsigned int argc,
- char *argv [])
-
- /*
- * Function :
- * Main code of MakeTable.
- *
- * Parameters :
- * The usual.
- *
- * Returns :
- * EXIT_SUCCESS.
- *
- */
-
- {
- FILE *handle ;
- unsigned int i ;
-
- printf ("MakeTable version %s.\n", Version) ;
- if (argc == 2)
- {
- handle = fopen (argv [1], "w") ;
- if (handle == NULL)
- {
- printf ("Can't open output file - \"%s\".\n",
- argv [1]) ;
- }
- else
- {
- fprintf (handle,
- "static unsigned short int ratios [16][2] = {\n") ;
- for (i = 0; i < 16; i++)
- {
- calc_ratio (i, handle) ;
- if (i == 15)
- {
- fprintf (handle, "\n") ;
- }
- else
- {
- fprintf (handle, ",\n") ;
- }
- }
- fprintf (handle, "} ;\n") ;
- fclose (handle) ;
- }
- }
- else
- {
- printf ("Syntax: MAKETABLE <output file>\n") ;
- }
- return (EXIT_SUCCESS) ;
- }
-
- /*
- *==========================================================================
- *
- * Local routines.
- *
- *==========================================================================
- */
-
- static void calc_ratio (
- unsigned int i,
- FILE *handle)
-
- /*
- * Function :
- * Calculate a ratio for a given position.
- *
- * Parameters :
- * i Value from 0 to 15.
- * handle Handle of file to write to.
- *
- * Returns :
- * None.
- *
- */
-
- {
- unsigned int approx_top ;
- unsigned int best_bottom ;
- unsigned int best_top ;
- unsigned int bottom ;
- double correct_value ;
- double error ;
- unsigned int limit ;
- double new_error ;
- unsigned int top ;
-
- correct_value = sin ((((double) (i * 6)) * PI) / 180.0) ;
- error = 10000 ;
- best_bottom = 1 ;
- best_top = 0 ;
- printf ("Starting %d.\n", i) ;
- for (bottom = 1; bottom < 65536; bottom++)
- {
- approx_top = ((unsigned int) (((double) bottom) * correct_value)) - 10 ;
- limit = approx_top + 20 ;
- if (limit >= 65536)
- {
- limit = 65535 ;
- }
- for (top = approx_top; top < limit; top++)
- {
- new_error = fabs ((((double) top) / ((double) bottom)) - correct_value) ;
- if (new_error < error)
- {
- error = new_error ;
- best_bottom = bottom ;
- best_top = top ;
- }
- }
- }
- fprintf (handle,
- " {%d, %d} /* %f, error %f */",
- best_top,
- best_bottom,
- correct_value,
- error) ;
- }
-