eigenmath/tan.cpp

244 lines
2.8 KiB
C++
Raw Permalink Normal View History

2005-10-16 00:10:22 +02:00
// Tangent function of numerical and symbolic arguments
2004-03-03 21:24:06 +01:00
#include "stdafx.h"
#include "defs.h"
void
eval_tan(void)
{
push(cadr(p1));
eval();
tangent();
}
void
tangent(void)
{
save();
2005-10-15 22:41:33 +02:00
yytangent();
2004-03-03 21:24:06 +01:00
restore();
}
2005-10-15 22:41:33 +02:00
void
yytangent(void)
2004-03-03 21:24:06 +01:00
{
int n;
double d;
p1 = pop();
2006-04-19 01:18:30 +02:00
if (car(p1) == symbol(ARCTAN)) {
push(cadr(p1));
return;
}
2005-08-06 22:57:37 +02:00
if (isdouble(p1)) {
2004-03-03 21:24:06 +01:00
d = tan(p1->u.d);
if (fabs(d) < 1e-10)
d = 0.0;
push_double(d);
return;
}
2005-10-15 22:41:33 +02:00
// tan function is antisymmetric, tan(-x) = -tan(x)
if (isnegative(p1)) {
push(p1);
negate();
tangent();
negate();
return;
}
2004-03-03 21:24:06 +01:00
// multiply by 180/pi
push(p1);
push_integer(180);
multiply();
push_symbol(PI);
divide();
n = pop_integer();
2005-10-15 22:41:33 +02:00
if (n < 0) {
push(symbol(TAN));
2004-03-03 21:24:06 +01:00
push(p1);
list(2);
return;
}
2005-10-15 22:41:33 +02:00
switch (n % 360) {
case 0:
case 180:
2004-03-03 21:24:06 +01:00
push_integer(0);
break;
2005-10-15 22:41:33 +02:00
case 30:
case 210:
push_rational(1, 3);
push_integer(3);
push_rational(1, 2);
power();
multiply();
2004-03-03 21:24:06 +01:00
break;
2005-10-15 22:41:33 +02:00
case 150:
case 330:
push_rational(-1, 3);
push_integer(3);
push_rational(1, 2);
power();
multiply();
2004-03-03 21:24:06 +01:00
break;
case 45:
2005-10-15 22:41:33 +02:00
case 225:
2004-03-03 21:24:06 +01:00
push_integer(1);
break;
case 135:
2005-10-15 22:41:33 +02:00
case 315:
2004-03-03 21:24:06 +01:00
push_integer(-1);
break;
2005-10-15 22:41:33 +02:00
case 60:
case 240:
push_integer(3);
push_rational(1, 2);
power();
2004-03-03 21:24:06 +01:00
break;
2005-10-15 22:41:33 +02:00
case 120:
case 300:
push_integer(3);
push_rational(1, 2);
power();
negate();
2004-03-03 21:24:06 +01:00
break;
default:
2005-10-15 22:41:33 +02:00
push(symbol(TAN));
2004-03-03 21:24:06 +01:00
push(p1);
list(2);
break;
}
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-03-03 21:24:06 +01:00
static char *s[] = {
"tan(x)",
"tan(x)",
2005-10-15 22:41:33 +02:00
"tan(-x)",
"-tan(x)",
"tan(b-a)",
"-tan(a-b)",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
// check against the floating point math library
"f(a,x)=1+tan(float(a/360*2*pi))-float(x)+tan(a/360*2*pi)-x",
"",
"f(0,0)", // 0
"1",
"f(180,0)", // 180
"1",
"f(360,0)", // 360
2004-03-03 21:24:06 +01:00
"1",
2005-10-15 22:41:33 +02:00
"f(-180,0)", // -180
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-360,0)", // -360
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(45,1)", // 45
2004-03-03 21:24:06 +01:00
"1",
2005-10-15 22:41:33 +02:00
"f(135,-1)", // 135
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(225,1)", // 225
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(315,-1)", // 315
2004-03-03 21:24:06 +01:00
"1",
2005-10-15 22:41:33 +02:00
"f(-45,-1)", // -45
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-135,1)", // -135
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-225,-1)", // -225
2004-03-03 21:24:06 +01:00
"1",
2005-10-15 22:41:33 +02:00
"f(-315,1)", // -315
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(30,sqrt(3)/3)", // 30
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(150,-sqrt(3)/3)", // 150
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(210,sqrt(3)/3)", // 210
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(330,-sqrt(3)/3)", // 330
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-30,-sqrt(3)/3)", // -30
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-150,sqrt(3)/3)", // -150
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-210,-sqrt(3)/3)", // -210
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-330,sqrt(3)/3)", // -330
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(60,sqrt(3))", // 60
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(120,-sqrt(3))", // 120
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(240,sqrt(3))", // 240
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(300,-sqrt(3))", // 300
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-60,-sqrt(3))", // -60
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-120,sqrt(3))", // -120
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-240,-sqrt(3))", // -240
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-300,sqrt(3))", // -300
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f=quote(f)",
2004-03-03 21:24:06 +01:00
"",
2006-04-19 01:18:30 +02:00
"tan(arctan(x))",
"x",
2006-05-06 01:57:37 +02:00
2006-09-20 18:09:25 +02:00
// check the default case
"tan(1/12*pi)",
"tan(1/12*pi)",
2004-03-03 21:24:06 +01:00
};
void
test_tan(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif