From cf903578dab6002be56fa00f3e2064f904c95fba Mon Sep 17 00:00:00 2001 From: tlatorre Date: Thu, 13 Sep 2018 10:04:56 -0500 Subject: add a function to compute log(n) for integer n This commit adds the function ln() to compute log(n) for integer n. It uses a lookup table for n < 100 to speed things up. --- src/test.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/test.c') diff --git a/src/test.c b/src/test.c index fa4256f..9976f1f 100644 --- a/src/test.c +++ b/src/test.c @@ -489,6 +489,28 @@ err: return 1; } +int test_ln(char *err) +{ + /* Tests the lnfact() function. */ + size_t i; + double x, expected; + + for (i = 1; i < 1000; i++) { + x = ln(i); + expected = log(i); + + if (!isclose(x, expected, 1e-9, 1e-9)) { + sprintf(err, "ln(%zu) returned %.5g, but expected %.5g", i, x, expected); + goto err; + } + } + + return 0; + +err: + return 1; +} + struct tests { testFunction *test; char *name; @@ -505,6 +527,7 @@ struct tests { {test_interp1d, "test_interp1d"}, {test_kahan_sum, "test_kahan_sum"}, {test_lnfact, "test_lnfact"}, + {test_ln, "test_ln"}, }; int main(int argc, char **argv) -- cgit