home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / D2E.PRG < prev    next >
Text File  |  1991-08-16  |  2KB  |  95 lines

  1. /*
  2.  * File......: D2E.PRG
  3.  * Author....: Gary Baren
  4.  * CIS ID....: 75470,1027
  5.  * Date......: $Date:   15 Aug 1991 23:05:02  $
  6.  * Revision..: $Revision:   1.2  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/d2e.prv  $
  8.  *
  9.  * This is an original work by Gary Baren and is hereby placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   E:/nanfor/src/d2e.prv  $
  16.  * 
  17.  *    Rev 1.2   15 Aug 1991 23:05:02   GLENN
  18.  * Forest Belt proofread/edited/cleaned up doc
  19.  * 
  20.  *    Rev 1.1   14 Jun 1991 19:51:24   GLENN
  21.  * Minor edit to file header
  22.  * 
  23.  *    Rev 1.0   09 Jun 1991 00:27:06   GLENN
  24.  * Initial revision.
  25.  *  
  26.  */
  27.  
  28.  
  29. /*  $DOC$
  30.  *  $FUNCNAME$
  31.  *      FT_D2E()
  32.  *  $CATEGORY$
  33.  *      Conversion
  34.  *  $ONELINER$
  35.  *        Convert decimal to scientific notation
  36.  *  $SYNTAX$
  37.  *        FT_D2E( <nDec>, <nPrecision> )  -> <cNumE>
  38.  *  $ARGUMENTS$
  39.  *        <nDec>         Decimal number to convert
  40.  *
  41.  *        <nPrecision>   Number of decimal places in result.
  42.  *                     Defaults to 6 decimal places.
  43.  *  $RETURNS$
  44.  *        <cNumE>        A string representing a number in
  45.  *                     scientific notation
  46.  *  $DESCRIPTION$
  47.  *        Given a decimal number and the desired precision,
  48.  *        a string representing the equivalent in scientific
  49.  *        notation is returned.
  50.  *  $EXAMPLES$
  51.  *        ? FT_D2E( 12.345, 2 )
  52.  *          -> 1.23E1
  53.  *
  54.  *        ? FT_D2E( -12.345, 3 )
  55.  *          -> -1.235E1
  56.  *
  57.  *        ? FT_D2E( 0.00000543, 2 )
  58.  *          -> 5.43E-6
  59.  *  $SEEALSO$
  60.  *     FT_E2D()
  61.  *  $END$
  62.  */
  63.  
  64. #define log10( num )    log( num ) / log( 10 )
  65. #define DEFAULT_PRECISION    6
  66. #command DEFAULT <p> TO <val> => <p> := iif( <p> == NIL, <val>, <p> )
  67.  
  68. #ifdef FT_TEST
  69.   function main( cNum, cPrec )
  70.      DEFAULT cPrec TO str( DEFAULT_PRECISION )
  71.      return qout( ft_d2e( val(cNum), val(cPrec) ) )
  72. #endif
  73.  
  74. function ft_d2e( nDec, nPrecision )
  75.   local nExp, sScn
  76.   DEFAULT nPrecision TO DEFAULT_PRECISION
  77.  
  78.   if nDec == 0
  79.      nExp := 0
  80.   elseif abs( nDec ) < 1
  81.      nExp := int( log10( nDec ) ) - 1
  82.   else
  83.      nExp := int( log10( abs(nDec)+0.00001 ) )   && 0.00001 == kludge
  84.   endif                                                    && for imprecise logs
  85.  
  86.   nDec /= 10 ^ nExp
  87.  
  88.   if round( abs(nDec), nPrecision ) >= 10 
  89.      nDec /= 10
  90.      nExp++
  91.   endif another kludge for stuff like '999999999'
  92.  
  93.   sScn := ltrim( str( nDec, nPrecision + 3, nPrecision ) )
  94.   return( sScn + 'E' + alltrim( str( nExp, 5, 0 ) ) )
  95.