Browse Source

core: implement tan() for real numbers.

legacy
Sam Hocevar sam 13 years ago
parent
commit
efafb640b7
2 changed files with 25 additions and 0 deletions
  1. +24
    -0
      src/real.cpp
  2. +1
    -0
      src/real.h

+ 24
- 0
src/real.cpp View File

@@ -746,6 +746,30 @@ real cos(real const &x)
return sin(real::R_PI_2 - x);
}

real tan(real const &x)
{
/* Constrain input to [-π,π] */
real y = fmod(x, real::R_PI);

/* Constrain input to [-π/2,π/2] */
if (y < -real::R_PI_2)
y += real::R_PI;
else if (y > real::R_PI_2)
y -= real::R_PI;

/* In [-π/4,π/4] return sin/cos */
if (fabs(y) <= real::R_PI_4)
return sin(y) / cos(y);

/* Otherwise, return cos/sin */
if (y > real::R_0)
y = real::R_PI_2 - y;
else
y = -real::R_PI_2 - y;

return cos(y) / sin(y);
}

static real asinacos(real const &x, bool is_asin, bool is_negative)
{
/* Strategy for asin(): in [-0.5..0.5], use a Taylor series around


+ 1
- 0
src/real.h View File

@@ -76,6 +76,7 @@ public:

friend real sin(real const &x);
friend real cos(real const &x);
friend real tan(real const &x);
friend real asin(real const &x);
friend real acos(real const &x);
friend real atan(real const &x);


Loading…
Cancel
Save