diff --git a/src/math/misc.c b/src/math/numerics.c similarity index 87% rename from src/math/misc.c rename to src/math/numerics.c index d36f14f..7272617 100644 --- a/src/math/misc.c +++ b/src/math/numerics.c @@ -181,4 +181,30 @@ float sqrootf(float n) { // Returns the approximate result of square root of n float sqrootl(long double n) { return powerl(n, 0.5); +} + +// Returns the ceiling of n (smallest integral value not less than n) +double ceild(double n) { + if (n >= 0) { + if (n - ((long) n) > 0.0f) { + return (double) (((long) n) + 1); + } else { + return (double) ((long) n); + } + } else { + return (double) ((long) n); + } +} + +// Returns the floor of n (largest integral value not greater than n) +double floord(double n) { + if (n >= 0) { + return (double) ((long) n); + } else { + if ((double) ((long) n) != n) { + return (double) ((long) n - 1); + } else { + return n; + } + } } \ No newline at end of file diff --git a/src/math/misc.h b/src/math/numerics.h similarity index 89% rename from src/math/misc.h rename to src/math/numerics.h index 892f020..fc1a290 100644 --- a/src/math/misc.h +++ b/src/math/numerics.h @@ -44,4 +44,10 @@ long double powerl(long double base, long double exp); float sqrootf(float n); // Returns the approximate result of square root of n -float sqrootl(long double n); \ No newline at end of file +float sqrootl(long double n); + +// Returns the ceiling of n (smallest integral value not less than n) +double ceild(double n); + +// Returns the floor of n (largest integral value not greater than n) +double floord(double n); \ No newline at end of file diff --git a/testing/test.c b/testing/test.c index be7dfa4..0aeaa5f 100644 --- a/testing/test.c +++ b/testing/test.c @@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include #include #include +#include #include #include "../src/rng/rng.h" @@ -24,7 +25,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include "../src/fs/fs.h" #include "../src/bits/bits.h" #include "../src/math/vector.h" -#include "../src/math/misc.h" +#include "../src/math/numerics.h" #include "../src/datastruct/cvec.h" #include "../src/strings/levenshtein.h" #include "../src/crypt/xorcipher.h" @@ -225,7 +226,7 @@ int test_vector() { return EXIT_SUCCESS; } -int test_misc() { +int test_numerics() { const float num1 = -0.5; if (absf(num1) != 0.5f) { printf("[ERROR] Failed to get absolute value of %f: got %f; expected %f", num1, absf(num1), -num1); @@ -247,7 +248,50 @@ int test_misc() { const int num3 = 4; if (sqrootf(num3) != 2.0) { - printf("[ERROR] Failed to calculate square root of %d: got %f; expected %d", num3, sqrootf(num3), 2); + printf("[ERROR] Failed to calculate square root of %d: got %f; expected %d\n", num3, sqrootf(num3), 2); + return EXIT_FAILURE; + } + + const double pn = 6.4f; + if (ceild(pn) != 7.0f) { + printf("[ERROR] ceild returned %f on %f instead of %f\n", ceild(pn), pn, 7.0f); + return EXIT_FAILURE; + } + + if (ceil(pn) != ceild(pn)) { + printf("[ERROR] ceild result on %f does not match std's ceil: %f != %f\n", pn, ceild(pn), ceil(pn)); + return EXIT_FAILURE; + } + + const double pn2 = 3.0f; + if (ceild(pn2) != 3.0f) { + printf("[ERROR] ceild result on %f does not match std's ceil: %f != %f\n", pn2, ceild(pn), ceil(pn)); + return EXIT_FAILURE; + } + + const double pn3 = -2.6f; + if (ceil(pn3) != ceild(pn3)) { + printf("[ERROR] ceild result on %f does not match std's ceil: %f != %f\n", pn3, ceild(pn), ceil(pn)); + return EXIT_FAILURE; + } + + if (floord(pn) != 6.0f) { + printf("[ERROR] floord returned %f on %f: expected to be %f\n", floord(pn), pn, 6.0f); + return EXIT_FAILURE; + } + + if (floord(pn2) != 3.0f) { + printf("[ERROR] floord returned %f on %f: expected to be %f\n", floord(pn2), pn2, 3.0f); + return EXIT_FAILURE; + } + + if (floord(pn3) != -3.0f) { + printf("[ERROR] floord returned %f on %f: expected to be %f\n", floord(pn3), pn3, -3.0f); + return EXIT_FAILURE; + } + + if (floord(pn3) != floor(pn3)) { + printf("[ERROR] floord's result on %f is different from std's floor: %f != %f\n", pn3, floord(pn3), floor(pn3)); return EXIT_FAILURE; } @@ -260,8 +304,8 @@ int test_math() { return EXIT_FAILURE; } - if (test_misc() == EXIT_FAILURE) { - printf("[ERROR] Misc test failed\n"); + if (test_numerics() == EXIT_FAILURE) { + printf("[ERROR] Numerics test failed\n"); return EXIT_FAILURE; }