Browse Source

Add test for decimal exponents

master
fdai7848 11 months ago
parent
commit
650e1f439d
  1. 22
      src/exponent.c
  2. 16
      test/test_exponent.c

22
src/exponent.c

@ -3,6 +3,13 @@
#include "exponent.h"
double absD(double x){
if(x<0){
return -1 * x;
}
return x;
}
double expI(double e, double num){
double prod = 1.0;
if(e==0.0){
@ -16,16 +23,16 @@ double expI(double e, double num){
}
double expIN(double e, double num){
if(e - ((int) e) < 0.000001 || e - ((int) e) > -0.000001){
if(e>0){
if(absD(e) - ((int) absD(e)) < 0.000001){
if(e>0.0){
return expI(e, num);
}
else{
return (1.0 / expI((e*-1), num));
}
}else{
if(e>0){
return exponential(e*logN(num, 0.000001));
if(e>0.0){
return exponential(e*logN(num, 0.00000001));
}
}
return 0;
@ -52,12 +59,7 @@ double exponential(double x){
return sum;
}
double absD(double x){
if(x<0){
return -1 * x;
}
return x;
}
double logN(double x, double eps){
if(x <= 0){

16
test/test_exponent.c

@ -40,6 +40,22 @@ void test_DoubleBase_ForNegativeExponent(void){
TEST_ASSERT_EQUAL_FLOAT(0.25, r3);
}
void test_DoubleBase_for_positive_decimal_exponent(void){
float r1, r2, r3;
r1 = expIN(0.45, 3.0);
r2 = expIN(0.5, 9.0);
r3 = expIN(0.5, 2.0);
printf("Result 1: %f\n", r1);
printf("Result 2: %f\n", r2);
printf("\nResult 3: %lf\n\n", r3);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.414213, r3);
}
void test_Exponent_with_positive_number(void){
double r1, r2, r3;

Loading…
Cancel
Save