From 18200c9252026024d5bd261389475d0e9cea5bd7 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:46:41 +0100 Subject: [PATCH] add tests for edge cases --- src/exponent.c | 12 +++++++++--- test/test_exponent.c | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index feea7d7..e74dbf3 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -24,21 +24,27 @@ double expI(double e, double num){ double expIN(double e, double num){ if(absD(e) - ((int) absD(e)) < 0.000001){ - if(e>0.0){ + if(e>=0.0){ return expI(e, num); } else{ + if (num == 0.0){ + return -1.0; + } return (1.0 / expI((e*-1), num)); } }else if(num > 0.0){ - if(e>0.0){ + if(e>=0.0){ return exponential(e*logN(num, 0.00000001)); } else{ + if (num == 0.0){ + return -1.0; + } return (1.0 / exponential((-1*e)*logN(num, 0.00000001))); } } - return -1; + return -1.0; } unsigned long long fac(int x){ diff --git a/test/test_exponent.c b/test/test_exponent.c index f0a09d0..8f371cd 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -84,6 +84,22 @@ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.000964, r3); } +void test_exponent_edge_case(void){ + float r1, r2, r3, r4, r5; + + r1 = expIN(0, 2); + r2 = expIN(0, 0); + r3 = expIN(2, 0); + r4 = expIN(-3, 0); + r5 = expIN(0.5, -3); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.0, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r4); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3;