Browse Source

refactoring: change variable names and function names

master
fdai7848 11 months ago
parent
commit
3089590084
  1. 35
      src/exponent.c
  2. 4
      src/exponent.h
  3. 46
      test/test_exponent.c

35
src/exponent.c

@ -10,38 +10,38 @@ double absD(double x){
return x; return x;
} }
double expI(double e, double num){
double p(double exp, double base){
double prod = 1.0; double prod = 1.0;
if(e==0.0){
if(exp==0.0){
return 1; return 1;
}else{ }else{
for(int i = 1; i <= (int) e; i++){
prod *= num;
for(int i = 1; i <= (int) exp; i++){
prod *= base;
} }
return prod; return prod;
} }
} }
double expIN(double e, double num){
if(absD(e) - ((int) absD(e)) < 0.000001){
if(e>=0.0){
return expI(e, num);
double powerD(double exp, double base){
if(absD(exp) - ((int) absD(exp)) < 0.000001){
if(exp>=0.0){
return p(exp, base);
} }
else{ else{
if (num == 0.0){
if (base == 0.0){
return -1.0; return -1.0;
} }
return (1.0 / expI((e*-1), num));
return (1.0 / p((exp*-1), base));
} }
}else if(num > 0.0){
if(e>=0.0){
return exponential(e*logN(num, 0.00000001));
}else if(base > 0.0){
if(exp>=0.0){
return exponential(exp*logN(base, 0.00000001));
} }
else{ else{
if (num == 0.0){
if (base == 0.0){
return -1.0; return -1.0;
} }
return (1.0 / exponential((-1*e)*logN(num, 0.00000001)));
return (1.0 / exponential((-1*exp)*logN(base, 0.00000001)));
} }
} }
return -1.0; return -1.0;
@ -63,7 +63,7 @@ unsigned long long fac(int x){
double exponential(double x){ double exponential(double x){
double sum = 0.0; double sum = 0.0;
for(int i = 0; i<=21; i++){ for(int i = 0; i<=21; i++){
sum += (1.0/fac(i)*(expIN(i,x)));
sum += (1.0/fac(i)*(powerD(i,x)));
} }
return sum; return sum;
} }
@ -77,8 +77,7 @@ double logN(double x, double eps){
double yn = x - 1.0; double yn = x - 1.0;
double yn1 = yn; double yn1 = yn;
do
{
do{
yn = yn1; yn = yn1;
yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn)); yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn));
} while (absD(yn - yn1) > eps); } while (absD(yn - yn1) > eps);

4
src/exponent.h

@ -1,8 +1,8 @@
#ifndef EXPONENT_H #ifndef EXPONENT_H
#define EXPONENT_H #define EXPONENT_H
double expI(double e, double num);
double expIN(double e, double num);
double p(double exp, double base);
double powerD(double exp, double base);
double exponential(double x); double exponential(double x);
double logN(double x, double eps); double logN(double x, double eps);

46
test/test_exponent.c

@ -15,9 +15,9 @@ void tearDown(void)
void test_DoubleBase_ForPositiveExponent(void){ void test_DoubleBase_ForPositiveExponent(void){
int r1, r2, r3; int r1, r2, r3;
r1 = expI(2.0, -2.0);
r2 = expI(3.0, -3.0);
r3 = expI(4.0, 5.0);
r1 = p(2.0, -2.0);
r2 = p(3.0, -3.0);
r3 = p(4.0, 5.0);
TEST_ASSERT_EQUAL_INT(4.0, r1); TEST_ASSERT_EQUAL_INT(4.0, r1);
TEST_ASSERT_EQUAL_INT(-27.0, r2); TEST_ASSERT_EQUAL_INT(-27.0, r2);
@ -27,9 +27,9 @@ void test_DoubleBase_ForPositiveExponent(void){
void test_DoubleBase_ForNegativeExponent(void){ void test_DoubleBase_ForNegativeExponent(void){
float r1, r2, r3; float r1, r2, r3;
r1 = expIN(-1.0, 2.0);
r2 = expIN(-3.0, -3.0);
r3 = expIN(-2.0, -2.0);
r1 = powerD(-1.0, 2.0);
r2 = powerD(-3.0, -3.0);
r3 = powerD(-2.0, -2.0);
TEST_ASSERT_EQUAL_FLOAT(0.5, r1); TEST_ASSERT_EQUAL_FLOAT(0.5, r1);
TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2); TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2);
@ -39,9 +39,9 @@ void test_DoubleBase_ForNegativeExponent(void){
void test_DoubleBase_for_positive_decimal_exponent(void){ void test_DoubleBase_for_positive_decimal_exponent(void){
float r1, r2, r3; float r1, r2, r3;
r1 = expIN(0.45, 3.0);
r2 = expIN(0.5, 9.0);
r3 = expIN(0.5, 2.0);
r1 = powerD(0.45, 3.0);
r2 = powerD(0.5, 9.0);
r3 = powerD(0.5, 2.0);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2);
@ -51,9 +51,9 @@ void test_DoubleBase_for_positive_decimal_exponent(void){
void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){
float r1, r2, r3; float r1, r2, r3;
r1 = expIN(0.31, 0.413);
r2 = expIN(0.5, 3.13432);
r3 = expIN(0.124, 10.1231);
r1 = powerD(0.31, 0.413);
r2 = powerD(0.5, 3.13432);
r3 = powerD(0.124, 10.1231);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2);
@ -63,9 +63,9 @@ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){
void test_DoubleBase_for_negative_decimal_exponent(void){ void test_DoubleBase_for_negative_decimal_exponent(void){
float r1, r2, r3; float r1, r2, r3;
r1 = expIN(-0.125, 3.0);
r2 = expIN(-0.5, 2.0);
r3 = expIN(-0.12923, 10.0);
r1 = powerD(-0.125, 3.0);
r2 = powerD(-0.5, 2.0);
r3 = powerD(-0.12923, 10.0);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2);
@ -75,9 +75,9 @@ void test_DoubleBase_for_negative_decimal_exponent(void){
void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){
float r1, r2, r3; float r1, r2, r3;
r1 = expIN(-0.5, 6.2315);
r2 = expIN(-0.1937, 9.2309);
r3 = expIN(-2.2145, 22.9983);
r1 = powerD(-0.5, 6.2315);
r2 = powerD(-0.1937, 9.2309);
r3 = powerD(-2.2145, 22.9983);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.400593, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.400593, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.650177, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.650177, r2);
@ -87,11 +87,11 @@ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){
void test_exponent_edge_case(void){ void test_exponent_edge_case(void){
float r1, r2, r3, r4, r5; 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);
r1 = powerD(0, 2);
r2 = powerD(0, 0);
r3 = powerD(2, 0);
r4 = powerD(-3, 0);
r5 = powerD(0.5, -3);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2);

Loading…
Cancel
Save