Changeset 998


Ignore:
Timestamp:
Oct 3, 2011, 1:36:16 AM (11 years ago)
Author:
sam
Message:

core: add real constants for 0, 1, 2 and 10, and reorder static constants
so that their initialisation order ensures they have the proper values. Add
unit tests for these constants.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r997 r998  
    324324real real::operator /(real const &x) const
    325325{
    326     return *this * fres(x);
     326    return *this * re(x);
    327327}
    328328
     
    456456}
    457457
    458 real fres(real const &x)
     458real re(real const &x)
    459459{
    460460    if (!(x.m_signexp << 1))
     
    742742}
    743743
    744 real const real::R_E        = exp((real)1.0);
    745 real const real::R_LOG2E    = (real)1.0 / R_LN2;
    746 real const real::R_LOG10E   = (real)1.0 / R_LN10;
    747 real const real::R_LN2      = log((real)2.0);
    748 real const real::R_LN10     = log((real)10.0);
     744real const real::R_0        = (real)0.0;
     745real const real::R_1        = (real)1.0;
     746real const real::R_2        = (real)2.0;
     747real const real::R_10       = (real)10.0;
     748
     749real const real::R_E        = exp(R_1);
     750real const real::R_LN2      = log(R_2);
     751real const real::R_LN10     = log(R_10);
     752real const real::R_LOG2E    = re(R_LN2);
     753real const real::R_LOG10E   = re(R_LN10);
    749754real const real::R_PI       = fast_pi();
    750755real const real::R_PI_2     = R_PI >> 1;
    751756real const real::R_PI_4     = R_PI >> 2;
    752 real const real::R_1_PI     = (real)1.0 / R_PI;
     757real const real::R_1_PI     = re(R_PI);
    753758real const real::R_2_PI     = R_1_PI << 1;
    754 real const real::R_2_SQRTPI = (real)2.0 / sqrt(R_PI);
    755 real const real::R_SQRT2    = sqrt((real)2.0);
     759real const real::R_2_SQRTPI = re(sqrt(R_PI)) << 1;
     760real const real::R_SQRT2    = sqrt(R_2);
    756761real const real::R_SQRT1_2  = R_SQRT2 >> 1;
    757762
  • trunk/src/real.h

    r997 r998  
    6464    friend real fabs(real const &x);
    6565
    66     friend real fres(real const &x);
     66    friend real re(real const &x);
    6767    friend real sqrt(real const &x);
    6868    friend real log(real const &x);
     
    7373
    7474    void print(int ndigits = 150) const;
     75
     76    static real const R_0;
     77    static real const R_1;
     78    static real const R_2;
     79    static real const R_10;
    7580
    7681    static real const R_E;
  • trunk/test/unit/real.cpp

    r997 r998  
    2323LOLUNIT_FIXTURE(RealTest)
    2424{
     25    LOLUNIT_TEST(Constants)
     26    {
     27        double a0 = real::R_0;
     28        double a1 = real::R_1;
     29        double a2 = real::R_2;
     30        double a10 = real::R_10;
     31
     32        LOLUNIT_ASSERT_EQUAL(a0, 0.0);
     33        LOLUNIT_ASSERT_EQUAL(a1, 1.0);
     34        LOLUNIT_ASSERT_EQUAL(a2, 2.0);
     35        LOLUNIT_ASSERT_EQUAL(a10, 10.0);
     36
     37        double b0 = log(real::R_E);
     38        LOLUNIT_ASSERT_EQUAL(b0, 1.0);
     39
     40        double b1 = exp(re(real::R_LOG2E));
     41        LOLUNIT_ASSERT_EQUAL(b1, 2.0);
     42
     43        double b2 = exp(re(real::R_LOG10E));
     44        LOLUNIT_ASSERT_EQUAL(b2, 10.0);
     45
     46        double b3 = exp(real::R_LN2);
     47        LOLUNIT_ASSERT_EQUAL(b3, 2.0);
     48
     49        double b4 = exp(real::R_LN10);
     50        LOLUNIT_ASSERT_EQUAL(b4, 10.0);
     51
     52        double b5 = sin(real::R_PI);
     53        double b6 = cos(real::R_PI);
     54        LOLUNIT_ASSERT_DOUBLES_EQUAL(b5, 0.0, 1e-100);
     55        LOLUNIT_ASSERT_EQUAL(b6, -1.0);
     56
     57        double b7 = sin(real::R_PI_2);
     58        double b8 = cos(real::R_PI_2);
     59        LOLUNIT_ASSERT_EQUAL(b7, 1.0);
     60        LOLUNIT_ASSERT_DOUBLES_EQUAL(b8, 0.0, 1e-100);
     61
     62        double b9 = sin(real::R_PI_4) * sin(real::R_PI_4);
     63        double b10 = cos(real::R_PI_4) * cos(real::R_PI_4);
     64        LOLUNIT_ASSERT_EQUAL(b9, 0.5);
     65        LOLUNIT_ASSERT_EQUAL(b10, 0.5);
     66    }
     67
    2568    LOLUNIT_TEST(FloatToReal)
    2669    {
Note: See TracChangeset for help on using the changeset viewer.