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

  1. /*
  2.    Listing 24.13  Iee2real()
  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 Iee2real(cIee)
  14. LOCAL retval:=0,k,x1:="",sign,exponent,fraction
  15. if cIee <> repl(chr(0),8)       // Are all bits set to zero?
  16.    for k=len(cIee) to 1 step -1
  17.       x1 += Dec2bin(asc(Substr(cIee,k,1)))
  18.    next
  19.    sign     := (Substr(x1,1,1)="1")
  20.    exponent := Bin2dec(Substr(x1,2,11)) - 1022
  21.    fraction := Bin2dec("1"+Substr(x1,13),.T.)
  22.    retval   := if(sign,-1,1)* (fraction * (2**exponent))
  23. endif
  24. return retval
  25.  
  26. function Bin2dec(_string)
  27. LOCAL retval :=0 ,k
  28. for k=1 to len(_string)
  29.     retval := If(Substr(_string,k,1)="0",0,1)+retval+retval
  30. next
  31. return retval
  32.  
  33. function Dec2bin(_number)
  34. LOCAL tmp := _number,retval:="",remd,quot
  35. while .T.
  36.    quot  := int(tmp/2)
  37.    remd  := abs(tmp)-2*abs(quot)
  38.    retval:= Substr("01",remd+1,1)+retval
  39.    if quot= 0
  40.       exit
  41.    endif
  42.    tmp := quot
  43. enddo
  44. while len(retval)<16
  45.    retval:="0"+retval
  46. enddo
  47. return retval
  48.  
  49. // end of file CHP2413.PRG
  50.