Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / log.cpp
blob3d2bc708df44614f1cba5159a44c35e7fc70123a
1 // natural logarithm
3 #include "stdafx.h"
4 #include "defs.h"
6 void
7 eval_log(void)
9 push(cadr(p1));
10 eval();
11 logarithm();
14 void
15 logarithm(void)
17 save();
18 yylog();
19 restore();
22 void
23 yylog(void)
25 double d;
27 p1 = pop();
29 if (p1 == symbol(E)) {
30 push_integer(1);
31 return;
34 if (equaln(p1, 1)) {
35 push_integer(0);
36 return;
39 if (isnegativenumber(p1)) {
40 push(p1);
41 negate();
42 logarithm();
43 push(imaginaryunit);
44 push_symbol(PI);
45 multiply();
46 add();
47 return;
50 if (isdouble(p1)) {
51 d = log(p1->u.d);
52 push_double(d);
53 return;
56 // rational number and not an integer?
58 if (isfraction(p1)) {
59 push(p1);
60 numerator();
61 logarithm();
62 push(p1);
63 denominator();
64 logarithm();
65 subtract();
66 return;
69 // log(a ^ b) --> b log(a)
71 if (car(p1) == symbol(POWER)) {
72 push(caddr(p1));
73 push(cadr(p1));
74 logarithm();
75 multiply();
76 return;
79 // log(a * b) --> log(a) + log(b)
81 if (car(p1) == symbol(MULTIPLY)) {
82 push_integer(0);
83 p1 = cdr(p1);
84 while (iscons(p1)) {
85 push(car(p1));
86 logarithm();
87 add();
88 p1 = cdr(p1);
90 return;
93 push_symbol(LOG);
94 push(p1);
95 list(2);