-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.ps
83 lines (72 loc) · 1.63 KB
/
math.ps
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
% PostScript Math Procedures
% by Raymond Luckhurst, scriptit.uk
% constants
/PI 3.141592654 def
/E 2.718281828 def % Euler's number
/I32 1 32 bitshift 0 eq def % true if 32 bit integers
% sign function
/sign {
dup 0 ne { 0 lt { -1 }{ +1 } ifelse }{ cvi } ifelse % integer
} bind def
% absolute maximum value
/absmax {
2 copy abs exch abs gt { exch } if pop
} bind def
% absolute minimum value
/absmin {
2 copy abs exch abs lt { exch } if pop
} bind def
% 32 bit integer addition
//I32 { % 32-bit ints
/iadd32 {
2 copy add type /integertype eq {
add 16#FFFFFFFF and
}{
2 copy xor 16#C0000000 and % to xor with carry bits
2 index 16#3FFFFFFF and 2 index 16#3FFFFFFF and add % 30 bit sum with carry
2 copy and 5 -2 roll and or 1 bitshift 16#80000000 and % top carry bit
or % 30 bit sum with both carry bits
xor % fix top 2 bit sum
} ifelse
} bind def
}{ % 64-bit ints
/iadd32 { add 16#FFFFFFFF and } bind def
} ifelse
% 32 bit integer subtraction
//I32 { % 32-bit ints
/isub32 {
dup neg type /integertype eq {
neg
}{
not 1 iadd32
} ifelse
iadd32
} bind def
}{ % 64-bit ints
/isub32 { sub 16#FFFFFFFF and } bind def
} ifelse
% radians to degrees
% <radians> degrees <degrees>
/degrees {
180 //PI div mul
} bind def
% degrees to radians
% <degrees> radians <radians>
/radians {
//PI 180 div mul
} bind def
% root of difference of two squares
% <x> <y> rtdiffsq <d>
/rtdiffsq {
dup mul exch dup mul sub abs sqrt
} bind def
% arc-cosine
% <adjacent> <hypotenuse> acos <angle>
/acos {
1 index rtdiffsq exch atan
} bind def
% arc-sine
% <opposite> <hypotenuse> asin <angle>
/asin {
1 index rtdiffsq atan
} bind def