home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / ncvs.c < prev    next >
Text File  |  1990-05-20  |  2KB  |  71 lines

  1. /*
  2. HEADER:         ;
  3. TITLE:          BASIC ncvs() function in C
  4. VERSION:        1.4;
  5.  
  6. DESCRIPTION:    performs function of BASIC ncvs(), unpacking a string
  7.                 of 4 chars previously packed by mks()
  8.  
  9. KEYWORDS:       ncvs,single precision,BASIC;
  10. SYSTEM:         Xenix 3.4b, MSDOS;
  11. FILENAME:       ncvs.c
  12. WARNINGS:       compile with -dNO_PROTOTYPE if your system does not
  13.                 support prototyping, with -dFOR_MSDOS if you are compiling
  14.                 for MSDOS with an ANSI standard compiler.
  15.                 Defaults assume compiling with prototypes for
  16.                 Xenix 3.4b on Altos 2086 computer.
  17.  
  18. SEE-ALSO:       vmks();
  19. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  20.                 My thanks to Mike Thompson of ACT Consulting in
  21.                 Canton, Ohio for his help several years ago to write
  22.                 the original version.
  23. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  24. */
  25.  
  26. #include "vernmath.h"
  27.  
  28. #define WIDTH 4
  29. #define XTYPE float
  30. #define KTYPE float
  31.  
  32. double ncvs(string)
  33. char *string;
  34. {
  35.     int i,s,e;
  36.     XTYPE x = (XTYPE) 1;
  37.     unsigned char c[WIDTH];
  38.     static KTYPE k[WIDTH] = {
  39.         (KTYPE) 1.1920928955078125e-07,
  40.         (KTYPE) 3.0517578125000000e-05,
  41.         (KTYPE) 7.8125000000000000e-03,
  42.     };
  43.  
  44. /* convert chars to unsigned chars */
  45.         for (i=0;i<=WIDTH-1;i++) {
  46.         c[i] = (unsigned char) string[i];
  47.     }
  48. /* last char in string determines power to raise final total by */
  49.     e=c[WIDTH-1]-129;
  50.  
  51. /* determine sign */
  52. /* next to last char determines sign */
  53.     if (c[WIDTH-2] < (unsigned char) 128) {
  54.         s=1;  /* if it is positive, sign is 1 */
  55.     }
  56.     else {
  57.         s = (-1);
  58.         /* make it think its positive */
  59.         c[WIDTH-2] -= (unsigned char) 128;
  60.     }
  61.  
  62. /* total first 3 chars multiplied by there respective factors */
  63.     for (i=0;i<WIDTH-1;i++) {
  64.         x += (XTYPE) ((KTYPE) c[i] * k[i]);
  65.     }
  66.  
  67. /* raise the total by the power */
  68.     x *= (XTYPE) pow(2.,(double)e)*(XTYPE)s;
  69.     return(x);
  70. }
  71.