Changeset 1017
- Timestamp:
- Oct 12, 2011, 12:01:44 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/real.cpp
r1016 r1017 23 23 { 24 24 25 static int const BIGIT_BITS = 32;26 27 25 real::real(float f) { *this = (double)f; } 28 26 real::real(int i) { *this = (double)i; } … … 501 499 ret.m_signexp |= (exponent - 1) & 0x7fffffffu; 502 500 503 /* FIXME: log2(BIGITS) steps of Newton-Raphson seems to be enough for501 /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for 504 502 * convergence, but this hasn't been checked seriously. */ 505 for (int i = 2; i <real::BIGITS; i *= 2)503 for (int i = 1; i <= real::BIGITS; i *= 2) 506 504 ret = ret * (real::R_2 - ret * x); 507 505 … … 545 543 ret.m_signexp |= exponent & 0x7fffffffu; 546 544 547 /* FIXME: log2(BIGITS) steps of Newton-Raphson seems to be enough for545 /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for 548 546 * convergence, but this hasn't been checked seriously. */ 549 for (int i = 2; i <real::BIGITS; i *= 2)547 for (int i = 1; i <= real::BIGITS; i *= 2) 550 548 { 551 549 ret = ret * (real::R_3 - ret * ret * x); … … 669 667 if (exponent <= 0) 670 668 ret.m_mantissa[i] = 0; 671 else if (exponent < BIGIT_BITS)672 ret.m_mantissa[i] &= ~((1 << ( BIGIT_BITS - exponent)) - 1);673 674 exponent -= BIGIT_BITS;669 else if (exponent < real::BIGIT_BITS) 670 ret.m_mantissa[i] &= ~((1 << (real::BIGIT_BITS - exponent)) - 1); 671 672 exponent -= real::BIGIT_BITS; 675 673 } 676 674 -
trunk/src/real.h
r1016 r1017 105 105 static real const R_SQRT1_2; 106 106 107 private:108 107 /* XXX: changing this requires tuning real::fres (the number of 109 108 * Newton-Raphson iterations) and real::print (the number of printed 110 109 * digits) */ 111 110 static int const BIGITS = 16; 111 static int const BIGIT_BITS = 32; 112 112 113 private: 113 114 uint32_t m_size; 114 115 uint32_t m_signexp; -
trunk/test/unit/real.cpp
r1003 r1017 181 181 } 182 182 183 LOLUNIT_TEST(Division) 184 { 185 real a1(1.0f); 186 real a2(2.0f); 187 188 float m1 = a1 / a1; 189 float m2 = a2 / a1; 190 float m3 = a1 / a2; 191 float m4 = a2 / a2; 192 float m5 = a1 / -a2; 183 LOLUNIT_TEST(ExactDivision) 184 { 185 float m1 = real::R_1 / real::R_1; 186 float m2 = real::R_2 / real::R_1; 187 float m3 = real::R_1 / real::R_2; 188 float m4 = real::R_2 / real::R_2; 189 float m5 = real::R_1 / -real::R_2; 193 190 194 191 LOLUNIT_ASSERT_EQUAL(m1, 1.0f); … … 197 194 LOLUNIT_ASSERT_EQUAL(m4, 1.0f); 198 195 LOLUNIT_ASSERT_EQUAL(m5, -0.5f); 196 } 197 198 LOLUNIT_TEST(InexactDivision) 199 { 200 /* 1 / 3 * 3 should be close to 1... check that it does not differ 201 * by more than 2^-k where k is the number of bits in the mantissa. */ 202 real a = real::R_1 / real::R_3 * real::R_3; 203 real b = (real::R_1 - a) << (real::BIGITS * real::BIGIT_BITS); 204 205 LOLUNIT_ASSERT_LEQUAL((double)fabs(b), 1.0); 199 206 } 200 207
Note: See TracChangeset
for help on using the changeset viewer.