home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 156_01 / float.doc < prev    next >
Text File  |  1985-08-21  |  3KB  |  90 lines

  1.         FLOAT Library Documentation 
  2.  
  3. FLOAT contains the floating point arithmetic routines, and some
  4. functions visible to the user's program.
  5.  
  6.  
  7. GENERAL INFORMATION
  8.  
  9. These routines will execute only on a Z-80. They use the
  10. alternate registers and some of the undocumented instructions
  11. of that processor. They do not conform to the IEEE floating
  12. point standard. The routines were written by Neil Colvin, and
  13. are worth study. They are the best code I have ever seen for
  14. the Z-80.      - Jim Van Zandt 
  15.   
  16.  
  17. FLOATING POINT FORMAT 
  18.  
  19. Each floating point number is 6 bytes long, and consists of a
  20. 40 bit fraction (most significant byte in the highest address)
  21. and an 8 bit exponent. For nonzero numbers, the fraction f has
  22. a value in the range 0.5 <= f < 1.0. Since its most significant
  23. bit would always be 1, it would carry no information and is
  24. replaced by the sign bit (set for a negative number). The
  25. exponent is 80H if the number is in the range 0.5 <= x < 1.0,
  26. and is increased by 1 for each place the binary point of f
  27. should be moved to the right. For example:
  28.  
  29.     Representation             Number     
  30.     00h,00h,00h,00h,00h,80h        0.5     
  31.     00h,00h,00h,00h,80h,80h        -0.5 
  32.     00h,00h,00h,00h,00h,81h        1.0 
  33.     00h,00h,00h,00h,00h,7fh        0.25 
  34.     0fah,33h,0f3h,04h,035h,80h    sqrt(.5) = .707106... 
  35.     38h,0a9h,0d8h,5bh,5eh,7fh    1/log(10) = .43429...
  36.     21h,0a2h,0dah,0fh,49h,81h    pi/2 = 1.5707... 
  37.  
  38.  
  39. ARITHMETIC OPERATIONS
  40.  
  41. Each of the primary operations (DADD, DSUB, DMUL, and DDIV)
  42. takes its first operand from the stack (under the return
  43. address) and the second from the fixed location FA (for
  44. Floating point Accumulator). The result of the operation is
  45. left in FA. For example, we have the following C expression and
  46. its translation into calls to floating point operations:
  47.   
  48.     ;double a,b,c,d; 
  49.     ;main() 
  50.     QMAIN: 
  51.     ;{    a=b+c/d; 
  52.         LD HL,QB    ;get address of 1st operand 
  53.         CALL DLOAD    ;put operand in FA  
  54.         CALL DPUSH    ;move from FA to stack 
  55.         LD HL,QC    ;put 2nd operand...  
  56.         CALL DLOAD 
  57.         CALL DPUSH    ;...on stack 
  58.         LD HL,QD 
  59.         CALL DLOAD    ;put D in FA 
  60.         CALL DDIV    ;find c/d 
  61.         CALL DADD    ;find b+c/d  
  62.         LD HL,QA    ;load destination address  
  63.         CALL DSTORE    ;save result  
  64.     ;} 
  65.         RET 
  66.  
  67.     QA:    DS 6        ;declare storage space  
  68.     QB:    DS 6 
  69.     QC:    DS 6 
  70.     QD:    DS 6 
  71.  
  72.  
  73. FUNCTIONS 
  74.  
  75. Each of these functions return a double: 
  76.  
  77. float(x); double x;    integer to floating point conversion 
  78. fmod(x,y); double x,y;    mod(x,y) 
  79.             if 0 < y then 0 <= mod(x,y) < y and  
  80.             x = n*y + mod(x,y) for some integer n 
  81. fabs(x); double x;    absolute value 
  82. floor(x); double x;    largest integer not greater than 
  83. ceil(x); double x;    smallest integer not less than 
  84. rand();            random number in range 0...1 
  85.   
  86. This function returns an int: 
  87.   
  88. int ifix(x); double x;    floating point to integer 
  89.             (takes floor first) 
  90.