UNPKG

1.33 kBtext/coffeescriptView Raw
1# Natural logarithm.
2#
3# Note that we use the mathematics / Javascript / Mathematica
4# convention that "log" is indeed the natural logarithm.
5#
6# In engineering, biology, astronomy, "log" can stand instead
7# for the "common" logarithm i.e. base 10. Also note that Google
8# calculations use log for the common logarithm.
9
10
11Eval_log = ->
12 push(cadr(p1))
13 Eval()
14 logarithm()
15
16logarithm = ->
17 save()
18 yylog()
19 restore()
20
21yylog = ->
22 d = 0.0
23
24 p1 = pop()
25
26 if (p1 == symbol(E))
27 push_integer(1)
28 return
29
30 if (equaln(p1, 1))
31 push_integer(0)
32 return
33
34 if (isnegativenumber(p1))
35 push(p1)
36 negate()
37 logarithm()
38 push(imaginaryunit)
39 if evaluatingAsFloats
40 push_double(Math.PI)
41 else
42 push_symbol(PI)
43 multiply()
44 add()
45 return
46
47 if (isdouble(p1))
48 d = Math.log(p1.d)
49 push_double(d)
50 return
51
52 # rational number and not an integer?
53
54 if (isfraction(p1))
55 push(p1)
56 numerator()
57 logarithm()
58 push(p1)
59 denominator()
60 logarithm()
61 subtract()
62 return
63
64 # log(a ^ b) --> b log(a)
65
66 if (car(p1) == symbol(POWER))
67 push(caddr(p1))
68 push(cadr(p1))
69 logarithm()
70 multiply()
71 return
72
73 # log(a * b) --> log(a) + log(b)
74
75 if (car(p1) == symbol(MULTIPLY))
76 push_integer(0)
77 p1 = cdr(p1)
78 while (iscons(p1))
79 push(car(p1))
80 logarithm()
81 add()
82 p1 = cdr(p1)
83 return
84
85 push_symbol(LOG)
86 push(p1)
87 list(2)
88