eigenmath/arccos.cpp

166 lines
1.9 KiB
C++
Raw Permalink Normal View History

2004-03-03 21:24:06 +01:00
#include "stdafx.h"
#include "defs.h"
void
eval_arccos(void)
{
push(cadr(p1));
eval();
2008-06-08 21:18:13 +02:00
arccos();
2004-03-03 21:24:06 +01:00
}
void
arccos(void)
{
int n;
double d;
2008-06-08 21:18:13 +02:00
save();
2004-03-03 21:24:06 +01:00
p1 = pop();
2006-04-19 01:18:30 +02:00
if (car(p1) == symbol(COS)) {
push(cadr(p1));
2008-06-08 21:18:13 +02:00
restore();
2006-04-19 01:18:30 +02:00
return;
}
2005-08-06 22:57:37 +02:00
if (isdouble(p1)) {
2004-03-03 21:24:06 +01:00
errno = 0;
d = acos(p1->u.d);
if (errno)
stop("arccos function argument is not in the interval [-1,1]");
push_double(d);
2008-06-08 21:18:13 +02:00
restore();
2007-04-25 01:53:40 +02:00
return;
}
// if p1 == 1/sqrt(2) then return 1/4*pi (45 degrees)
2007-04-25 04:14:14 +02:00
if (isoneoversqrttwo(p1)) {
2007-04-25 01:53:40 +02:00
push_rational(1, 4);
push_symbol(PI);
multiply();
2008-06-08 21:18:13 +02:00
restore();
2007-04-25 01:53:40 +02:00
return;
}
// if p1 == -1/sqrt(2) then return 3/4*pi (135 degrees)
2007-04-25 04:14:14 +02:00
if (isminusoneoversqrttwo(p1)) {
2007-04-25 01:53:40 +02:00
push_rational(3, 4);
push_symbol(PI);
multiply();
2008-06-08 21:18:13 +02:00
restore();
2004-03-03 21:24:06 +01:00
return;
}
2005-08-06 22:57:37 +02:00
if (!isrational(p1)) {
2004-03-03 21:24:06 +01:00
push_symbol(ARCCOS);
push(p1);
list(2);
2008-06-08 21:18:13 +02:00
restore();
2004-03-03 21:24:06 +01:00
return;
}
push(p1);
push_integer(2);
multiply();
n = pop_integer();
switch (n) {
case -2:
push_symbol(PI);
break;
case -1:
push_rational(2, 3);
push_symbol(PI);
multiply();
break;
case 0:
push_rational(1, 2);
push_symbol(PI);
multiply();
break;
case 1:
push_rational(1, 3);
push_symbol(PI);
multiply();
break;
case 2:
2004-06-25 22:45:15 +02:00
push(zero);
2004-03-03 21:24:06 +01:00
break;
default:
push_symbol(ARCCOS);
push(p1);
list(2);
break;
}
2008-06-08 21:18:13 +02:00
restore();
2004-03-03 21:24:06 +01:00
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-03-03 21:24:06 +01:00
static char *s[] = {
"arccos(1)",
"0",
"arccos(1/2)",
"1/3*pi",
"arccos(0)",
"1/2*pi",
"arccos(-1/2)",
"2/3*pi",
"arccos(-1)",
"pi",
"arccos(cos(0))",
"0",
"arccos(cos(1/3*pi))",
"1/3*pi",
"arccos(cos(1/2*pi))",
"1/2*pi",
"arccos(cos(2/3*pi))",
"2/3*pi",
"arccos(cos(pi))",
"pi",
2006-04-19 01:18:30 +02:00
"arccos(cos(x))",
"x",
2007-04-25 01:53:40 +02:00
"arccos(1/sqrt(2))",
"1/4*pi",
"arccos(-1/sqrt(2))",
"3/4*pi",
"arccos(cos(1/4*pi))",
"1/4*pi",
"arccos(cos(3/4*pi))",
"3/4*pi",
2004-03-03 21:24:06 +01:00
};
void
test_arccos(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif