home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / p4_23jb.seq < prev    next >
Encoding:
Text File  |  1990-04-17  |  2.9 KB  |  81 lines

  1. COMMENT:
  2. Solution to problem 4.23 Presented in Lesson 4 Part 14
  3.  
  4. Here is the preamble to the problem.
  5.  
  6. Comment on the the file DMULDIV.SEQ  contained in the file SMITH.ZIP
  7. which is unpacked by INSTALL into the  C:\FPC\TOOLS  subdirectory.
  8.  
  9. This file provides some some useful double number arithmetic operators.
  10.  
  11. For multiplying two unsigned 32-bit double numbers and yielding an
  12. unsigned 64-bit quad presion result:
  13.  
  14. UMD* ( ud1 ud2 -- uq )  \ uq is the unsigned 64 bit product.
  15.  
  16. For multiplying two signed 32-bit numbers and yielding a 32-bit signed
  17. product:
  18.  
  19. D* ( d1 d2 -- dn ) \ dn is the signed 32 bit product of d1 and d2.
  20.  
  21. Also provided are some mixed precision division operators:
  22.  
  23. For dividing and unsigned 64-bit dividend by an unsigned 32-bit divisor
  24. to yield an unsigned 32-bit quotient and unsigned 32-bit remainder:
  25.  
  26. UMD/MOD ( uqdividend uddivisor -- udremainder udquotient )
  27.  
  28. NOTE: the stack comment for UMD/MOD in the 2.15 version of the file
  29.       DMULDIV.SEQ is wrong! In that file  the remainder and quotient
  30.       are reversed. The above stack comment is correct.
  31.  
  32. NOTE: the stack comment for UMD/MOD in the 2.25 version of DMULDIV.SEQ
  33.       is still wrong!
  34.  
  35. NOTE: check the stack comment in the 3.5 version.
  36.  
  37. In the case study we gave the definitions for the unsigned operators
  38. UD/MOD  UD/ and UDMOD.
  39.  
  40. We also would like to have signed version of these operators that
  41. conform to signed floored symmetric 32-bit division.  In particular we
  42. need the following:
  43.  
  44. D/MOD ( ddividend ddivisor -- dremainder dquotient )
  45. D/    ( ddividend ddivisor -- dquotient )
  46. DMOD  ( ddividend ddivisor -- dremainder )
  47.  
  48. Problem 4.23
  49. Write definitions for the above signed division operators.  Make sure
  50. they conform to symmetric floored division rules!
  51. COMMENT;
  52.  
  53. FLOAD  DMULDIV.SEQ
  54.  
  55. \ This is Forth 83 floored integer division
  56. \ for double numbers using SMITH's coded UMD/MOD
  57. \ Divides two double numbers.  All numbers are signed doubles.
  58. : D/MOD  ( dn1 dn2 -- drem dquot )
  59.         2DUP >R >R              \ Save dn2 complete with sign for later.
  60.         2 PICK  >R              \ Save sign of dn1 for later.
  61.         DABS >R >R              \ Save uddivisor for a moment
  62.         DABS 0 0 R> R>          \ uqdividend uddivisor
  63.         UMD/MOD  2SWAP          \ udquot udrem
  64.         R@ ?DNEGATE             \ apply sign to remainder.
  65.         R> R> R@ SWAP >R XOR 0< \ find sign of quotient.
  66.         IF   R> R> D+   2SWAP   \ adjust remainder.
  67.              DNEGATE 1. D-      \ adjust quotient.
  68.         ELSE R> R> 2DROP 2SWAP
  69.         THEN  ;                 \ drem dquot
  70.  
  71. \ Now the other two operators D/ and DMOD are easy!
  72.  
  73. \ Leave the Forth 83 "floored integer quotient"  result for dn1/dn2
  74. : D/    ( dn1 dn2 -- dquot )
  75.         D/MOD 2SWAP 2DROP ;
  76.  
  77. \ Leave the Forth 83 "floored integer remainder" result for dn1/dn2
  78. : DMOD  ( dn1 dn2 -- drem )
  79.         D/MOD 2DROP ;
  80.  
  81.