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

  1. /*
  2. HEADER:         ;
  3. TITLE:          BASIC cvd() in C
  4. VERSION:        1.0;
  5.  
  6. DESCRIPTION:    perform BASIC cvd() function on packed double precision
  7.                 DATA to unpack the data into a double value
  8.  
  9. KEYWORDS:       unpack, BASIC,cvd, double precision;
  10. SYSTEM:         Xenix 3.4b, MSDOS;
  11. FILENAME:       ncvd.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:       ;
  19. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  20.                 Thanks to Mike Thompson of ACT Consulting of Canton, Ohio
  21.                 for helping me write the origninal version several years
  22.                 ago.
  23. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  24. */
  25.  
  26. #include "vernmath.h"
  27.  
  28. #define WIDTH 8
  29.  
  30. #define XTYPE double
  31. #define RETURN_CAST (XTYPE)
  32.  
  33. #define KTYPE double
  34.  
  35. XTYPE ncvd(string)
  36. char *string;
  37. {
  38.     int i,s,e;
  39.     XTYPE x = (XTYPE) 1;
  40.     unsigned char c[WIDTH];
  41.     static KTYPE k[WIDTH] = {
  42.         (KTYPE) 2.7755575615628914e-17,
  43.         (KTYPE) 7.1054273576010019e-15,
  44.         (KTYPE) 1.8189894035458565e-12,
  45.         (KTYPE) 4.6566128730773926e-10,
  46.         (KTYPE) 1.1920928955078125e-07,
  47.         (KTYPE) 3.0517578125000000e-05,
  48.         (KTYPE) 7.8125000000000000e-03,
  49.     };
  50.  
  51. /* convert chars to unsigned chars */
  52.         for (i=0;i<=WIDTH-1;i++) {
  53.         c[i] = (unsigned char) string[i];
  54.     }
  55. /* last char in string determines power to raise final total by */
  56.     e=c[WIDTH-1]-129;
  57.  
  58. /* determine sign */
  59. /* next to last char determines sign */
  60.     if (c[WIDTH-2] < (unsigned char) 128) {
  61.         s=1;  /* if it is positive, sign is 1 */
  62.     }
  63.     else {
  64.         s = (-1);
  65.         /* make it think its positive */
  66.         c[WIDTH-2] -= (unsigned char) 128;
  67.     }
  68.  
  69. /* total first 3 chars multiplied by there respective factors */
  70.     for (i=0;i<WIDTH-1;i++) {
  71.         x += (XTYPE) ((KTYPE) c[i] * k[i]);
  72.     }
  73.  
  74. /* raise the total by the power */
  75.     x *= (XTYPE) pow(2.,(double)e)*(XTYPE)s;
  76.  
  77.     return( RETURN_CAST x);
  78. }
  79.