瀏覽代碼

core: implement comparison operators for reals.

legacy
Sam Hocevar sam 13 年之前
父節點
當前提交
a173e49ddc
共有 3 個檔案被更改,包括 89 行新增10 行删除
  1. +63
    -10
      src/real.cpp
  2. +5
    -0
      src/real.h
  3. +21
    -0
      test/unit/real.cpp

+ 63
- 10
src/real.cpp 查看文件

@@ -77,17 +77,19 @@ real real::operator +(real const &x) const
if (x.m_signexp << 1 == 0)
return *this;

/* Ensure *this is the larger exponent, and both arguments are
* positive (otherwise, switch signs, or replace + with -). */
if ((m_signexp << 1) < (x.m_signexp << 1))
return x + *this;

/* Ensure both arguments are positive. Otherwise, switch signs,
* or replace + with -). */
if (m_signexp >> 31)
return -(-*this + -x);

if (x.m_signexp >> 31)
return *this - x;

/* Ensure *this is the larger exponent (no need to be strictly larger,
* as in subtraction). Otherwise, switch. */
if ((m_signexp << 1) < (x.m_signexp << 1))
return x + *this;

real ret;

int e1 = m_signexp - (1 << 30) + 1;
@@ -137,17 +139,18 @@ real real::operator -(real const &x) const
if (x.m_signexp << 1 == 0)
return *this;

/* Ensure *this is the larger exponent, and both arguments are
* positive (otherwise, switch signs, or replace - with +). */
if ((m_signexp << 1) < (x.m_signexp << 1))
return -(x - *this);

/* Ensure both arguments are positive. Otherwise, switch signs,
* or replace - with +). */
if (m_signexp >> 31)
return -(-*this + x);

if (x.m_signexp >> 31)
return (*this) + (-x);

/* Ensure *this is larger than x */
if (*this < x)
return -(x - *this);

real ret;

return ret;
@@ -202,6 +205,56 @@ real real::operator *(real const &x) const
return ret;
}

bool real::operator <(real const &x) const
{
/* Ensure both numbers are positive */
if (m_signexp >> 31)
return (x.m_signexp >> 31) ? -*this > -x : true;

if (x.m_signexp >> 31)
return false;

/* Compare all relevant bits */
if (m_signexp != x.m_signexp)
return m_signexp < x.m_signexp;

for (int i = 0; i < BIGITS; i++)
if (m_mantissa[i] != x.m_mantissa[i])
return m_mantissa[i] < x.m_mantissa[i];

return false;
}

bool real::operator <=(real const &x) const
{
return !(*this > x);
}

bool real::operator >(real const &x) const
{
/* Ensure both numbers are positive */
if (m_signexp >> 31)
return (x.m_signexp >> 31) ? -*this < -x : false;

if (x.m_signexp >> 31)
return true;

/* Compare all relevant bits */
if (m_signexp != x.m_signexp)
return m_signexp > x.m_signexp;

for (int i = 0; i < BIGITS; i++)
if (m_mantissa[i] != x.m_mantissa[i])
return m_mantissa[i] > x.m_mantissa[i];

return false;
}

bool real::operator >=(real const &x) const
{
return !(*this < x);
}

void real::print() const
{
printf("%x %08x ", m_signexp >> 31, (m_signexp << 1) >> 1);


+ 5
- 0
src/real.h 查看文件

@@ -35,6 +35,11 @@ public:
real operator -(real const &x) const;
real operator *(real const &x) const;

bool operator <(real const &x) const;
bool operator >(real const &x) const;
bool operator <=(real const &x) const;
bool operator >=(real const &x) const;

void print() const;

private:


+ 21
- 0
test/unit/real.cpp 查看文件

@@ -50,6 +50,27 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_EQUAL(a4, 0.0f);
}

LOLUNIT_TEST(test_real_comp)
{
LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
LOLUNIT_ASSERT(real(1.0f) >= real(1.0f));

LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f));
LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f));
LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f));

LOLUNIT_ASSERT(real(-1.0f) < real(0.5f));
LOLUNIT_ASSERT(real(-0.5f) < real(1.0f));
LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f));
LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f));

LOLUNIT_ASSERT(real(1.0f) > real(-0.5f));
LOLUNIT_ASSERT(real(0.5f) > real(-1.0f));
LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f));
LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
}

LOLUNIT_TEST(test_real_add)
{
float a1 = real(1.0f) + real(0.0f);


Loading…
取消
儲存