UNPKG

2.36 kBtext/coffeescriptView Raw
1### arccos =====================================================================
2
3Tags
4----
5scripting, JS, internal, treenode, general concept
6
7Parameters
8----------
9x
10
11General description
12-------------------
13Returns the inverse cosine of x.
14
15###
16
17
18Eval_arccos = ->
19 push(cadr(p1))
20 Eval()
21 arccos()
22
23arccos = ->
24 n = 0
25 d = 0.0
26
27 save()
28
29 p1 = pop()
30
31 if (car(p1) == symbol(COS))
32 push(cadr(p1))
33 restore()
34 return
35
36 if (isdouble(p1))
37 errno = 0
38 d = Math.acos(p1.d)
39 if (errno)
40 stop("arccos function argument is not in the interval [-1,1]")
41 push_double(d)
42 restore()
43 return
44
45 # if p1 == 1/sqrt(2) then return 1/4*pi (45 degrees)
46 # second if catches the other way of saying it, sqrt(2)/2
47
48 if (isoneoversqrttwo(p1)) or
49 (car(p1) == symbol(MULTIPLY) && equalq(car(cdr(p1)), 1,2) and car(car(cdr(cdr(p1)))) == symbol(POWER) && equaln(car(cdr(car(cdr(cdr(p1))))),2) && equalq(car(cdr(cdr(car(cdr(cdr(p1)))))), 1, 2))
50 if evaluatingAsFloats
51 push_double(Math.PI / 4.0)
52 else
53 push_rational(1, 4)
54 push_symbol(PI)
55 multiply()
56 restore()
57 return
58
59 # if p1 == -1/sqrt(2) then return 3/4*pi (135 degrees)
60 # second if catches the other way of saying it, -sqrt(2)/2
61
62 if (isminusoneoversqrttwo(p1)) or
63 (car(p1) == symbol(MULTIPLY) && equalq(car(cdr(p1)), -1,2) and car(car(cdr(cdr(p1)))) == symbol(POWER) && equaln(car(cdr(car(cdr(cdr(p1))))),2) && equalq(car(cdr(cdr(car(cdr(cdr(p1)))))), 1, 2))
64 if evaluatingAsFloats
65 push_double(Math.PI * 3.0 / 4.0)
66 else
67 push_rational(3, 4)
68 push_symbol(PI)
69 multiply()
70 restore()
71 return
72
73 if (!isrational(p1))
74 push_symbol(ARCCOS)
75 push(p1)
76 list(2)
77 restore()
78 return
79
80 push(p1)
81 push_integer(2)
82 multiply()
83 n = pop_integer()
84
85 switch (n)
86
87 when -2
88 if evaluatingAsFloats
89 push_double(Math.PI)
90 else
91 push_symbol(PI)
92
93 when -1
94 if evaluatingAsFloats
95 push_double(Math.PI * 2.0 / 3.0)
96 else
97 push_rational(2, 3)
98 push_symbol(PI)
99 multiply()
100
101 when 0
102 if evaluatingAsFloats
103 push_double(Math.PI / 2.0)
104 else
105 push_rational(1, 2)
106 push_symbol(PI)
107 multiply()
108
109 when 1
110 if evaluatingAsFloats
111 push_double(Math.PI / 3.0)
112 else
113 push_rational(1, 3)
114 push_symbol(PI)
115 multiply()
116
117 when 2
118 if evaluatingAsFloats
119 push_double(0.0)
120 else
121 push(zero)
122
123 else
124 push_symbol(ARCCOS)
125 push(p1)
126 list(2)
127
128 restore()
129
130