Cosine is symmetric, sine is antisymmetric.
This commit is contained in:
parent
e3091ccc0a
commit
6c2ff199a1
4 changed files with 63 additions and 26 deletions
29
cos.cpp
29
cos.cpp
|
@ -43,6 +43,14 @@ cosine_f(void)
|
|||
return;
|
||||
}
|
||||
|
||||
// cosine function is symmetric, cos(-x) = cos(x)
|
||||
|
||||
if (isnegative(p1)) {
|
||||
push(p1);
|
||||
negate();
|
||||
p1 = pop();
|
||||
}
|
||||
|
||||
// multiply by 180/pi
|
||||
|
||||
push(p1);
|
||||
|
@ -53,16 +61,13 @@ cosine_f(void)
|
|||
|
||||
n = pop_integer();
|
||||
|
||||
if (n == (int) 0x80000000) {
|
||||
if (n < 0) {
|
||||
push_symbol(COS);
|
||||
push(p1);
|
||||
list(2);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n < 0)
|
||||
n = -n;
|
||||
|
||||
switch (n % 360) {
|
||||
case 0:
|
||||
push_integer(1);
|
||||
|
@ -105,10 +110,10 @@ static char *s[] = {
|
|||
"-1",
|
||||
|
||||
"cos(-5/6*pi)", // -150 degrees
|
||||
"cos(-5/6*pi)",
|
||||
"cos(5/6*pi)",
|
||||
|
||||
"cos(-3/4*pi)", // -135 degrees
|
||||
"cos(-3/4*pi)",
|
||||
"cos(3/4*pi)",
|
||||
|
||||
"cos(-pi*2/3)", // -120 degrees
|
||||
"-1/2",
|
||||
|
@ -120,10 +125,10 @@ static char *s[] = {
|
|||
"1/2",
|
||||
|
||||
"cos(-1/4*pi)", // -45 degrees
|
||||
"cos(-1/4*pi)",
|
||||
"cos(1/4*pi)",
|
||||
|
||||
"cos(-1/6*pi)", // -30 degrees
|
||||
"cos(-1/6*pi)",
|
||||
"cos(1/6*pi)",
|
||||
|
||||
"cos(0)", // 0 degrees
|
||||
"1",
|
||||
|
@ -160,6 +165,14 @@ static char *s[] = {
|
|||
|
||||
"expomode=0",
|
||||
"",
|
||||
|
||||
// cosine function is symmetric
|
||||
|
||||
"cos(-x)",
|
||||
"cos(x)",
|
||||
|
||||
"cos(b-a)",
|
||||
"cos(a-b)",
|
||||
};
|
||||
|
||||
void
|
||||
|
|
1
defs.h
1
defs.h
|
@ -368,6 +368,7 @@ void gmp_collect(void);
|
|||
void denominator(void);
|
||||
void rationalize(void);
|
||||
int isnegativenumber(U *);
|
||||
int isnegative(U *);
|
||||
void test_gcd(void);
|
||||
void factor(void);
|
||||
void test_factor(void);
|
||||
|
|
11
is.cpp
11
is.cpp
|
@ -202,3 +202,14 @@ iseveninteger(U *p)
|
|||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
isnegative(U *p)
|
||||
{
|
||||
if (car(p) == symbol(ADD) && isnegativeterm(cadr(p)))
|
||||
return 1;
|
||||
else if (isnegativeterm(p))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
|
48
sin.cpp
48
sin.cpp
|
@ -24,7 +24,7 @@ sine(void)
|
|||
static void
|
||||
sine_f(void)
|
||||
{
|
||||
int n, u;
|
||||
int n;
|
||||
double d;
|
||||
|
||||
p1 = pop();
|
||||
|
@ -43,6 +43,16 @@ sine_f(void)
|
|||
return;
|
||||
}
|
||||
|
||||
// sine function is antisymmetric, sin(-x) = -sin(x)
|
||||
|
||||
if (isnegative(p1)) {
|
||||
push(p1);
|
||||
negate();
|
||||
sine();
|
||||
negate();
|
||||
return;
|
||||
}
|
||||
|
||||
// multiply by 180/pi
|
||||
|
||||
push(p1);
|
||||
|
@ -53,43 +63,37 @@ sine_f(void)
|
|||
|
||||
n = pop_integer();
|
||||
|
||||
if (n == (int) 0x80000000) {
|
||||
if (n < 0) {
|
||||
push_symbol(SIN);
|
||||
push(p1);
|
||||
list(2);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
n = -n;
|
||||
u = -1;
|
||||
} else
|
||||
u = 1;
|
||||
|
||||
switch (n % 360) {
|
||||
case 0:
|
||||
push_integer(0);
|
||||
break;
|
||||
case 30:
|
||||
push_rational(u, 2);
|
||||
push_rational(1, 2);
|
||||
break;
|
||||
case 90:
|
||||
push_integer(u);
|
||||
push_integer(1);
|
||||
break;
|
||||
case 150:
|
||||
push_rational(u, 2);
|
||||
push_rational(1, 2);
|
||||
break;
|
||||
case 180:
|
||||
push_integer(0);
|
||||
break;
|
||||
case 210:
|
||||
push_rational(-u, 2);
|
||||
push_rational(-1, 2);
|
||||
break;
|
||||
case 270:
|
||||
push_integer(-u);
|
||||
push_integer(-1);
|
||||
break;
|
||||
case 330:
|
||||
push_rational(-u, 2);
|
||||
push_rational(-1, 2);
|
||||
break;
|
||||
default:
|
||||
push_symbol(SIN);
|
||||
|
@ -111,19 +115,19 @@ static char *s[] = {
|
|||
"-1/2",
|
||||
|
||||
"sin(-3/4*pi)", // -135 degrees
|
||||
"sin(-3/4*pi)",
|
||||
"-sin(3/4*pi)",
|
||||
|
||||
"sin(-2/3*pi)", // -120 degrees
|
||||
"sin(-2/3*pi)",
|
||||
"-sin(2/3*pi)",
|
||||
|
||||
"sin(-pi/2)", // -90 degrees
|
||||
"-1",
|
||||
|
||||
"sin(-1/3*pi)", // -60 degrees
|
||||
"sin(-1/3*pi)",
|
||||
"-sin(1/3*pi)",
|
||||
|
||||
"sin(-1/4*pi)", // -45 degrees
|
||||
"sin(-1/4*pi)",
|
||||
"-sin(1/4*pi)",
|
||||
|
||||
"sin(-pi/6)", // -30 degrees
|
||||
"-1/2",
|
||||
|
@ -166,6 +170,14 @@ static char *s[] = {
|
|||
|
||||
"expomode=0",
|
||||
"",
|
||||
|
||||
// sine function is antisymmetric
|
||||
|
||||
"sin(-x)",
|
||||
"-sin(x)",
|
||||
|
||||
"sin(b-a)",
|
||||
"-sin(a-b)",
|
||||
};
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue