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

  1. /*
  2. HEADER:         ;
  3. TITLE:          BASIC mkd() in C
  4. VERSION:        1.0;
  5.  
  6. DESCRIPTION:    performs BASIC mkd() packing a value into a string 8 chars
  7.                 long
  8.  
  9. KEYWORDS:       ;
  10. SYSTEM:         Xenix 3.4b, MSDOS;
  11. FILENAME:       vmkd.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. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  21. */
  22.  
  23. #include "vernmath.h"
  24.  
  25. #define WIDTH 8
  26.  
  27. char *vmkd(amt)
  28. double amt;
  29. {
  30. /* local int */
  31.     int e;      /* exponent */
  32.     double mantissa = frexp(amt,&e);
  33.  
  34.     int i,sign = 1;
  35.  
  36.     static unsigned char c[WIDTH];
  37.     static double k[ WIDTH -1 ] = {
  38.         2.7755575615628914e-17,
  39.         7.1054273576010019e-15,
  40.         1.8189894035458565e-12,
  41.         4.6566128730773926e-10,
  42.         1.1920928955078125e-07,
  43.         3.0517578125000000e-05,
  44.         7.8125000000000000e-03,
  45.     };
  46.  
  47. /* make mantissa a positive number if necessary */
  48.     if (mantissa < 0.0) {
  49.         sign = -1;
  50.         mantissa *= (double) sign;
  51.     }
  52.  
  53. /* adjust result by multiplying mantissa by 2 until mantissa is greater than 1,
  54.     each time you multiply the mantissa by 2, subtract 1 from
  55.     the power you raise two by, in this way we duplicate the values
  56.     returned by BASIC's mkd() which returns 1.x * 2^e, where as
  57.     frexp() returns .x * 2^e */
  58.  
  59.     while(mantissa < 1.0) {
  60.         mantissa *= 2.0;
  61.         e--;
  62.     }
  63.  
  64.     c[ WIDTH - 1 ] = (unsigned char) (e + 129);
  65.  
  66. /* total first 3 chars multiplied by there respective factors */
  67.     for ( i = WIDTH - 2; i >= 0;i-- ) {
  68.         if (mantissa < k[ i ]) c[ i ] = (unsigned char) 0;
  69.         else {
  70.             c[ i ] = (unsigned char) (mantissa/k[ i ]);
  71.             mantissa -= c[ i ] * k[ i ];
  72.         }
  73.     }
  74.  
  75. /* unless the number is negative, reverse next to last char */
  76.     if (sign != -1) {
  77.         c[ WIDTH - 2 ] -= (unsigned char) 128;
  78.     }
  79.  
  80.     if (amt == 0.0) c[ WIDTH - 1 ] = (unsigned char) 0;
  81.  
  82.  
  83.     return((char *)c);
  84. }
  85.