Browse Source

refactoring: move exponential function to reihen.c

master
fdai7848 11 months ago
parent
commit
12393766b6
  1. 22
      src/exponent.c
  2. 1
      src/exponent.h
  3. 1
      src/logarithmus.c
  4. 20
      src/reihen.c
  5. 2
      src/reihen.h
  6. 23
      test/test_exponent.c
  7. 1
      test/test_logarithmus.c
  8. 22
      test/test_reihen.c

22
src/exponent.c

@ -4,6 +4,7 @@
#include "exponent.h" #include "exponent.h"
#include "logarithmus.h" #include "logarithmus.h"
#include "reihen.h"
double absD(double x){ double absD(double x){
if(x<0){ if(x<0){
@ -49,24 +50,3 @@ double powerD(double exp, double base){
return -1.0; return -1.0;
} }
unsigned long long fac(int x){
unsigned long long prod = 1;
if(x==0) return 1;
for (int i = 1; i <= x; i++){
if (prod > ULLONG_MAX / i){
break;
}
prod*=i;
}
return prod;
}
double exponential(double x){
double sum = 0.0;
for(int i = 0; i<=21; i++){
sum += (1.0/fac(i)*(powerD(i,x)));
}
return sum;
}

1
src/exponent.h

@ -3,7 +3,6 @@
double p(double exp, double base); double p(double exp, double base);
double powerD(double exp, double base); double powerD(double exp, double base);
double exponential(double x);
double absD(double x); double absD(double x);
#endif // exponent.h #endif // exponent.h

1
src/logarithmus.c

@ -3,6 +3,7 @@
#include "logarithmus.h" #include "logarithmus.h"
#include "exponent.h" #include "exponent.h"
#include "reihen.h"
double logX(double base, double value){ double logX(double base, double value){
if(base == 1.0 || base <= 0.0 || value <= 0.0){ if(base == 1.0 || base <= 0.0 || value <= 0.0){

20
src/reihen.c

@ -5,3 +5,23 @@
#include "reihen.h" #include "reihen.h"
#include "exponent.h" #include "exponent.h"
#include "logarithmus.h" #include "logarithmus.h"
unsigned long long fac(int x){
unsigned long long prod = 1;
if(x==0) return 1;
for (int i = 1; i <= x; i++){
if (prod > ULLONG_MAX / i){
break;
}
prod*=i;
}
return prod;
}
double exponential(double x){
double sum = 0.0;
for(int i = 0; i<=21; i++){
sum += (1.0/fac(i)*(powerD(i,x)));
}
return sum;
}

2
src/reihen.h

@ -1,6 +1,6 @@
#ifndef REIHEN_H #ifndef REIHEN_H
#define REIHEN_H #define REIHEN_H
double exponential(double x);
#endif // reihen.h #endif // reihen.h

23
test/test_exponent.c

@ -4,6 +4,7 @@
#include "exponent.h" #include "exponent.h"
#include "logarithmus.h" #include "logarithmus.h"
#include "reihen.h"
void setUp(void) void setUp(void)
{ {
@ -101,26 +102,4 @@ void test_exponent_edge_case(void){
TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5); TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5);
} }
void test_Exponent_with_positive_number(void){
double r1, r2, r3;
r1 = exponential(0.0);
r2 = exponential(1.0);
r3 = exponential(2.0);
TEST_ASSERT_EQUAL_DOUBLE(1.0, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 2.718282, r2);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 7.389056, r3);
}
void test_Exponent_with_negative_number(void){
double r1, r2, r3;
r1 = exponential(-1.0);
r2 = exponential(-2.0);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.367879, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2);
}
#endif // TEST #endif // TEST

1
test/test_logarithmus.c

@ -4,6 +4,7 @@
#include "logarithmus.h" #include "logarithmus.h"
#include "exponent.h" #include "exponent.h"
#include "reihen.h"
void setUp(void) void setUp(void)
{ {

22
test/test_reihen.c

@ -14,4 +14,26 @@ void tearDown(void)
{ {
} }
void test_Exponent_with_positive_number(void){
double r1, r2, r3;
r1 = exponential(0.0);
r2 = exponential(1.0);
r3 = exponential(2.0);
TEST_ASSERT_EQUAL_DOUBLE(1.0, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 2.718282, r2);
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 7.389056, r3);
}
void test_Exponent_with_negative_number(void){
double r1, r2, r3;
r1 = exponential(-1.0);
r2 = exponential(-2.0);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.367879, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2);
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save