Changeset 1130


Ignore:
Timestamp:
Jan 12, 2012, 12:36:29 AM (10 years ago)
Author:
sam
Message:

math: implement ulp() for reals, which returns the smallest real y > 0 such
that x + y != x, and nextafter() which behaves like the C function.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/math/real.h

    r1122 r1130  
    8989    friend real ldexp(real const &x, int exp);
    9090    friend real modf(real const &x, real *iptr);
     91    friend real ulp(real const &x);
     92    friend real nextafter(real const &x, real const &y);
    9193
    9294    /* Power functions */
  • trunk/src/real.cpp

    r1128 r1130  
    939939}
    940940
     941real ulp(real const &x)
     942{
     943    real ret = real::R_1;
     944    if (x)
     945        ret.m_signexp = x.m_signexp + 1 - real::BIGITS * real::BIGIT_BITS;
     946    else
     947        ret.m_signexp = 0;
     948    return ret;
     949}
     950
     951real nextafter(real const &x, real const &y)
     952{
     953    if (x == y)
     954        return x;
     955    else if (x < y)
     956        return x + ulp(x);
     957    else
     958        return x - ulp(x);
     959}
     960
    941961real copysign(real const &x, real const &y)
    942962{
  • trunk/test/unit/real.cpp

    r1122 r1130  
    241241        LOLUNIT_ASSERT_EQUAL((double)ldexp(a3, 7), 0.0);
    242242        LOLUNIT_ASSERT_EQUAL((double)ldexp(a3, -7), 0.0);
     243    }
     244
     245    LOLUNIT_TEST(Ulp)
     246    {
     247        real a1 = real::R_PI;
     248
     249        LOLUNIT_ASSERT_NOT_EQUAL((double)(a1 + ulp(a1) - a1), 0.0);
     250        LOLUNIT_ASSERT_EQUAL((double)(a1 + ulp(a1) / 2 - a1), 0.0);
    243251    }
    244252
Note: See TracChangeset for help on using the changeset viewer.