Changeset 973


Ignore:
Timestamp:
Sep 22, 2011, 9:16:02 AM (10 years ago)
Author:
sam
Message:

core: implement comparison operators for reals.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r972 r973  
    7878        return *this;
    7979
    80     /* Ensure *this is the larger exponent, and both arguments are
    81      * positive (otherwise, switch signs, or replace + with -). */
     80    /* Ensure both arguments are positive. Otherwise, switch signs,
     81     * or replace + with -). */
     82    if (m_signexp >> 31)
     83        return -(-*this + -x);
     84
     85    if (x.m_signexp >> 31)
     86        return *this - x;
     87
     88    /* Ensure *this is the larger exponent (no need to be strictly larger,
     89     * as in subtraction). Otherwise, switch. */
    8290    if ((m_signexp << 1) < (x.m_signexp << 1))
    8391        return x + *this;
    84 
    85     if (m_signexp >> 31)
    86         return -(-*this + -x);
    87 
    88     if (x.m_signexp >> 31)
    89         return *this - x;
    9092
    9193    real ret;
     
    138140        return *this;
    139141
    140     /* Ensure *this is the larger exponent, and both arguments are
    141      * positive (otherwise, switch signs, or replace - with +). */
    142     if ((m_signexp << 1) < (x.m_signexp << 1))
     142    /* Ensure both arguments are positive. Otherwise, switch signs,
     143     * or replace - with +). */
     144    if (m_signexp >> 31)
     145        return -(-*this + x);
     146
     147    if (x.m_signexp >> 31)
     148        return (*this) + (-x);
     149
     150    /* Ensure *this is larger than x */
     151    if (*this < x)
    143152        return -(x - *this);
    144 
    145     if (m_signexp >> 31)
    146         return -(-*this + x);
    147 
    148     if (x.m_signexp >> 31)
    149         return (*this) + (-x);
    150153
    151154    real ret;
     
    203206}
    204207
     208bool real::operator <(real const &x) const
     209{
     210    /* Ensure both numbers are positive */
     211    if (m_signexp >> 31)
     212        return (x.m_signexp >> 31) ? -*this > -x : true;
     213
     214    if (x.m_signexp >> 31)
     215        return false;
     216
     217    /* Compare all relevant bits */
     218    if (m_signexp != x.m_signexp)
     219        return m_signexp < x.m_signexp;
     220
     221    for (int i = 0; i < BIGITS; i++)
     222        if (m_mantissa[i] != x.m_mantissa[i])
     223            return m_mantissa[i] < x.m_mantissa[i];
     224
     225    return false;
     226}
     227
     228bool real::operator <=(real const &x) const
     229{
     230    return !(*this > x);
     231}
     232
     233bool real::operator >(real const &x) const
     234{
     235    /* Ensure both numbers are positive */
     236    if (m_signexp >> 31)
     237        return (x.m_signexp >> 31) ? -*this < -x : false;
     238
     239    if (x.m_signexp >> 31)
     240        return true;
     241
     242    /* Compare all relevant bits */
     243    if (m_signexp != x.m_signexp)
     244        return m_signexp > x.m_signexp;
     245
     246    for (int i = 0; i < BIGITS; i++)
     247        if (m_mantissa[i] != x.m_mantissa[i])
     248            return m_mantissa[i] > x.m_mantissa[i];
     249
     250    return false;
     251}
     252
     253bool real::operator >=(real const &x) const
     254{
     255    return !(*this < x);
     256}
     257
    205258void real::print() const
    206259{
  • trunk/src/real.h

    r972 r973  
    3636    real operator *(real const &x) const;
    3737
     38    bool operator <(real const &x) const;
     39    bool operator >(real const &x) const;
     40    bool operator <=(real const &x) const;
     41    bool operator >=(real const &x) const;
     42
    3843    void print() const;
    3944
  • trunk/test/unit/real.cpp

    r972 r973  
    5151    }
    5252
     53    LOLUNIT_TEST(test_real_comp)
     54    {
     55        LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
     56        LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
     57        LOLUNIT_ASSERT(real(1.0f) >= real(1.0f));
     58
     59        LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f));
     60        LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f));
     61        LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f));
     62
     63        LOLUNIT_ASSERT(real(-1.0f) < real(0.5f));
     64        LOLUNIT_ASSERT(real(-0.5f) < real(1.0f));
     65        LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f));
     66        LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f));
     67
     68        LOLUNIT_ASSERT(real(1.0f) > real(-0.5f));
     69        LOLUNIT_ASSERT(real(0.5f) > real(-1.0f));
     70        LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f));
     71        LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
     72    }
     73
    5374    LOLUNIT_TEST(test_real_add)
    5475    {
Note: See TracChangeset for help on using the changeset viewer.