Changeset 1018


Ignore:
Timestamp:
Oct 12, 2011, 12:01:47 AM (8 years ago)
Author:
sam
Message:

core: implement tan() for real numbers.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r1017 r1018  
    745745{
    746746    return sin(real::R_PI_2 - x);
     747}
     748
     749real tan(real const &x)
     750{
     751    /* Constrain input to [-π,π] */
     752    real y = fmod(x, real::R_PI);
     753
     754    /* Constrain input to [-π/2,π/2] */
     755    if (y < -real::R_PI_2)
     756        y += real::R_PI;
     757    else if (y > real::R_PI_2)
     758        y -= real::R_PI;
     759
     760    /* In [-π/4,π/4] return sin/cos */
     761    if (fabs(y) <= real::R_PI_4)
     762        return sin(y) / cos(y);
     763
     764    /* Otherwise, return cos/sin */
     765    if (y > real::R_0)
     766        y = real::R_PI_2 - y;
     767    else
     768        y = -real::R_PI_2 - y;
     769
     770    return cos(y) / sin(y);
    747771}
    748772
  • trunk/src/real.h

    r1017 r1018  
    7777    friend real sin(real const &x);
    7878    friend real cos(real const &x);
     79    friend real tan(real const &x);
    7980    friend real asin(real const &x);
    8081    friend real acos(real const &x);
Note: See TracChangeset for help on using the changeset viewer.