home *** CD-ROM | disk | FTP | other *** search
- COMMENT:
- Solution to problem 4.23 Presented in Lesson 4 Part 14
-
- Here is the preamble to the problem.
-
- Comment on the the file DMULDIV.SEQ contained in the file SMITH.ZIP
- which is unpacked by INSTALL into the C:\FPC\TOOLS subdirectory.
-
- This file provides some some useful double number arithmetic operators.
-
- For multiplying two unsigned 32-bit double numbers and yielding an
- unsigned 64-bit quad presion result:
-
- UMD* ( ud1 ud2 -- uq ) \ uq is the unsigned 64 bit product.
-
- For multiplying two signed 32-bit numbers and yielding a 32-bit signed
- product:
-
- D* ( d1 d2 -- dn ) \ dn is the signed 32 bit product of d1 and d2.
-
- Also provided are some mixed precision division operators:
-
- For dividing and unsigned 64-bit dividend by an unsigned 32-bit divisor
- to yield an unsigned 32-bit quotient and unsigned 32-bit remainder:
-
- UMD/MOD ( uqdividend uddivisor -- udremainder udquotient )
-
- NOTE: the stack comment for UMD/MOD in the 2.15 version of the file
- DMULDIV.SEQ is wrong! In that file the remainder and quotient
- are reversed. The above stack comment is correct.
-
- NOTE: the stack comment for UMD/MOD in the 2.25 version of DMULDIV.SEQ
- is still wrong!
-
- NOTE: check the stack comment in the 3.5 version.
-
- In the case study we gave the definitions for the unsigned operators
- UD/MOD UD/ and UDMOD.
-
- We also would like to have signed version of these operators that
- conform to signed floored symmetric 32-bit division. In particular we
- need the following:
-
- D/MOD ( ddividend ddivisor -- dremainder dquotient )
- D/ ( ddividend ddivisor -- dquotient )
- DMOD ( ddividend ddivisor -- dremainder )
-
- Problem 4.23
- Write definitions for the above signed division operators. Make sure
- they conform to symmetric floored division rules!
- COMMENT;
-
- FLOAD DMULDIV.SEQ
-
- \ This is Forth 83 floored integer division
- \ for double numbers using SMITH's coded UMD/MOD
- \ Divides two double numbers. All numbers are signed doubles.
- : D/MOD ( dn1 dn2 -- drem dquot )
- 2DUP >R >R \ Save dn2 complete with sign for later.
- 2 PICK >R \ Save sign of dn1 for later.
- DABS >R >R \ Save uddivisor for a moment
- DABS 0 0 R> R> \ uqdividend uddivisor
- UMD/MOD 2SWAP \ udquot udrem
- R@ ?DNEGATE \ apply sign to remainder.
- R> R> R@ SWAP >R XOR 0< \ find sign of quotient.
- IF R> R> D+ 2SWAP \ adjust remainder.
- DNEGATE 1. D- \ adjust quotient.
- ELSE R> R> 2DROP 2SWAP
- THEN ; \ drem dquot
-
- \ Now the other two operators D/ and DMOD are easy!
-
- \ Leave the Forth 83 "floored integer quotient" result for dn1/dn2
- : D/ ( dn1 dn2 -- dquot )
- D/MOD 2SWAP 2DROP ;
-
- \ Leave the Forth 83 "floored integer remainder" result for dn1/dn2
- : DMOD ( dn1 dn2 -- drem )
- D/MOD 2DROP ;
-
-