Pārlūkot izejas kodu

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

legacy
Sam Hocevar sam pirms 13 gadiem
vecāks
revīzija
c5c4913bda
3 mainītis faili ar 48 papildinājumiem un 23 dzēšanām
  1. +20
    -0
      src/real.cpp
  2. +2
    -0
      src/real.h
  3. +26
    -23
      test/unit/real.cpp

+ 20
- 0
src/real.cpp Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

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


Notiek ielāde…
Atcelt
Saglabāt