From fef22fedff07936979fb3cb8b58f39f1b8c7863b Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 19:31:59 +0100 Subject: [PATCH] test ln functions for values smaller than 0 and 0 --- src/exponent.c | 5 ++++- test/test_exponent.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/exponent.c b/src/exponent.c index b37deab..03f4607 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -16,7 +16,7 @@ double expI(double e, double num){ } double expIN(double e, double num){ - if(e - ((int) e) < 0.0001 || e - ((int) e) > -0.0001){ + if(e - ((int) e) < 0.000001 || e - ((int) e) > -0.000001){ if(e>0){ return expI(e, num); } @@ -56,6 +56,9 @@ double absD(double x){ } double logN(double x, double eps){ + if(x <= 0){ + return -1.0; + } double yn = x - 1.0; double yn1 = yn; diff --git a/test/test_exponent.c b/test/test_exponent.c index 8545529..8bd76c9 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -85,6 +85,21 @@ void test_ln_with_positive_number(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.916290, r3); } +void test_ln_with_zero_and_negative_numbers(void){ + double r1, r2, r3; + + r1 = logN(0.0, 0.000001); + r2 = logN(-1.0, 0.000001); + r3 = logN(-2.5, 0.000001); + + printf("Result 1: %lf\n", r1); + printf("Result 2: %lf\n", r2); + printf("Result 3: %lf\n", r3); + + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r3); +} #endif // TEST \ No newline at end of file