home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / desktop / t / utils / !Clock_C_MakeTable < prev    next >
Encoding:
Text File  |  1992-09-28  |  4.1 KB  |  180 lines

  1. /*
  2.  *
  3.  *       Title                  : Make table
  4.  *       System                 : WinAppLib */
  5. #define  Version                  "1.0"
  6. /*       Copyright              : (c) John Winters
  7.  *       Date                   : 19th January, 1992
  8.  *       Author                 : John H. Winters
  9.  *
  10.  *       Function               : Create a look-up table for calculating
  11.  *                                SINs and COSs.
  12.  *
  13.  *
  14.  *       Modification history.
  15.  *
  16.  *       Version                : (Reflect in header IDENT)
  17.  *       Date                   : 
  18.  *       Author                 : 
  19.  *       Changes                : 
  20.  *
  21.  */
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <math.h>
  26.  
  27. /*
  28.  *==========================================================================
  29.  *
  30.  *  Hash defines.
  31.  *
  32.  *==========================================================================
  33.  */
  34.  
  35. #define PI 3.14159265358979324
  36.  
  37. /*
  38.  *==========================================================================
  39.  *
  40.  *  Forward declarations.
  41.  *
  42.  *==========================================================================
  43.  */
  44.  
  45. static void calc_ratio (
  46.     unsigned int  i,
  47.     FILE         *handle) ;
  48.  
  49. /*
  50.  *==========================================================================
  51.  *
  52.  *  Externally visible routines.
  53.  *
  54.  *==========================================================================
  55.  */
  56.  
  57. int main (
  58.     unsigned int  argc,
  59.     char         *argv [])
  60.  
  61. /*
  62.  *  Function :
  63.  *                  Main code of MakeTable.
  64.  *
  65.  *  Parameters :
  66.  *                  The usual.
  67.  *
  68.  *  Returns :
  69.  *                  EXIT_SUCCESS.
  70.  *
  71.  */
  72.  
  73. {
  74.     FILE         *handle ;
  75.     unsigned int  i ;
  76.  
  77.     printf ("MakeTable version %s.\n", Version) ;
  78.     if (argc == 2)
  79.     {
  80.         handle = fopen (argv [1], "w") ;
  81.         if (handle == NULL)
  82.         {
  83.             printf ("Can't open output file - \"%s\".\n",
  84.                     argv [1]) ;
  85.         }
  86.         else
  87.         {
  88.             fprintf (handle,
  89.                      "static unsigned short int ratios [16][2] = {\n") ;
  90.             for (i = 0; i < 16; i++)
  91.             {
  92.                 calc_ratio (i, handle) ;
  93.                 if (i == 15)
  94.                 {
  95.                     fprintf (handle, "\n") ;
  96.                 }
  97.                 else
  98.                 {
  99.                     fprintf (handle, ",\n") ;
  100.                 }
  101.             }
  102.             fprintf (handle, "} ;\n") ;
  103.             fclose (handle) ;
  104.         }
  105.     }
  106.     else
  107.     {
  108.         printf ("Syntax: MAKETABLE <output file>\n") ;
  109.     }
  110.     return (EXIT_SUCCESS) ;
  111. }
  112.  
  113. /*
  114.  *==========================================================================
  115.  *
  116.  *  Local routines.
  117.  *
  118.  *==========================================================================
  119.  */
  120.  
  121. static void calc_ratio (
  122.     unsigned int  i,
  123.     FILE         *handle)
  124.  
  125. /*
  126.  *  Function :
  127.  *                  Calculate a ratio for a given position.
  128.  *
  129.  *  Parameters :
  130.  *                  i       Value from 0 to 15.
  131.  *                  handle  Handle of file to write to.
  132.  *
  133.  *  Returns :
  134.  *                  None.
  135.  *
  136.  */
  137.  
  138. {
  139.     unsigned int approx_top ;
  140.     unsigned int best_bottom ;
  141.     unsigned int best_top ;
  142.     unsigned int bottom ;
  143.     double       correct_value ;
  144.     double       error ;
  145.     unsigned int limit ;
  146.     double       new_error ;
  147.     unsigned int top ;
  148.  
  149.     correct_value = sin ((((double) (i * 6)) * PI) / 180.0) ;
  150.     error       = 10000 ;
  151.     best_bottom = 1 ;
  152.     best_top    = 0 ;
  153.     printf ("Starting %d.\n", i) ;
  154.     for (bottom = 1; bottom < 65536; bottom++)
  155.     {
  156.         approx_top = ((unsigned int) (((double) bottom) * correct_value)) - 10 ;
  157.         limit = approx_top + 20 ;
  158.         if (limit >= 65536)
  159.         {
  160.             limit = 65535 ;
  161.         }
  162.         for (top = approx_top; top < limit; top++)
  163.         {
  164.             new_error = fabs ((((double) top) / ((double) bottom)) - correct_value) ;
  165.             if (new_error < error)
  166.             {
  167.                 error = new_error ;
  168.                 best_bottom = bottom ;
  169.                 best_top    = top ;
  170.             }
  171.         }
  172.     }
  173.     fprintf (handle,
  174.              "    {%d, %d}    /* %f, error %f */",
  175.              best_top,
  176.              best_bottom,
  177.              correct_value,
  178.              error) ;
  179. }
  180.