Changeset 1163


Ignore:
Timestamp:
Mar 14, 2012, 8:19:48 PM (10 years ago)
Author:
sam
Message:

math: ensure real::fabs() is never chosen over std::fabs() for arguments
that are not explicitly real, even with namespace mistakes.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/debug/sphere.cpp

    r1146 r1163  
    153153
    154154    int const ndiv = 2;
     155    using std::log;
    155156    int const ntriangles = 20 * (1 << (ndiv * 2))
    156157                              * (int)(log(1.0f / 0.01f) / log(1.1f) + 0.9999f);
  • trunk/src/lol/math/real.h

    r1130 r1163  
    2525{
    2626
    27 class real
     27/*
     28 * The base class for reals. The only real reason for making this a template
     29 * class is so we can have implicit constructors ("real x = 1" works) but
     30 * avoid accidental implicit conversions ("int x = 1; sqrt(x)" will never
     31 * call real::sqrt).
     32 */
     33template<int N> class Real
    2834{
    2935public:
    30     real();
    31     real(real const &x);
    32     real const &operator =(real const &x);
    33     ~real();
    34 
    35     real(float f);
    36     real(double f);
    37     real(int i);
    38     real(unsigned int i);
    39 
    40     real(char const *str);
     36    Real();
     37    Real(Real<N> const &x);
     38    Real const &operator =(Real<N> const &x);
     39    ~Real();
     40
     41    Real(float f);
     42    Real(double f);
     43    Real(int i);
     44    Real(unsigned int i);
     45
     46    Real(char const *str);
    4147
    4248    operator float() const;
     
    4551    operator unsigned int() const;
    4652
    47     real operator +() const;
    48     real operator -() const;
    49     real operator +(real const &x) const;
    50     real operator -(real const &x) const;
    51     real operator *(real const &x) const;
    52     real operator /(real const &x) const;
    53     real const &operator +=(real const &x);
    54     real const &operator -=(real const &x);
    55     real const &operator *=(real const &x);
    56     real const &operator /=(real const &x);
    57 
    58     bool operator ==(real const &x) const;
    59     bool operator !=(real const &x) const;
    60     bool operator <(real const &x) const;
    61     bool operator >(real const &x) const;
    62     bool operator <=(real const &x) const;
    63     bool operator >=(real const &x) const;
     53    Real<N> operator +() const;
     54    Real<N> operator -() const;
     55    Real<N> operator +(Real<N> const &x) const;
     56    Real<N> operator -(Real<N> const &x) const;
     57    Real<N> operator *(Real<N> const &x) const;
     58    Real<N> operator /(Real<N> const &x) const;
     59    Real<N> const &operator +=(Real<N> const &x);
     60    Real<N> const &operator -=(Real<N> const &x);
     61    Real<N> const &operator *=(Real<N> const &x);
     62    Real<N> const &operator /=(Real<N> const &x);
     63
     64    bool operator ==(Real<N> const &x) const;
     65    bool operator !=(Real<N> const &x) const;
     66    bool operator <(Real<N> const &x) const;
     67    bool operator >(Real<N> const &x) const;
     68    bool operator <=(Real<N> const &x) const;
     69    bool operator >=(Real<N> const &x) const;
    6470
    6571    bool operator !() const;
     
    6773
    6874    /* Trigonometric functions */
    69     friend real sin(real const &x);
    70     friend real cos(real const &x);
    71     friend real tan(real const &x);
    72     friend real asin(real const &x);
    73     friend real acos(real const &x);
    74     friend real atan(real const &x);
    75     friend real atan2(real const &y, real const &x);
     75    template<int M> friend Real<M> sin(Real<M> const &x);
     76    template<int M> friend Real<M> cos(Real<M> const &x);
     77    template<int M> friend Real<M> tan(Real<M> const &x);
     78    template<int M> friend Real<M> asin(Real<M> const &x);
     79    template<int M> friend Real<M> acos(Real<M> const &x);
     80    template<int M> friend Real<M> atan(Real<M> const &x);
     81    template<int M> friend Real<M> atan2(Real<M> const &y, Real<M> const &x);
    7682
    7783    /* Hyperbolic functions */
    78     friend real sinh(real const &x);
    79     friend real cosh(real const &x);
    80     friend real tanh(real const &x);
     84    template<int M> friend Real<M> sinh(Real<M> const &x);
     85    template<int M> friend Real<M> cosh(Real<M> const &x);
     86    template<int M> friend Real<M> tanh(Real<M> const &x);
    8187
    8288    /* Exponential and logarithmic functions */
    83     friend real exp(real const &x);
    84     friend real exp2(real const &x);
    85     friend real log(real const &x);
    86     friend real log2(real const &x);
    87     friend real log10(real const &x);
    88     friend real frexp(real const &x, int *exp);
    89     friend real ldexp(real const &x, int exp);
    90     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);
     89    template<int M> friend Real<M> exp(Real<M> const &x);
     90    template<int M> friend Real<M> exp2(Real<M> const &x);
     91    template<int M> friend Real<M> log(Real<M> const &x);
     92    template<int M> friend Real<M> log2(Real<M> const &x);
     93    template<int M> friend Real<M> log10(Real<M> const &x);
     94    template<int M> friend Real<M> frexp(Real<M> const &x, int *exp);
     95    template<int M> friend Real<M> ldexp(Real<M> const &x, int exp);
     96    template<int M> friend Real<M> modf(Real<M> const &x, Real<M> *iptr);
     97    template<int M> friend Real<M> ulp(Real<M> const &x);
     98    template<int M> friend Real<M> nextafter(Real<M> const &x, Real<M> const &y);
    9399
    94100    /* Power functions */
    95     friend real re(real const &x);
    96     friend real sqrt(real const &x);
    97     friend real cbrt(real const &x);
    98     friend real pow(real const &x, real const &y);
    99     friend real gamma(real const &x);
     101    template<int M> friend Real<M> re(Real<M> const &x);
     102    template<int M> friend Real<M> sqrt(Real<M> const &x);
     103    template<int M> friend Real<M> cbrt(Real<M> const &x);
     104    template<int M> friend Real<M> pow(Real<M> const &x, Real<M> const &y);
     105    template<int M> friend Real<M> gamma(Real<M> const &x);
    100106
    101107    /* Rounding, absolute value, remainder etc. */
    102     friend real ceil(real const &x);
    103     friend real copysign(real const &x, real const &y);
    104     friend real floor(real const &x);
    105     friend real fabs(real const &x);
    106     friend real round(real const &x);
    107     friend real fmod(real const &x, real const &y);
     108    template<int M> friend Real<M> ceil(Real<M> const &x);
     109    template<int M> friend Real<M> copysign(Real<M> const &x, Real<M> const &y);
     110    template<int M> friend Real<M> floor(Real<M> const &x);
     111    template<int M> friend Real<M> fabs(Real<M> const &x);
     112    template<int M> friend Real<M> round(Real<M> const &x);
     113    template<int M> friend Real<M> fmod(Real<M> const &x, Real<N> const &y);
    108114
    109115    void hexprint() const;
     
    112118    /* Additional operators using base C++ types */
    113119#define __LOL_REAL_OP_HELPER_GENERIC(op, type) \
    114     inline real operator op(type x) const { return *this op (real)x; } \
    115     inline real const &operator op##=(type x) { return *this = (*this op x); }
     120    inline Real<N> operator op(type x) const { return *this op (Real<N>)x; } \
     121    inline Real<N> const &operator op##=(type x) { return *this = (*this op x); }
    116122#define __LOL_REAL_OP_HELPER_FASTMULDIV(op, type) \
    117     inline real operator op(type x) const \
     123    inline Real<N> operator op(type x) const \
    118124    { \
    119         real tmp = *this; return tmp op##= x; \
     125        Real<N> tmp = *this; return tmp op##= x; \
    120126    } \
    121     inline real const &operator op##=(type x) \
     127    inline Real<N> const &operator op##=(type x) \
    122128    { \
    123129        /* If multiplying or dividing by a power of two, take a shortcut */ \
     
    128134        } \
    129135        else \
    130             *this = *this op (real)x; \
     136            *this = *this op (Real<N>)x; \
    131137        return *this; \
    132138    }
     
    148154
    149155    /* Constants */
    150     static real const R_0;
    151     static real const R_1;
    152     static real const R_2;
    153     static real const R_3;
    154     static real const R_10;
    155 
    156     static real const R_E;
    157     static real const R_LOG2E;
    158     static real const R_LOG10E;
    159     static real const R_LN2;
    160     static real const R_LN10;
    161     static real const R_PI;
    162     static real const R_PI_2;
    163     static real const R_PI_3;
    164     static real const R_PI_4;
    165     static real const R_1_PI;
    166     static real const R_2_PI;
    167     static real const R_2_SQRTPI;
    168     static real const R_SQRT2;
    169     static real const R_SQRT3;
    170     static real const R_SQRT1_2;
     156    static Real<N> const R_0;
     157    static Real<N> const R_1;
     158    static Real<N> const R_2;
     159    static Real<N> const R_3;
     160    static Real<N> const R_10;
     161
     162    static Real<N> const R_E;
     163    static Real<N> const R_LOG2E;
     164    static Real<N> const R_LOG10E;
     165    static Real<N> const R_LN2;
     166    static Real<N> const R_LN10;
     167    static Real<N> const R_PI;
     168    static Real<N> const R_PI_2;
     169    static Real<N> const R_PI_3;
     170    static Real<N> const R_PI_4;
     171    static Real<N> const R_1_PI;
     172    static Real<N> const R_2_PI;
     173    static Real<N> const R_2_SQRTPI;
     174    static Real<N> const R_SQRT2;
     175    static Real<N> const R_SQRT3;
     176    static Real<N> const R_SQRT1_2;
    171177
    172178    /* XXX: changing this requires tuning real::fres (the number of
    173179     * Newton-Raphson iterations) and real::print (the number of printed
    174180     * digits) */
    175     static int const BIGITS = 16;
     181    static int const BIGITS = N;
    176182    static int const BIGIT_BITS = 32;
    177183
     
    181187};
    182188
     189/*
     190 * The real type used for real numbers
     191 */
     192typedef Real<16> real;
     193
     194/*
     195 * Mandatory forward declarations of template specialisations
     196 */
     197template<> real::Real();
     198template<> real::Real(real const &x);
     199template<> real const &real::operator =(real const &x);
     200template<> real::~Real();
     201template<> real::Real(float f);
     202template<> real::Real(double f);
     203template<> real::Real(int i);
     204template<> real::Real(unsigned int i);
     205template<> real::Real(char const *str);
     206
     207template<> real::operator float() const;
     208template<> real::operator double() const;
     209template<> real::operator int() const;
     210template<> real::operator unsigned int() const;
     211template<> real real::operator +() const;
     212template<> real real::operator -() const;
     213template<> real real::operator +(real const &x) const;
     214template<> real real::operator -(real const &x) const;
     215template<> real real::operator *(real const &x) const;
     216template<> real real::operator /(real const &x) const;
     217template<> real const &real::operator +=(real const &x);
     218template<> real const &real::operator -=(real const &x);
     219template<> real const &real::operator *=(real const &x);
     220template<> real const &real::operator /=(real const &x);
     221template<> bool real::operator ==(real const &x) const;
     222template<> bool real::operator !=(real const &x) const;
     223template<> bool real::operator <(real const &x) const;
     224template<> bool real::operator >(real const &x) const;
     225template<> bool real::operator <=(real const &x) const;
     226template<> bool real::operator >=(real const &x) const;
     227template<> bool real::operator !() const;
     228template<> real::operator bool() const;
     229
     230template<> real sin(real const &x);
     231template<> real cos(real const &x);
     232template<> real tan(real const &x);
     233template<> real asin(real const &x);
     234template<> real acos(real const &x);
     235template<> real atan(real const &x);
     236template<> real atan2(real const &y, real const &x);
     237template<> real sinh(real const &x);
     238template<> real cosh(real const &x);
     239template<> real tanh(real const &x);
     240template<> real exp(real const &x);
     241template<> real exp2(real const &x);
     242template<> real log(real const &x);
     243template<> real log2(real const &x);
     244template<> real log10(real const &x);
     245template<> real frexp(real const &x, int *exp);
     246template<> real ldexp(real const &x, int exp);
     247template<> real modf(real const &x, real *iptr);
     248template<> real ulp(real const &x);
     249template<> real nextafter(real const &x, real const &y);
     250template<> real re(real const &x);
     251template<> real sqrt(real const &x);
     252template<> real cbrt(real const &x);
     253template<> real pow(real const &x, real const &y);
     254template<> real gamma(real const &x);
     255template<> real ceil(real const &x);
     256template<> real copysign(real const &x, real const &y);
     257template<> real floor(real const &x);
     258template<> real fabs(real const &x);
     259template<> real round(real const &x);
     260template<> real fmod(real const &x, real const &y);
     261
     262template<> void real::hexprint() const;
     263template<> void real::print(int ndigits) const;
     264
    183265} /* namespace lol */
    184266
  • trunk/src/lol/math/vector.h

    r1161 r1163  
    10481048    inline double len(tname<type> const &a) \
    10491049    { \
    1050         using namespace std; \
     1050        using std::sqrt; \
    10511051        return sqrt((double)sqlen(a)); \
    10521052    } \
  • trunk/src/lol/unit.h

    r937 r1163  
    270270    do { \
    271271        m_asserts++; \
     272        using std::fabs; \
    272273        if (True() && fabs((a) - (b)) > fabs((t))) \
    273274        { \
  • trunk/src/real.cpp

    r1130 r1163  
    3434{
    3535
    36 real::real()
     36template<> real::Real()
    3737{
    3838    m_mantissa = new uint32_t[BIGITS];
     
    4040}
    4141
    42 real::real(real const &x)
     42template<> real::Real(real const &x)
    4343{
    4444    m_mantissa = new uint32_t[BIGITS];
     
    4747}
    4848
    49 real const &real::operator =(real const &x)
     49template<> real const &real::operator =(real const &x)
    5050{
    5151    if (&x != this)
     
    5858}
    5959
    60 real::~real()
     60template<> real::~Real()
    6161{
    6262    delete[] m_mantissa;
    6363}
    6464
    65 real::real(float f) { new(this) real((double)f); }
    66 real::real(int i) { new(this) real((double)i); }
    67 real::real(unsigned int i) { new(this) real((double)i); }
    68 
    69 real::real(double d)
     65template<> real::Real(float f) { new(this) real((double)f); }
     66template<> real::Real(int i) { new(this) real((double)i); }
     67template<> real::Real(unsigned int i) { new(this) real((double)i); }
     68
     69template<> real::Real(double d)
    7070{
    7171    new(this) real();
     
    9494}
    9595
    96 real::operator float() const { return (float)(double)(*this); }
    97 real::operator int() const { return (int)(double)(*this); }
    98 real::operator unsigned int() const { return (unsigned int)(double)(*this); }
    99 
    100 real::operator double() const
     96template<> real::operator float() const { return (float)(double)(*this); }
     97template<> real::operator int() const { return (int)(double)(*this); }
     98template<> real::operator unsigned() const { return (unsigned)(double)(*this); }
     99
     100template<> real::operator double() const
    101101{
    102102    union { double d; uint64_t x; } u;
     
    136136 * Create a real number from an ASCII representation
    137137 */
    138 real::real(char const *str)
     138template<> real::Real(char const *str)
    139139{
    140140    real ret = 0;
     
    193193}
    194194
    195 real real::operator +() const
     195template<> real real::operator +() const
    196196{
    197197    return *this;
    198198}
    199199
    200 real real::operator -() const
     200template<> real real::operator -() const
    201201{
    202202    real ret = *this;
     
    205205}
    206206
    207 real real::operator +(real const &x) const
     207template<> real real::operator +(real const &x) const
    208208{
    209209    if (x.m_signexp << 1 == 0)
     
    269269}
    270270
    271 real real::operator -(real const &x) const
     271template<> real real::operator -(real const &x) const
    272272{
    273273    if (x.m_signexp << 1 == 0)
     
    373373}
    374374
    375 real real::operator *(real const &x) const
     375template<> real real::operator *(real const &x) const
    376376{
    377377    real ret;
     
    446446}
    447447
    448 real real::operator /(real const &x) const
     448template<> real real::operator /(real const &x) const
    449449{
    450450    return *this * re(x);
    451451}
    452452
    453 real const &real::operator +=(real const &x)
     453template<> real const &real::operator +=(real const &x)
    454454{
    455455    real tmp = *this;
     
    457457}
    458458
    459 real const &real::operator -=(real const &x)
     459template<> real const &real::operator -=(real const &x)
    460460{
    461461    real tmp = *this;
     
    463463}
    464464
    465 real const &real::operator *=(real const &x)
     465template<> real const &real::operator *=(real const &x)
    466466{
    467467    real tmp = *this;
     
    469469}
    470470
    471 real const &real::operator /=(real const &x)
     471template<> real const &real::operator /=(real const &x)
    472472{
    473473    real tmp = *this;
     
    475475}
    476476
    477 bool real::operator ==(real const &x) const
     477template<> bool real::operator ==(real const &x) const
    478478{
    479479    if ((m_signexp << 1) == 0 && (x.m_signexp << 1) == 0)
     
    486486}
    487487
    488 bool real::operator !=(real const &x) const
     488template<> bool real::operator !=(real const &x) const
    489489{
    490490    return !(*this == x);
    491491}
    492492
    493 bool real::operator <(real const &x) const
     493template<> bool real::operator <(real const &x) const
    494494{
    495495    /* Ensure both numbers are positive */
     
    511511}
    512512
    513 bool real::operator <=(real const &x) const
     513template<> bool real::operator <=(real const &x) const
    514514{
    515515    return !(*this > x);
    516516}
    517517
    518 bool real::operator >(real const &x) const
     518template<> bool real::operator >(real const &x) const
    519519{
    520520    /* Ensure both numbers are positive */
     
    536536}
    537537
    538 bool real::operator >=(real const &x) const
     538template<> bool real::operator >=(real const &x) const
    539539{
    540540    return !(*this < x);
    541541}
    542542
    543 bool real::operator !() const
     543template<> bool real::operator !() const
    544544{
    545545    return !(bool)*this;
    546546}
    547547
    548 real::operator bool() const
     548template<> real::operator bool() const
    549549{
    550550    /* A real is "true" if it is non-zero (exponent is non-zero) AND
     
    554554}
    555555
    556 real re(real const &x)
     556template<> real re(real const &x)
    557557{
    558558    if (!(x.m_signexp << 1))
     
    587587}
    588588
    589 real sqrt(real const &x)
     589template<> real sqrt(real const &x)
    590590{
    591591    /* if zero, return x */
     
    634634}
    635635
    636 real cbrt(real const &x)
     636template<> real cbrt(real const &x)
    637637{
    638638    /* if zero, return x */
     
    671671}
    672672
    673 real pow(real const &x, real const &y)
     673template<> real pow(real const &x, real const &y)
    674674{
    675675    if (!y)
     
    720720}
    721721
    722 real gamma(real const &x)
     722template<> real gamma(real const &x)
    723723{
    724724    /* We use Spouge's formula. FIXME: precision is far from acceptable,
     
    747747}
    748748
    749 real fabs(real const &x)
     749template<> real fabs(real const &x)
    750750{
    751751    real ret = x;
     
    787787}
    788788
    789 real log(real const &x)
     789template<> real log(real const &x)
    790790{
    791791    /* Strategy for log(x): if x = 2^E*M then log(x) = E log(2) + log(M),
     
    803803}
    804804
    805 real log2(real const &x)
     805template<> real log2(real const &x)
    806806{
    807807    /* Strategy for log2(x): see log(x). */
     
    818818}
    819819
    820 real log10(real const &x)
     820template<> real log10(real const &x)
    821821{
    822822    return log(x) * real::R_LOG10E;
     
    844844}
    845845
    846 real exp(real const &x)
     846template<> real exp(real const &x)
    847847{
    848848    /* Strategy for exp(x): the Taylor series does not converge very fast
     
    869869}
    870870
    871 real exp2(real const &x)
     871template<> real exp2(real const &x)
    872872{
    873873    /* Strategy for exp2(x): see strategy in exp(). */
     
    879879}
    880880
    881 real sinh(real const &x)
     881template<> real sinh(real const &x)
    882882{
    883883    /* We cannot always use (exp(x)-exp(-x))/2 because we'll lose
     
    890890}
    891891
    892 real tanh(real const &x)
     892template<> real tanh(real const &x)
    893893{
    894894    /* See sinh() for the strategy here */
     
    900900}
    901901
    902 real cosh(real const &x)
     902template<> real cosh(real const &x)
    903903{
    904904    /* No need to worry about accuracy here; maybe the last bit is slightly
     
    907907}
    908908
    909 real frexp(real const &x, int *exp)
     909template<> real frexp(real const &x, int *exp)
    910910{
    911911    if (!x)
     
    922922}
    923923
    924 real ldexp(real const &x, int exp)
     924template<> real ldexp(real const &x, int exp)
    925925{
    926926    real ret = x;
     
    930930}
    931931
    932 real modf(real const &x, real *iptr)
     932template<> real modf(real const &x, real *iptr)
    933933{
    934934    real absx = fabs(x);
     
    939939}
    940940
    941 real ulp(real const &x)
     941template<> real ulp(real const &x)
    942942{
    943943    real ret = real::R_1;
     
    949949}
    950950
    951 real nextafter(real const &x, real const &y)
     951template<> real nextafter(real const &x, real const &y)
    952952{
    953953    if (x == y)
     
    959959}
    960960
    961 real copysign(real const &x, real const &y)
     961template<> real copysign(real const &x, real const &y)
    962962{
    963963    real ret = x;
     
    967967}
    968968
    969 real floor(real const &x)
     969template<> real floor(real const &x)
    970970{
    971971    /* Strategy for floor(x):
     
    998998}
    999999
    1000 real ceil(real const &x)
     1000template<> real ceil(real const &x)
    10011001{
    10021002    /* Strategy for ceil(x):
     
    10131013}
    10141014
    1015 real round(real const &x)
     1015template<> real round(real const &x)
    10161016{
    10171017    if (x < real::R_0)
     
    10211021}
    10221022
    1023 real fmod(real const &x, real const &y)
     1023template<> real fmod(real const &x, real const &y)
    10241024{
    10251025    if (!y)
     
    10331033}
    10341034
    1035 real sin(real const &x)
     1035template<> real sin(real const &x)
    10361036{
    10371037    int switch_sign = x.m_signexp & 0x80000000u;
     
    10661066}
    10671067
    1068 real cos(real const &x)
     1068template<> real cos(real const &x)
    10691069{
    10701070    return sin(real::R_PI_2 - x);
    10711071}
    10721072
    1073 real tan(real const &x)
     1073template<> real tan(real const &x)
    10741074{
    10751075    /* Constrain input to [-π,π] */
     
    11381138}
    11391139
    1140 real asin(real const &x)
     1140template<> real asin(real const &x)
    11411141{
    11421142    return asinacos(x, 1, x.m_signexp >> 31);
    11431143}
    11441144
    1145 real acos(real const &x)
     1145template<> real acos(real const &x)
    11461146{
    11471147    return asinacos(x, 0, x.m_signexp >> 31);
    11481148}
    11491149
    1150 real atan(real const &x)
     1150template<> real atan(real const &x)
    11511151{
    11521152    /* Computing atan(x): we choose a different Taylor series depending on
     
    12511251}
    12521252
    1253 real atan2(real const &y, real const &x)
     1253template<> real atan2(real const &y, real const &x)
    12541254{
    12551255    if (!y)
     
    12771277}
    12781278
    1279 void real::hexprint() const
     1279template<> void real::hexprint() const
    12801280{
    12811281    printf("%08x", m_signexp);
     
    12851285}
    12861286
    1287 void real::print(int ndigits) const
     1287template<> void real::print(int ndigits) const
    12881288{
    12891289    real x = *this;
     
    13511351}
    13521352
    1353 real const real::R_0        = (real)0.0;
    1354 real const real::R_1        = (real)1.0;
    1355 real const real::R_2        = (real)2.0;
    1356 real const real::R_3        = (real)3.0;
    1357 real const real::R_10       = (real)10.0;
     1353template<> real const real::R_0        = (real)0.0;
     1354template<> real const real::R_1        = (real)1.0;
     1355template<> real const real::R_2        = (real)2.0;
     1356template<> real const real::R_3        = (real)3.0;
     1357template<> real const real::R_10       = (real)10.0;
    13581358
    13591359/*
     
    13651365 *  - sqrt() requires R_3
    13661366 */
    1367 real const real::R_LN2      = fast_log(R_2);
    1368 real const real::R_LN10     = log(R_10);
    1369 real const real::R_LOG2E    = re(R_LN2);
    1370 real const real::R_LOG10E   = re(R_LN10);
    1371 real const real::R_E        = exp(R_1);
    1372 real const real::R_PI       = fast_pi();
    1373 real const real::R_PI_2     = R_PI / 2;
    1374 real const real::R_PI_3     = R_PI / R_3;
    1375 real const real::R_PI_4     = R_PI / 4;
    1376 real const real::R_1_PI     = re(R_PI);
    1377 real const real::R_2_PI     = R_1_PI * 2;
    1378 real const real::R_2_SQRTPI = re(sqrt(R_PI)) * 2;
    1379 real const real::R_SQRT2    = sqrt(R_2);
    1380 real const real::R_SQRT3    = sqrt(R_3);
    1381 real const real::R_SQRT1_2  = R_SQRT2 / 2;
     1367template<> real const real::R_LN2      = fast_log(R_2);
     1368template<> real const real::R_LN10     = log(R_10);
     1369template<> real const real::R_LOG2E    = re(R_LN2);
     1370template<> real const real::R_LOG10E   = re(R_LN10);
     1371template<> real const real::R_E        = exp(R_1);
     1372template<> real const real::R_PI       = fast_pi();
     1373template<> real const real::R_PI_2     = R_PI / 2;
     1374template<> real const real::R_PI_3     = R_PI / R_3;
     1375template<> real const real::R_PI_4     = R_PI / 4;
     1376template<> real const real::R_1_PI     = re(R_PI);
     1377template<> real const real::R_2_PI     = R_1_PI * 2;
     1378template<> real const real::R_2_SQRTPI = re(sqrt(R_PI)) * 2;
     1379template<> real const real::R_SQRT2    = sqrt(R_2);
     1380template<> real const real::R_SQRT3    = sqrt(R_3);
     1381template<> real const real::R_SQRT1_2  = R_SQRT2 / 2;
    13821382
    13831383} /* namespace lol */
  • trunk/test/unit/trig.cpp

    r1146 r1163  
    2525    LOLUNIT_TEST(Sin)
    2626    {
     27        using std::fabs;
     28
    2729        for (int i = -10000; i < 10000; i++)
    2830        {
     
    3537            double b = lol_sin(f);
    3638            LOLUNIT_SET_CONTEXT(f);
    37             LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     39            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    3840        }
    3941
     
    4850            double b = lol_sin(f);
    4951            LOLUNIT_SET_CONTEXT(f);
    50             LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     52            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    5153        }
    5254    }
     
    5456    LOLUNIT_TEST(Cos)
    5557    {
     58        using std::fabs;
     59
    5660        for (int i = -10000; i < 10000; i++)
    5761        {
     
    6468            double b = lol_cos(f);
    6569            LOLUNIT_SET_CONTEXT(f);
    66             LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     70            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    6771        }
    6872
     
    7781            double b = lol_cos(f);
    7882            LOLUNIT_SET_CONTEXT(f);
    79             LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     83            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    8084        }
    8185    }
     
    8387    LOLUNIT_TEST(SinCos)
    8488    {
     89        using std::fabs;
     90
    8591        for (int i = -10000; i < 10000; i++)
    8692        {
     
    96102            lol_sincos(f, &b1, &b2);
    97103            LOLUNIT_SET_CONTEXT(f);
    98             LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, std::fabs(f) * 1e-11);
    99             LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, std::fabs(f) * 1e-11);
     104            LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
     105            LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
    100106        }
    101107
     
    113119            lol_sincos(f, &b1, &b2);
    114120            LOLUNIT_SET_CONTEXT(f);
    115             LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, std::fabs(f) * 1e-11);
    116             LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, std::fabs(f) * 1e-11);
     121            LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
     122            LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
    117123        }
    118124    }
     
    120126    LOLUNIT_TEST(Tan)
    121127    {
     128        using std::fabs;
     129
    122130        for (int i = -100000; i < 100000; i++)
    123131        {
     
    130138            double b = lol_tan(f);
    131139            LOLUNIT_SET_CONTEXT(f);
    132             if (std::fabs(a) > 1e4)
    133                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * std::fabs(a) * 1e-11);
    134             else if (std::fabs(a) > 1.0)
    135                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * 1e-11);
     140            if (fabs(a) > 1e4)
     141                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
     142            else if (fabs(a) > 1.0)
     143                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
    136144            else
    137                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     145                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    138146        }
    139147
     
    148156            double b = lol_tan(f);
    149157            LOLUNIT_SET_CONTEXT(f);
    150             if (std::fabs(a) > 1e4)
    151                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * std::fabs(a) * 1e-11);
    152             else if (std::fabs(a) > 1.0)
    153                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * 1e-11);
     158            if (fabs(a) > 1e4)
     159                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
     160            else if (fabs(a) > 1.0)
     161                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
    154162            else
    155                 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
     163                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
    156164        }
    157165    }
Note: See TracChangeset for help on using the changeset viewer.