Explorar el Código

core: implement round() and fmod() for real numbers.

legacy
Sam Hocevar sam hace 13 años
padre
commit
c5c4913bda
Se han modificado 3 ficheros con 48 adiciones y 23 borrados
  1. +20
    -0
      src/real.cpp
  2. +2
    -0
      src/real.h
  3. +26
    -23
      test/unit/real.cpp

+ 20
- 0
src/real.cpp Ver fichero

@@ -685,6 +685,26 @@ real ceil(real const &x)
return ret + real::R_1;
}

real round(real const &x)
{
if (x < real::R_0)
return -round(-x);

return floor(x + (real::R_1 >> 1));
}

real fmod(real const &x, real const &y)
{
if (!y)
return real::R_0; /* FIXME: return NaN */

if (!x)
return x;

real tmp = round(x / y);
return x - tmp * y;
}

real sin(real const &x)
{
real ret = 0.0, fact = 1.0, xn = x, x2 = x * x;


+ 2
- 0
src/real.h Ver fichero

@@ -71,6 +71,8 @@ public:

friend real floor(real const &x);
friend real ceil(real const &x);
friend real round(real const &x);
friend real fmod(real const &x, real const &y);

friend real sin(real const &x);
friend real cos(real const &x);


+ 26
- 23
test/unit/real.cpp Ver fichero

@@ -257,42 +257,45 @@ LOLUNIT_FIXTURE(RealTest)
}
}

LOLUNIT_TEST(FloorCeil)
LOLUNIT_TEST(FloorCeilEtc)
{
double tests[] =
{
-2.0, -2.0, -2.0,
-1.5, -2.0, -1.0,
-1.0, -1.0, -1.0,
-0.0, -0.0, -0.0,
0.0, 0.0, 0.0,
0.25, 0.0, 1.0,
0.375, 0.0, 1.0,
0.5, 0.0, 1.0,
1.0, 1.0, 1.0,
1.5, 1.0, 2.0,
2.0, 2.0, 2.0,
2.5, 2.0, 3.0,
3.0, 3.0, 3.0,
8192.0, 8192.0, 8192.0,
8192.03125, 8192.0, 8193.0,
8192.5, 8192.0, 8193.0,
8193.0, 8193.0, 8193.0,
549755813888.0, 549755813888.0, 549755813888.0,
549755813888.03125, 549755813888.0, 549755813889.0,
549755813888.5, 549755813888.0, 549755813889.0,
549755813889.0, 549755813889.0, 549755813889.0,
-2.0, -2.0, -2.0, -2.0,
-1.5, -2.0, -1.0, -2.0,
-1.0, -1.0, -1.0, -1.0,
-0.0, -0.0, -0.0, -0.0,
0.0, 0.0, 0.0, 0.0,
0.25, 0.0, 1.0, 0.0,
0.375, 0.0, 1.0, 0.0,
0.5, 0.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.5, 1.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0,
2.5, 2.0, 3.0, 3.0,
3.0, 3.0, 3.0, 3.0,
8192.0, 8192.0, 8192.0, 8192.0,
8192.03125, 8192.0, 8193.0, 8192.0,
8192.5, 8192.0, 8193.0, 8193.0,
8193.0, 8193.0, 8193.0, 8193.0,
549755813888.0, 549755813888.0, 549755813888.0, 549755813888.0,
549755813888.03125, 549755813888.0, 549755813889.0, 549755813888.0,
549755813888.5, 549755813888.0, 549755813889.0, 549755813889.0,
549755813889.0, 549755813889.0, 549755813889.0, 549755813889.0,
};

for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 3)
for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 4)
{
double a0 = floor((real)tests[n]);
double b0 = tests[n + 1];
double a1 = ceil((real)tests[n]);
double b1 = tests[n + 2];
double a2 = round((real)tests[n]);
double b2 = tests[n + 3];

LOLUNIT_ASSERT_EQUAL(b0, a0);
LOLUNIT_ASSERT_EQUAL(b1, a1);
LOLUNIT_ASSERT_EQUAL(b2, a2);
}
}
};


Cargando…
Cancelar
Guardar