2005-06-30 03:25:26 +02:00
|
|
|
// natural logarithm
|
2004-07-16 02:36:18 +02:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
2005-06-30 03:25:26 +02:00
|
|
|
static void ylog(void);
|
2004-07-16 02:36:18 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
eval_log(void)
|
|
|
|
{
|
|
|
|
push(cadr(p1));
|
|
|
|
eval();
|
|
|
|
logarithm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
logarithm(void)
|
|
|
|
{
|
|
|
|
save();
|
2005-06-30 03:25:26 +02:00
|
|
|
ylog();
|
2004-07-16 02:36:18 +02:00
|
|
|
restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-06-30 03:25:26 +02:00
|
|
|
ylog(void)
|
2004-07-16 02:36:18 +02:00
|
|
|
{
|
|
|
|
double d;
|
|
|
|
|
|
|
|
p1 = pop();
|
|
|
|
|
2005-08-06 22:57:37 +02:00
|
|
|
if (isdouble(p1)) {
|
2004-07-16 02:36:18 +02:00
|
|
|
d = p1->u.d;
|
|
|
|
if (d < 0.0) {
|
|
|
|
d = log(-d);
|
|
|
|
push_double(d);
|
|
|
|
push(imaginaryunit);
|
|
|
|
push_symbol(PI);
|
|
|
|
multiply();
|
|
|
|
add();
|
|
|
|
} else {
|
|
|
|
d = log(d);
|
|
|
|
push_double(d);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(p1, one)) {
|
|
|
|
push_integer(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1 == symbol(E)) {
|
|
|
|
push(one);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (car(p1) == symbol(POWER)) {
|
|
|
|
push(caddr(p1));
|
|
|
|
push(cadr(p1));
|
|
|
|
logarithm();
|
|
|
|
multiply();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
push_symbol(LOG);
|
|
|
|
push(p1);
|
|
|
|
list(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *s[] = {
|
|
|
|
|
|
|
|
"log(1)",
|
|
|
|
"0",
|
|
|
|
|
|
|
|
"log(exp(1))",
|
|
|
|
"1",
|
|
|
|
|
|
|
|
"log(exp(x))",
|
|
|
|
"x",
|
|
|
|
|
|
|
|
"exp(log(x))",
|
|
|
|
"x",
|
|
|
|
|
|
|
|
"log(x^2)",
|
|
|
|
"2*log(x)",
|
|
|
|
|
|
|
|
"log(1/x)",
|
|
|
|
"-log(x)",
|
|
|
|
|
|
|
|
"log(a^b)",
|
|
|
|
"b*log(a)",
|
|
|
|
|
|
|
|
"log(2)",
|
|
|
|
"log(2)",
|
|
|
|
|
|
|
|
"log(2.0)",
|
|
|
|
"0.693147",
|
|
|
|
|
|
|
|
"float(log(2))",
|
|
|
|
"0.693147",
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
test_log(void)
|
|
|
|
{
|
|
|
|
test(__FILE__, s, sizeof s / sizeof (char *));
|
|
|
|
}
|