diff --git a/src/real.cpp b/src/real.cpp index 19910139..a65ec97d 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -323,7 +323,7 @@ real real::operator *(real const &x) const real real::operator /(real const &x) const { - return *this * fres(x); + return *this * re(x); } real &real::operator +=(real const &x) @@ -455,7 +455,7 @@ real::operator bool() const return exponent && (~exponent || m_mantissa[0] == 0); } -real fres(real const &x) +real re(real const &x) { if (!(x.m_signexp << 1)) { @@ -741,18 +741,23 @@ static real fast_pi() return ret; } -real const real::R_E = exp((real)1.0); -real const real::R_LOG2E = (real)1.0 / R_LN2; -real const real::R_LOG10E = (real)1.0 / R_LN10; -real const real::R_LN2 = log((real)2.0); -real const real::R_LN10 = log((real)10.0); +real const real::R_0 = (real)0.0; +real const real::R_1 = (real)1.0; +real const real::R_2 = (real)2.0; +real const real::R_10 = (real)10.0; + +real const real::R_E = exp(R_1); +real const real::R_LN2 = log(R_2); +real const real::R_LN10 = log(R_10); +real const real::R_LOG2E = re(R_LN2); +real const real::R_LOG10E = re(R_LN10); real const real::R_PI = fast_pi(); real const real::R_PI_2 = R_PI >> 1; real const real::R_PI_4 = R_PI >> 2; -real const real::R_1_PI = (real)1.0 / R_PI; +real const real::R_1_PI = re(R_PI); real const real::R_2_PI = R_1_PI << 1; -real const real::R_2_SQRTPI = (real)2.0 / sqrt(R_PI); -real const real::R_SQRT2 = sqrt((real)2.0); +real const real::R_2_SQRTPI = re(sqrt(R_PI)) << 1; +real const real::R_SQRT2 = sqrt(R_2); real const real::R_SQRT1_2 = R_SQRT2 >> 1; } /* namespace lol */ diff --git a/src/real.h b/src/real.h index 02907b42..88d943ba 100644 --- a/src/real.h +++ b/src/real.h @@ -63,7 +63,7 @@ public: friend real fabs(real const &x); - friend real fres(real const &x); + friend real re(real const &x); friend real sqrt(real const &x); friend real log(real const &x); friend real exp(real const &x); @@ -73,6 +73,11 @@ public: void print(int ndigits = 150) const; + static real const R_0; + static real const R_1; + static real const R_2; + static real const R_10; + static real const R_E; static real const R_LOG2E; static real const R_LOG10E; diff --git a/test/unit/real.cpp b/test/unit/real.cpp index b635cb15..8753e7fc 100644 --- a/test/unit/real.cpp +++ b/test/unit/real.cpp @@ -22,6 +22,49 @@ namespace lol LOLUNIT_FIXTURE(RealTest) { + LOLUNIT_TEST(Constants) + { + double a0 = real::R_0; + double a1 = real::R_1; + double a2 = real::R_2; + double a10 = real::R_10; + + LOLUNIT_ASSERT_EQUAL(a0, 0.0); + LOLUNIT_ASSERT_EQUAL(a1, 1.0); + LOLUNIT_ASSERT_EQUAL(a2, 2.0); + LOLUNIT_ASSERT_EQUAL(a10, 10.0); + + double b0 = log(real::R_E); + LOLUNIT_ASSERT_EQUAL(b0, 1.0); + + double b1 = exp(re(real::R_LOG2E)); + LOLUNIT_ASSERT_EQUAL(b1, 2.0); + + double b2 = exp(re(real::R_LOG10E)); + LOLUNIT_ASSERT_EQUAL(b2, 10.0); + + double b3 = exp(real::R_LN2); + LOLUNIT_ASSERT_EQUAL(b3, 2.0); + + double b4 = exp(real::R_LN10); + LOLUNIT_ASSERT_EQUAL(b4, 10.0); + + double b5 = sin(real::R_PI); + double b6 = cos(real::R_PI); + LOLUNIT_ASSERT_DOUBLES_EQUAL(b5, 0.0, 1e-100); + LOLUNIT_ASSERT_EQUAL(b6, -1.0); + + double b7 = sin(real::R_PI_2); + double b8 = cos(real::R_PI_2); + LOLUNIT_ASSERT_EQUAL(b7, 1.0); + LOLUNIT_ASSERT_DOUBLES_EQUAL(b8, 0.0, 1e-100); + + double b9 = sin(real::R_PI_4) * sin(real::R_PI_4); + double b10 = cos(real::R_PI_4) * cos(real::R_PI_4); + LOLUNIT_ASSERT_EQUAL(b9, 0.5); + LOLUNIT_ASSERT_EQUAL(b10, 0.5); + } + LOLUNIT_TEST(FloatToReal) { float a1 = real(0.0f);