Changeset 1024


Ignore:
Timestamp:
Oct 14, 2011, 12:16:25 AM (11 years ago)
Author:
sam
Message:

core: implement frexp(), ldexp() and modf() for reals.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r1023 r1024  
    769769     * off, but that's about it. */
    770770    return (exp(x) + exp(-x)) >> 1;
     771}
     772
     773real frexp(real const &x, int *exp)
     774{
     775    if (!x)
     776    {
     777        *exp = 0;
     778        return x;
     779    }
     780
     781    real ret = x;
     782    int exponent = (ret.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
     783    *exp = exponent + 1;
     784    ret.m_signexp -= exponent + 1;
     785    return ret;
     786}
     787
     788real ldexp(real const &x, int exp)
     789{
     790    real ret = x;
     791    if (ret)
     792        ret.m_signexp += exp;
     793    return ret;
     794}
     795
     796real modf(real const &x, real *iptr)
     797{
     798    real absx = fabs(x);
     799    real tmp = floor(absx);
     800
     801    *iptr = copysign(tmp, x);
     802    return copysign(absx - tmp, x);
    771803}
    772804
     
    10641096}
    10651097
     1098real atan2(real const &y, real const &x)
     1099{
     1100    if (!y)
     1101    {
     1102        if ((x.m_signexp >> 31) == 0)
     1103            return y;
     1104        if (y.m_signexp >> 31)
     1105            return -real::R_PI;
     1106        return real::R_PI;
     1107    }
     1108
     1109    if (!x)
     1110    {
     1111        if (y.m_signexp >> 31)
     1112            return -real::R_PI;
     1113        return real::R_PI;
     1114    }
     1115
     1116    /* FIXME: handle the Inf and NaN cases */
     1117    real z = y / x;
     1118    real ret = atan(z);
     1119    if (x < real::R_0)
     1120        ret += (y > real::R_0) ? real::R_PI : -real::R_PI;
     1121    return ret;
     1122}
     1123
    10661124void real::hexprint() const
    10671125{
  • trunk/src/real.h

    r1023 r1024  
    7070    friend real acos(real const &x);
    7171    friend real atan(real const &x);
    72     /* FIXME: atan2 */
     72    friend real atan2(real const &y, real const &x);
    7373
    7474    /* Hyperbolic functions */
     
    8383    friend real log2(real const &x);
    8484    friend real log10(real const &x);
    85     /* FIXME: frexp ldexp modf */
     85    friend real frexp(real const &x, int *exp);
     86    friend real ldexp(real const &x, int exp);
     87    friend real modf(real const &x, real *iptr);
    8688
    8789    /* Power functions */
Note: See TracChangeset for help on using the changeset viewer.