-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathlog.coffee
88 lines (71 loc) · 1.3 KB
/
log.coffee
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
# Natural logarithm.
#
# Note that we use the mathematics / Javascript / Mathematica
# convention that "log" is indeed the natural logarithm.
#
# In engineering, biology, astronomy, "log" can stand instead
# for the "common" logarithm i.e. base 10. Also note that Google
# calculations use log for the common logarithm.
Eval_log = ->
push(cadr(p1))
Eval()
logarithm()
logarithm = ->
save()
yylog()
restore()
yylog = ->
d = 0.0
p1 = pop()
if (p1 == symbol(E))
push_integer(1)
return
if (equaln(p1, 1))
push_integer(0)
return
if (isnegativenumber(p1))
push(p1)
negate()
logarithm()
push(imaginaryunit)
if evaluatingAsFloats
push_double(Math.PI)
else
push_symbol(PI)
multiply()
add()
return
if (isdouble(p1))
d = Math.log(p1.d)
push_double(d)
return
# rational number and not an integer?
if (isfraction(p1))
push(p1)
numerator()
logarithm()
push(p1)
denominator()
logarithm()
subtract()
return
# log(a ^ b) --> b log(a)
if (car(p1) == symbol(POWER))
push(caddr(p1))
push(cadr(p1))
logarithm()
multiply()
return
# log(a * b) --> log(a) + log(b)
if (car(p1) == symbol(MULTIPLY))
push_integer(0)
p1 = cdr(p1)
while (iscons(p1))
push(car(p1))
logarithm()
add()
p1 = cdr(p1)
return
push_symbol(LOG)
push(p1)
list(2)