Ignore:
Timestamp:
Sep 28, 2011, 7:04:52 PM (11 years ago)
Author:
sam
Message:

core: create global real number variables such as R_E, R_PI, R_LN2 etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r994 r995  
    712712}
    713713
     714static real fast_pi()
     715{
     716    /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
     717    real ret = 0.0, x0 = 5.0, x1 = 239.0;
     718    real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;
     719
     720    /* Degree 240 is required for 512-bit mantissa precision */
     721    for (int i = 1; i < 240; i += 2)
     722    {
     723        ret += r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
     724        x0 *= m0;
     725        x1 *= m1;
     726    }
     727
     728    return ret;
     729}
     730
     731real const real::R_E        = exp((real)1.0);
     732real const real::R_LOG2E    = (real)1.0 / R_LN2;
     733real const real::R_LOG10E   = (real)1.0 / R_LN10;
     734real const real::R_LN2      = log((real)2.0);
     735real const real::R_LN10     = log((real)10.0);
     736real const real::R_PI       = fast_pi();
     737real const real::R_PI_2     = R_PI >> 1;
     738real const real::R_PI_4     = R_PI >> 2;
     739real const real::R_1_PI     = (real)1.0 / R_PI;
     740real const real::R_2_PI     = R_1_PI << 1;
     741real const real::R_2_SQRTPI = (real)2.0 / sqrt(R_PI);
     742real const real::R_SQRT2    = sqrt((real)2.0);
     743real const real::R_SQRT1_2  = R_SQRT2 >> 1;
     744
    714745} /* namespace lol */
    715746
Note: See TracChangeset for help on using the changeset viewer.