Changeset 990


Ignore:
Timestamp:
Sep 28, 2011, 2:59:39 AM (10 years ago)
Author:
sam
Message:

core: implement sin(), cos() and abs() for reals and fix a crash in the
addition and subtraction operators occurring when exponents were too
different.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r988 r990  
    130130    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
    131131
     132    if (bigoff > BIGITS)
     133        return *this;
     134
    132135    ret.m_signexp = m_signexp;
    133136
     
    188191    int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
    189192    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
     193
     194    if (bigoff > BIGITS)
     195        return *this;
    190196
    191197    ret.m_signexp = m_signexp;
     
    502508}
    503509
     510real fabs(real const &x)
     511{
     512    real ret = x;
     513    ret.m_signexp &= 0x7fffffffu;
     514    return ret;
     515}
     516
    504517real exp(real const &x)
    505518{
     
    526539    for (int i = 0; i < square; i++)
    527540        ret = ret * ret;
     541
     542    return ret;
     543}
     544
     545real sin(real const &x)
     546{
     547    real ret = 0.0, fact = 1.0, xn = x, x2 = x * x;
     548
     549    for (int i = 1; ; i += 2)
     550    {
     551        real newret = ret + xn / fact;
     552        if (ret == newret)
     553            break;
     554        ret = newret;
     555        xn *= x2;
     556        fact *= (real)(-(i + 1) * (i + 2));
     557    }
     558
     559    return ret;
     560}
     561
     562real cos(real const &x)
     563{
     564    real ret = 0.0, fact = 1.0, xn = 1.0, x2 = x * x;
     565
     566    for (int i = 1; ; i += 2)
     567    {
     568        real newret = ret + xn / fact;
     569        if (ret == newret)
     570            break;
     571        ret = newret;
     572        xn *= x2;
     573        fact *= (real)(-i * (i + 1));
     574    }
    528575
    529576    return ret;
  • trunk/src/real.h

    r988 r990  
    5454    bool operator >=(real const &x) const;
    5555
     56    friend real fabs(real const &x);
     57
    5658    friend real fres(real const &x);
    5759    friend real sqrt(real const &x);
    5860    friend real exp(real const &x);
     61
     62    friend real sin(real const &x);
     63    friend real cos(real const &x);
    5964
    6065    void print(int ndigits = 150) const;
Note: See TracChangeset for help on using the changeset viewer.