home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a012 / 1.ddi / CHAP24.EXE / CHP2412.PRG < prev    next >
Encoding:
Text File  |  1991-04-30  |  1.4 KB  |  39 lines

  1. /*
  2.    Listing 24.12  Real2iee()
  3.    Author: Joe Booth
  4.    Excerpted from "Clipper 5: A Developer's Guide"
  5.    Copyright (c) 1991 M&T Books
  6.                       501 Galveston Drive
  7.                       Redwood City, CA 94063-4728
  8.                       (415) 366-3600
  9. */
  10.  
  11. //───── NOTE: must compile with the /N option!
  12.  
  13. function real2iee(nReal)
  14. LOCAL retval:=replicate(chr(0),8), exponent, sign ,j
  15. if nReal<>0                           // If non-zero
  16.    sign    := if(nReal<0,128,0)       // Set the sign bit
  17.    exponent:= 1075             // Offset of 1023 + 52 bits max
  18.    nReal   := abs(nReal)       // We already know the sign
  19.    do while nReal < 2**52      // 2 raised to 52 power
  20.       nReal *= 2               // Multiply number by 2
  21.       exponent--               // Subtract one from exponent
  22.    enddo
  23.    do while nReal > 2*4503599627370496-1
  24.       nReal /= 2
  25.       exponent++
  26.    enddo
  27.    nReal  := int(nReal)          // Number is now an integer
  28.    retval := ''                  // Build the first 48 bits in reverse
  29.    for j  = 1 to 6               // by dividing by 256 and writing
  30.       retval += chr(nReal%256)   // remainder to string
  31.       nReal  := int(nReal/256)
  32.    next
  33.    nReal  -= 16                  // Strip out four high end bits
  34.    retval += chr(nReal+16*(exponent%16)) + chr(sign+int(exponent/16))
  35. endif
  36. return retval
  37.  
  38. // end of file CHP2412.PRG
  39.