Changeset 997 for trunk


Ignore:
Timestamp:
Sep 29, 2011, 9:10:00 PM (12 years ago)
Author:
sam
Message:

core: add boolean operators on real numbers, add unit tests for that,
and simplify the Remez code accordingly.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r995 r997  
    441441{
    442442    return !(*this < x);
     443}
     444
     445bool real::operator !() const
     446{
     447    return !(bool)*this;
     448}
     449
     450real::operator bool() const
     451{
     452    /* A real is "true" if it is non-zero (exponent is non-zero) AND
     453     * not NaN (exponent is not full bits OR higher order mantissa is zero) */
     454    uint32_t exponent = m_signexp << 1;
     455    return exponent && (~exponent || m_mantissa[0] == 0);
    443456}
    444457
  • trunk/src/real.h

    r995 r997  
    5959    bool operator >=(real const &x) const;
    6060
     61    bool operator !() const;
     62    operator bool() const;
     63
    6164    friend real fabs(real const &x);
    6265
  • trunk/test/math/remez.cpp

    r996 r997  
    2222static int const ORDER = 8;
    2323
    24 static real compute_pi()
    25 {
    26     /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
    27     real sum = 0.0, x0 = 5.0, x1 = 239.0;
    28     real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;
    29 
    30     /* Degree 240 is required for 512-bit mantissa precision */
    31     for (int i = 1; i < 240; i += 2)
    32     {
    33         sum += r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
    34         x0 *= m0;
    35         x1 *= m1;
    36     }
    37     return sum;
    38 }
    39 
    4024/* The function we want to approximate */
    4125static real myfun(real const &x)
    4226{
    43     static real const half_pi = compute_pi() * (real)0.5;
    4427    static real const one = 1.0;
    45     if (x == (real)0.0 || x == (real)-0.0)
    46         return half_pi;
    47     return sin(x * half_pi) / x;
     28    if (!x)
     29        return real::R_PI_2;
     30    return sin(x * real::R_PI_2) / x;
    4831    //return cos(x) - one;
    4932    //return exp(x);
  • trunk/test/unit/real.cpp

    r994 r997  
    177177        LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0);
    178178    }
     179
     180    LOLUNIT_TEST(Bool)
     181    {
     182        real a = 0.0;
     183        LOLUNIT_ASSERT(!a);
     184
     185        a = -0.0;
     186        LOLUNIT_ASSERT(!a);
     187
     188        a = 1234.0;
     189        LOLUNIT_ASSERT(a);
     190        LOLUNIT_ASSERT(!!a);
     191
     192        a = -1234.0;
     193        LOLUNIT_ASSERT(a);
     194        LOLUNIT_ASSERT(!!a);
     195    }
    179196};
    180197
Note: See TracChangeset for help on using the changeset viewer.