Explorar el Código

core: add real constants for 0, 1, 2 and 10, and reorder static constants

so that their initialisation order ensures they have the proper values. Add
unit tests for these constants.
legacy
Sam Hocevar sam hace 13 años
padre
commit
bbd75b80eb
Se han modificado 3 ficheros con 64 adiciones y 11 borrados
  1. +15
    -10
      src/real.cpp
  2. +6
    -1
      src/real.h
  3. +43
    -0
      test/unit/real.cpp

+ 15
- 10
src/real.cpp Ver fichero

@@ -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 */


+ 6
- 1
src/real.h Ver fichero

@@ -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;


+ 43
- 0
test/unit/real.cpp Ver fichero

@@ -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);


Cargando…
Cancelar
Guardar