home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  3.6 KB  |  195 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/log.c,v 1.3 90/09/04 17:40:29 toy Exp $
  3.  * NAME
  4.  *    log - logarithm
  5.  *
  6.  * SYNOPSIS
  7.  *    log [-H] [-P prec] [-c cols] [-b base] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the logarithm of the elements of
  11.  *    the specified vector(s).
  12.  *
  13.  * HISTORY
  14.  * $Log:    log.c,v $
  15.  * Revision 1.3  90/09/04  17:40:29  toy
  16.  * Set default precision from the environment variable STAT_PREC.
  17.  * Use print_help_strings to print out help.
  18.  *
  19.  * Revision 1.2  90/09/01  17:01:56  toy
  20.  * Use message function to print out messages.
  21.  *
  22.  * Revision 1.1  90/09/01  14:56:43  toy
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <string.h>
  30.  
  31. #if    defined(__STDC__) || defined(__GNUC__)
  32. #include <stddef.h>
  33. #include <stdlib.h>
  34. #endif
  35.  
  36. #include "gps.h"
  37.  
  38. #define    DEF_PROGNAME    "log"
  39.  
  40. static const char RCSID[] = "@(#) $Id: log.c,v 1.3 90/09/04 17:40:29 toy Exp $";
  41.  
  42. extern int getopt ();
  43. extern int optind;
  44. extern char *optarg;
  45.  
  46. const char *progname;
  47. double log_base;        /* Natural log of the base of the log     */
  48. double (*function) PARMS ((double));
  49.  
  50. #define    OPT_STRING    "HP:c:b:"
  51.  
  52. void
  53. help ()
  54. {
  55.   static const char *help_strings[] =
  56.   {
  57.     "\t-H\tThis help\n",
  58.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  59.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  60.     "\t\tuse default.\n",
  61.     "\t-c c\tSet number of output elements per line to n\n",
  62.     "\t-b b\tSet base of the logarithm to b\n",
  63.     "Compute the base b log of the elements of the given vectors\n",
  64.     NULL
  65.   };
  66.   (void) fprintf (stderr, "%s\n", RCSID);
  67.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [-b base] [vector ...]\n", progname);
  68.   print_help_strings (help_strings);
  69. }
  70.  
  71.  
  72. #if    defined(__STDC__) || defined(__GNUC__)
  73. double
  74. Log (double x)
  75. #else
  76. double
  77. Log (x)
  78.      double x;
  79. #endif
  80. {
  81.   return log (x) / log_base;
  82. }
  83.  
  84. #if    defined(__STDC__) || defined(__GNUC__)
  85. void
  86. do_func (FILE * fp)
  87. #else
  88. void
  89. do_func (fp)
  90.      FILE *fp;
  91. #endif
  92. {
  93.   int rc;
  94.   double x;
  95.  
  96.   do
  97.     {
  98.       rc = get_number (fp, &x);
  99.       if (rc == 1)
  100.     {
  101.       print_number (stdout, (*function) (x));
  102.     }
  103.       else if (rc == 0)
  104.     {
  105.       message ("Error reading number\n");
  106.     }
  107.   } while (rc == 1);
  108. }
  109.  
  110.  
  111. int
  112. main (argc, argv)
  113.      int argc;
  114.      char *argv[];
  115. {
  116.   int option;
  117.   int use_natural;
  118.   double base;
  119.  
  120.   progname = get_my_name (argv[0], DEF_PROGNAME);
  121.   set_def_prec ();
  122.  
  123.   /*
  124.    * Read options
  125.    */
  126.  
  127.   use_natural = TRUE;
  128.  
  129.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  130.     {
  131.       switch (option)
  132.     {
  133.     case 'H':        /* Help                 */
  134.       help ();
  135.       exit (0);
  136.       break;
  137.     case 'P':        /* Precision             */
  138.       (void) set_precision (atoi (optarg));
  139.       break;
  140.     case 'c':        /* Number of columns         */
  141.       (void) set_max_columns (atoi (optarg));
  142.       break;
  143.     case 'b':        /* Base of the logs         */
  144.       base = atof (optarg);
  145.       use_natural = FALSE;
  146.       if ((base <= 0) || (base = 1))
  147.         {
  148.           message ("invalid base specified: `%s'\n", optarg);
  149.           exit (1);
  150.         }
  151.       break;
  152.     default:
  153.       break;
  154.     }            /* endswitch     */
  155.     }                /* endwhile     */
  156.  
  157.   if (use_natural)
  158.     {
  159.       function = log;
  160.     }
  161.   else
  162.     {
  163.       log_base = log (base);
  164.       function = Log;
  165.     }
  166.  
  167.   /*
  168.    * If no files are listed, use stdin.  Otherwise use
  169.    * the given files
  170.    */
  171.  
  172.   if (optind == argc)
  173.     {
  174.       do_func (stdin);
  175.     }
  176.   else
  177.     {
  178.       while (optind < argc)
  179.     {
  180.       if (freopen (argv[optind], "r", stdin) == NULL)
  181.         {
  182.           message ("Cannot open `%s'\n", argv[optind]);
  183.         }
  184.       else
  185.         {
  186.           do_func (stdin);
  187.         }
  188.       optind++;
  189.     }            /* endwhile     */
  190.     }                /* endif     */
  191.  
  192.   end_column (stdout);
  193.   return 0;
  194. }
  195.