File: math.fth

package info (click to toggle)
pforth 21-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 816 kB
  • ctags: 873
  • sloc: ansic: 5,050; makefile: 104
file content (89 lines) | stat: -rw-r--r-- 1,856 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
\ @(#) math.fth 98/01/26 1.2
\ Extended Math routines
\ FM/MOD SM/REM
\
\ Author: Phil Burk
\ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
\
\ The pForth software code is dedicated to the public domain,
\ and any third party may reproduce, distribute and modify
\ the pForth software code or any derivative works thereof
\ without any compensation or license.  The pForth software
\ code is provided on an "as is" basis without any warranty
\ of any kind, including, without limitation, the implied
\ warranties of merchantability and fitness for a particular
\ purpose and their equivalents under the laws of any jurisdiction.

anew task-math.fth
decimal

: FM/MOD { dl dh nn | dlp dhp nnp rem quo -- rem quo , floored }
	dl dh dabs -> dhp -> dlp
	nn abs -> nnp
	dlp dhp nnp um/mod -> quo -> rem
	dh 0<  
	IF  \ negative dividend
		nn 0< 
		IF   \ negative divisor
			rem negate -> rem
		ELSE  \ positive divisor
			rem 0=
			IF
				quo negate -> quo
			ELSE
				quo 1+ negate -> quo
				nnp rem - -> rem
			THEN
		THEN
	ELSE  \ positive dividend
		nn 0<  
		IF  \ negative divisor
			rem 0=
			IF
				quo negate -> quo
			ELSE
				nnp rem - negate -> rem
				quo 1+ negate -> quo
			THEN
		THEN
	THEN
	rem quo
;

: SM/REM { dl dh nn | dlp dhp nnp rem quo -- rem quo , symmetric }
	dl dh dabs -> dhp -> dlp
	nn abs -> nnp
	dlp dhp nnp um/mod -> quo -> rem
	dh 0<  
	IF  \ negative dividend
		rem negate -> rem
		nn 0> 
		IF   \ positive divisor
			quo negate -> quo
		THEN
	ELSE  \ positive dividend
		nn 0<  
		IF  \ negative divisor
			quo negate -> quo
		THEN
	THEN
	rem quo
;


: /MOD ( a b -- rem quo )
	>r s>d r> sm/rem
;

: MOD ( a b -- rem )
	/mod drop
;

: */MOD ( a b c -- rem a*b/c , use double precision intermediate value )
	>r m*
	r> sm/rem
;
: */ ( a b c -- a*b/c , use double precision intermediate value )
	*/mod
	nip
;