Changeset 1163
- Timestamp:
- Mar 14, 2012, 8:19:48 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/debug/sphere.cpp
r1146 r1163 153 153 154 154 int const ndiv = 2; 155 using std::log; 155 156 int const ntriangles = 20 * (1 << (ndiv * 2)) 156 157 * (int)(log(1.0f / 0.01f) / log(1.1f) + 0.9999f); -
trunk/src/lol/math/real.h
r1130 r1163 25 25 { 26 26 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 */ 33 template<int N> class Real 28 34 { 29 35 public: 30 real();31 real(realconst &x);32 real const &operator =(realconst &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); 41 47 42 48 operator float() const; … … 45 51 operator unsigned int() const; 46 52 47 realoperator +() const;48 realoperator -() const;49 real operator +(realconst &x) const;50 real operator -(realconst &x) const;51 real operator *(realconst &x) const;52 real operator /(realconst &x) const;53 real const &operator +=(realconst &x);54 real const &operator -=(realconst &x);55 real const &operator *=(realconst &x);56 real const &operator /=(realconst &x);57 58 bool operator ==( realconst &x) const;59 bool operator !=( realconst &x) const;60 bool operator <( realconst &x) const;61 bool operator >( realconst &x) const;62 bool operator <=( realconst &x) const;63 bool operator >=( realconst &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; 64 70 65 71 bool operator !() const; … … 67 73 68 74 /* Trigonometric functions */ 69 friend real sin(realconst &x);70 friend real cos(realconst &x);71 friend real tan(realconst &x);72 friend real asin(realconst &x);73 friend real acos(realconst &x);74 friend real atan(realconst &x);75 friend real atan2(real const &y, realconst &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); 76 82 77 83 /* Hyperbolic functions */ 78 friend real sinh(realconst &x);79 friend real cosh(realconst &x);80 friend real tanh(realconst &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); 81 87 82 88 /* Exponential and logarithmic functions */ 83 friend real exp(realconst &x);84 friend real exp2(realconst &x);85 friend real log(realconst &x);86 friend real log2(realconst &x);87 friend real log10(realconst &x);88 friend real frexp(realconst &x, int *exp);89 friend real ldexp(realconst &x, int exp);90 friend real modf(real const &x, real*iptr);91 friend real ulp(realconst &x);92 friend real nextafter(real const &x, realconst &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); 93 99 94 100 /* Power functions */ 95 friend real re(realconst &x);96 friend real sqrt(realconst &x);97 friend real cbrt(realconst &x);98 friend real pow(real const &x, realconst &y);99 friend real gamma(realconst &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); 100 106 101 107 /* Rounding, absolute value, remainder etc. */ 102 friend real ceil(realconst &x);103 friend real copysign(real const &x, realconst &y);104 friend real floor(realconst &x);105 friend real fabs(realconst &x);106 friend real round(realconst &x);107 friend real fmod(real const &x, realconst &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); 108 114 109 115 void hexprint() const; … … 112 118 /* Additional operators using base C++ types */ 113 119 #define __LOL_REAL_OP_HELPER_GENERIC(op, type) \ 114 inline real operator op(type x) const { return *this op (real)x; } \115 inline realconst &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); } 116 122 #define __LOL_REAL_OP_HELPER_FASTMULDIV(op, type) \ 117 inline realoperator op(type x) const \123 inline Real<N> operator op(type x) const \ 118 124 { \ 119 realtmp = *this; return tmp op##= x; \125 Real<N> tmp = *this; return tmp op##= x; \ 120 126 } \ 121 inline realconst &operator op##=(type x) \127 inline Real<N> const &operator op##=(type x) \ 122 128 { \ 123 129 /* If multiplying or dividing by a power of two, take a shortcut */ \ … … 128 134 } \ 129 135 else \ 130 *this = *this op ( real)x; \136 *this = *this op (Real<N>)x; \ 131 137 return *this; \ 132 138 } … … 148 154 149 155 /* Constants */ 150 static realconst R_0;151 static realconst R_1;152 static realconst R_2;153 static realconst R_3;154 static realconst R_10;155 156 static realconst R_E;157 static realconst R_LOG2E;158 static realconst R_LOG10E;159 static realconst R_LN2;160 static realconst R_LN10;161 static realconst R_PI;162 static realconst R_PI_2;163 static realconst R_PI_3;164 static realconst R_PI_4;165 static realconst R_1_PI;166 static realconst R_2_PI;167 static realconst R_2_SQRTPI;168 static realconst R_SQRT2;169 static realconst R_SQRT3;170 static realconst 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; 171 177 172 178 /* XXX: changing this requires tuning real::fres (the number of 173 179 * Newton-Raphson iterations) and real::print (the number of printed 174 180 * digits) */ 175 static int const BIGITS = 16;181 static int const BIGITS = N; 176 182 static int const BIGIT_BITS = 32; 177 183 … … 181 187 }; 182 188 189 /* 190 * The real type used for real numbers 191 */ 192 typedef Real<16> real; 193 194 /* 195 * Mandatory forward declarations of template specialisations 196 */ 197 template<> real::Real(); 198 template<> real::Real(real const &x); 199 template<> real const &real::operator =(real const &x); 200 template<> real::~Real(); 201 template<> real::Real(float f); 202 template<> real::Real(double f); 203 template<> real::Real(int i); 204 template<> real::Real(unsigned int i); 205 template<> real::Real(char const *str); 206 207 template<> real::operator float() const; 208 template<> real::operator double() const; 209 template<> real::operator int() const; 210 template<> real::operator unsigned int() const; 211 template<> real real::operator +() const; 212 template<> real real::operator -() const; 213 template<> real real::operator +(real const &x) const; 214 template<> real real::operator -(real const &x) const; 215 template<> real real::operator *(real const &x) const; 216 template<> real real::operator /(real const &x) const; 217 template<> real const &real::operator +=(real const &x); 218 template<> real const &real::operator -=(real const &x); 219 template<> real const &real::operator *=(real const &x); 220 template<> real const &real::operator /=(real const &x); 221 template<> bool real::operator ==(real const &x) const; 222 template<> bool real::operator !=(real const &x) const; 223 template<> bool real::operator <(real const &x) const; 224 template<> bool real::operator >(real const &x) const; 225 template<> bool real::operator <=(real const &x) const; 226 template<> bool real::operator >=(real const &x) const; 227 template<> bool real::operator !() const; 228 template<> real::operator bool() const; 229 230 template<> real sin(real const &x); 231 template<> real cos(real const &x); 232 template<> real tan(real const &x); 233 template<> real asin(real const &x); 234 template<> real acos(real const &x); 235 template<> real atan(real const &x); 236 template<> real atan2(real const &y, real const &x); 237 template<> real sinh(real const &x); 238 template<> real cosh(real const &x); 239 template<> real tanh(real const &x); 240 template<> real exp(real const &x); 241 template<> real exp2(real const &x); 242 template<> real log(real const &x); 243 template<> real log2(real const &x); 244 template<> real log10(real const &x); 245 template<> real frexp(real const &x, int *exp); 246 template<> real ldexp(real const &x, int exp); 247 template<> real modf(real const &x, real *iptr); 248 template<> real ulp(real const &x); 249 template<> real nextafter(real const &x, real const &y); 250 template<> real re(real const &x); 251 template<> real sqrt(real const &x); 252 template<> real cbrt(real const &x); 253 template<> real pow(real const &x, real const &y); 254 template<> real gamma(real const &x); 255 template<> real ceil(real const &x); 256 template<> real copysign(real const &x, real const &y); 257 template<> real floor(real const &x); 258 template<> real fabs(real const &x); 259 template<> real round(real const &x); 260 template<> real fmod(real const &x, real const &y); 261 262 template<> void real::hexprint() const; 263 template<> void real::print(int ndigits) const; 264 183 265 } /* namespace lol */ 184 266 -
trunk/src/lol/math/vector.h
r1161 r1163 1048 1048 inline double len(tname<type> const &a) \ 1049 1049 { \ 1050 using namespace std; \1050 using std::sqrt; \ 1051 1051 return sqrt((double)sqlen(a)); \ 1052 1052 } \ -
trunk/src/lol/unit.h
r937 r1163 270 270 do { \ 271 271 m_asserts++; \ 272 using std::fabs; \ 272 273 if (True() && fabs((a) - (b)) > fabs((t))) \ 273 274 { \ -
trunk/src/real.cpp
r1130 r1163 34 34 { 35 35 36 real::real()36 template<> real::Real() 37 37 { 38 38 m_mantissa = new uint32_t[BIGITS]; … … 40 40 } 41 41 42 real::real(real const &x)42 template<> real::Real(real const &x) 43 43 { 44 44 m_mantissa = new uint32_t[BIGITS]; … … 47 47 } 48 48 49 real const &real::operator =(real const &x)49 template<> real const &real::operator =(real const &x) 50 50 { 51 51 if (&x != this) … … 58 58 } 59 59 60 real::~real()60 template<> real::~Real() 61 61 { 62 62 delete[] m_mantissa; 63 63 } 64 64 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)65 template<> real::Real(float f) { new(this) real((double)f); } 66 template<> real::Real(int i) { new(this) real((double)i); } 67 template<> real::Real(unsigned int i) { new(this) real((double)i); } 68 69 template<> real::Real(double d) 70 70 { 71 71 new(this) real(); … … 94 94 } 95 95 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() const96 template<> real::operator float() const { return (float)(double)(*this); } 97 template<> real::operator int() const { return (int)(double)(*this); } 98 template<> real::operator unsigned() const { return (unsigned)(double)(*this); } 99 100 template<> real::operator double() const 101 101 { 102 102 union { double d; uint64_t x; } u; … … 136 136 * Create a real number from an ASCII representation 137 137 */ 138 real::real(char const *str)138 template<> real::Real(char const *str) 139 139 { 140 140 real ret = 0; … … 193 193 } 194 194 195 real real::operator +() const195 template<> real real::operator +() const 196 196 { 197 197 return *this; 198 198 } 199 199 200 real real::operator -() const200 template<> real real::operator -() const 201 201 { 202 202 real ret = *this; … … 205 205 } 206 206 207 real real::operator +(real const &x) const207 template<> real real::operator +(real const &x) const 208 208 { 209 209 if (x.m_signexp << 1 == 0) … … 269 269 } 270 270 271 real real::operator -(real const &x) const271 template<> real real::operator -(real const &x) const 272 272 { 273 273 if (x.m_signexp << 1 == 0) … … 373 373 } 374 374 375 real real::operator *(real const &x) const375 template<> real real::operator *(real const &x) const 376 376 { 377 377 real ret; … … 446 446 } 447 447 448 real real::operator /(real const &x) const448 template<> real real::operator /(real const &x) const 449 449 { 450 450 return *this * re(x); 451 451 } 452 452 453 real const &real::operator +=(real const &x)453 template<> real const &real::operator +=(real const &x) 454 454 { 455 455 real tmp = *this; … … 457 457 } 458 458 459 real const &real::operator -=(real const &x)459 template<> real const &real::operator -=(real const &x) 460 460 { 461 461 real tmp = *this; … … 463 463 } 464 464 465 real const &real::operator *=(real const &x)465 template<> real const &real::operator *=(real const &x) 466 466 { 467 467 real tmp = *this; … … 469 469 } 470 470 471 real const &real::operator /=(real const &x)471 template<> real const &real::operator /=(real const &x) 472 472 { 473 473 real tmp = *this; … … 475 475 } 476 476 477 bool real::operator ==(real const &x) const477 template<> bool real::operator ==(real const &x) const 478 478 { 479 479 if ((m_signexp << 1) == 0 && (x.m_signexp << 1) == 0) … … 486 486 } 487 487 488 bool real::operator !=(real const &x) const488 template<> bool real::operator !=(real const &x) const 489 489 { 490 490 return !(*this == x); 491 491 } 492 492 493 bool real::operator <(real const &x) const493 template<> bool real::operator <(real const &x) const 494 494 { 495 495 /* Ensure both numbers are positive */ … … 511 511 } 512 512 513 bool real::operator <=(real const &x) const513 template<> bool real::operator <=(real const &x) const 514 514 { 515 515 return !(*this > x); 516 516 } 517 517 518 bool real::operator >(real const &x) const518 template<> bool real::operator >(real const &x) const 519 519 { 520 520 /* Ensure both numbers are positive */ … … 536 536 } 537 537 538 bool real::operator >=(real const &x) const538 template<> bool real::operator >=(real const &x) const 539 539 { 540 540 return !(*this < x); 541 541 } 542 542 543 bool real::operator !() const543 template<> bool real::operator !() const 544 544 { 545 545 return !(bool)*this; 546 546 } 547 547 548 real::operator bool() const548 template<> real::operator bool() const 549 549 { 550 550 /* A real is "true" if it is non-zero (exponent is non-zero) AND … … 554 554 } 555 555 556 real re(real const &x)556 template<> real re(real const &x) 557 557 { 558 558 if (!(x.m_signexp << 1)) … … 587 587 } 588 588 589 real sqrt(real const &x)589 template<> real sqrt(real const &x) 590 590 { 591 591 /* if zero, return x */ … … 634 634 } 635 635 636 real cbrt(real const &x)636 template<> real cbrt(real const &x) 637 637 { 638 638 /* if zero, return x */ … … 671 671 } 672 672 673 real pow(real const &x, real const &y)673 template<> real pow(real const &x, real const &y) 674 674 { 675 675 if (!y) … … 720 720 } 721 721 722 real gamma(real const &x)722 template<> real gamma(real const &x) 723 723 { 724 724 /* We use Spouge's formula. FIXME: precision is far from acceptable, … … 747 747 } 748 748 749 real fabs(real const &x)749 template<> real fabs(real const &x) 750 750 { 751 751 real ret = x; … … 787 787 } 788 788 789 real log(real const &x)789 template<> real log(real const &x) 790 790 { 791 791 /* Strategy for log(x): if x = 2^E*M then log(x) = E log(2) + log(M), … … 803 803 } 804 804 805 real log2(real const &x)805 template<> real log2(real const &x) 806 806 { 807 807 /* Strategy for log2(x): see log(x). */ … … 818 818 } 819 819 820 real log10(real const &x)820 template<> real log10(real const &x) 821 821 { 822 822 return log(x) * real::R_LOG10E; … … 844 844 } 845 845 846 real exp(real const &x)846 template<> real exp(real const &x) 847 847 { 848 848 /* Strategy for exp(x): the Taylor series does not converge very fast … … 869 869 } 870 870 871 real exp2(real const &x)871 template<> real exp2(real const &x) 872 872 { 873 873 /* Strategy for exp2(x): see strategy in exp(). */ … … 879 879 } 880 880 881 real sinh(real const &x)881 template<> real sinh(real const &x) 882 882 { 883 883 /* We cannot always use (exp(x)-exp(-x))/2 because we'll lose … … 890 890 } 891 891 892 real tanh(real const &x)892 template<> real tanh(real const &x) 893 893 { 894 894 /* See sinh() for the strategy here */ … … 900 900 } 901 901 902 real cosh(real const &x)902 template<> real cosh(real const &x) 903 903 { 904 904 /* No need to worry about accuracy here; maybe the last bit is slightly … … 907 907 } 908 908 909 real frexp(real const &x, int *exp)909 template<> real frexp(real const &x, int *exp) 910 910 { 911 911 if (!x) … … 922 922 } 923 923 924 real ldexp(real const &x, int exp)924 template<> real ldexp(real const &x, int exp) 925 925 { 926 926 real ret = x; … … 930 930 } 931 931 932 real modf(real const &x, real *iptr)932 template<> real modf(real const &x, real *iptr) 933 933 { 934 934 real absx = fabs(x); … … 939 939 } 940 940 941 real ulp(real const &x)941 template<> real ulp(real const &x) 942 942 { 943 943 real ret = real::R_1; … … 949 949 } 950 950 951 real nextafter(real const &x, real const &y)951 template<> real nextafter(real const &x, real const &y) 952 952 { 953 953 if (x == y) … … 959 959 } 960 960 961 real copysign(real const &x, real const &y)961 template<> real copysign(real const &x, real const &y) 962 962 { 963 963 real ret = x; … … 967 967 } 968 968 969 real floor(real const &x)969 template<> real floor(real const &x) 970 970 { 971 971 /* Strategy for floor(x): … … 998 998 } 999 999 1000 real ceil(real const &x)1000 template<> real ceil(real const &x) 1001 1001 { 1002 1002 /* Strategy for ceil(x): … … 1013 1013 } 1014 1014 1015 real round(real const &x)1015 template<> real round(real const &x) 1016 1016 { 1017 1017 if (x < real::R_0) … … 1021 1021 } 1022 1022 1023 real fmod(real const &x, real const &y)1023 template<> real fmod(real const &x, real const &y) 1024 1024 { 1025 1025 if (!y) … … 1033 1033 } 1034 1034 1035 real sin(real const &x)1035 template<> real sin(real const &x) 1036 1036 { 1037 1037 int switch_sign = x.m_signexp & 0x80000000u; … … 1066 1066 } 1067 1067 1068 real cos(real const &x)1068 template<> real cos(real const &x) 1069 1069 { 1070 1070 return sin(real::R_PI_2 - x); 1071 1071 } 1072 1072 1073 real tan(real const &x)1073 template<> real tan(real const &x) 1074 1074 { 1075 1075 /* Constrain input to [-π,π] */ … … 1138 1138 } 1139 1139 1140 real asin(real const &x)1140 template<> real asin(real const &x) 1141 1141 { 1142 1142 return asinacos(x, 1, x.m_signexp >> 31); 1143 1143 } 1144 1144 1145 real acos(real const &x)1145 template<> real acos(real const &x) 1146 1146 { 1147 1147 return asinacos(x, 0, x.m_signexp >> 31); 1148 1148 } 1149 1149 1150 real atan(real const &x)1150 template<> real atan(real const &x) 1151 1151 { 1152 1152 /* Computing atan(x): we choose a different Taylor series depending on … … 1251 1251 } 1252 1252 1253 real atan2(real const &y, real const &x)1253 template<> real atan2(real const &y, real const &x) 1254 1254 { 1255 1255 if (!y) … … 1277 1277 } 1278 1278 1279 void real::hexprint() const1279 template<> void real::hexprint() const 1280 1280 { 1281 1281 printf("%08x", m_signexp); … … 1285 1285 } 1286 1286 1287 void real::print(int ndigits) const1287 template<> void real::print(int ndigits) const 1288 1288 { 1289 1289 real x = *this; … … 1351 1351 } 1352 1352 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;1353 template<> real const real::R_0 = (real)0.0; 1354 template<> real const real::R_1 = (real)1.0; 1355 template<> real const real::R_2 = (real)2.0; 1356 template<> real const real::R_3 = (real)3.0; 1357 template<> real const real::R_10 = (real)10.0; 1358 1358 1359 1359 /* … … 1365 1365 * - sqrt() requires R_3 1366 1366 */ 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;1367 template<> real const real::R_LN2 = fast_log(R_2); 1368 template<> real const real::R_LN10 = log(R_10); 1369 template<> real const real::R_LOG2E = re(R_LN2); 1370 template<> real const real::R_LOG10E = re(R_LN10); 1371 template<> real const real::R_E = exp(R_1); 1372 template<> real const real::R_PI = fast_pi(); 1373 template<> real const real::R_PI_2 = R_PI / 2; 1374 template<> real const real::R_PI_3 = R_PI / R_3; 1375 template<> real const real::R_PI_4 = R_PI / 4; 1376 template<> real const real::R_1_PI = re(R_PI); 1377 template<> real const real::R_2_PI = R_1_PI * 2; 1378 template<> real const real::R_2_SQRTPI = re(sqrt(R_PI)) * 2; 1379 template<> real const real::R_SQRT2 = sqrt(R_2); 1380 template<> real const real::R_SQRT3 = sqrt(R_3); 1381 template<> real const real::R_SQRT1_2 = R_SQRT2 / 2; 1382 1382 1383 1383 } /* namespace lol */ -
trunk/test/unit/trig.cpp
r1146 r1163 25 25 LOLUNIT_TEST(Sin) 26 26 { 27 using std::fabs; 28 27 29 for (int i = -10000; i < 10000; i++) 28 30 { … … 35 37 double b = lol_sin(f); 36 38 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); 38 40 } 39 41 … … 48 50 double b = lol_sin(f); 49 51 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); 51 53 } 52 54 } … … 54 56 LOLUNIT_TEST(Cos) 55 57 { 58 using std::fabs; 59 56 60 for (int i = -10000; i < 10000; i++) 57 61 { … … 64 68 double b = lol_cos(f); 65 69 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); 67 71 } 68 72 … … 77 81 double b = lol_cos(f); 78 82 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); 80 84 } 81 85 } … … 83 87 LOLUNIT_TEST(SinCos) 84 88 { 89 using std::fabs; 90 85 91 for (int i = -10000; i < 10000; i++) 86 92 { … … 96 102 lol_sincos(f, &b1, &b2); 97 103 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); 100 106 } 101 107 … … 113 119 lol_sincos(f, &b1, &b2); 114 120 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); 117 123 } 118 124 } … … 120 126 LOLUNIT_TEST(Tan) 121 127 { 128 using std::fabs; 129 122 130 for (int i = -100000; i < 100000; i++) 123 131 { … … 130 138 double b = lol_tan(f); 131 139 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); 136 144 else 137 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);145 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); 138 146 } 139 147 … … 148 156 double b = lol_tan(f); 149 157 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); 154 162 else 155 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);163 LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); 156 164 } 157 165 }
Note: See TracChangeset
for help on using the changeset viewer.