Browse Source

Merge branch 'Reihen' into 'master'

Reihen

See merge request fdai7848/Linkes-Twix!4
master
fdai7848 11 months ago
parent
commit
13305407dc
  1. 30
      src/exponent.c
  2. 2
      src/exponent.h
  3. 2
      src/logarithmus.c
  4. 83
      src/reihen.c
  5. 11
      src/reihen.h
  6. 28
      src/util.c
  7. 8
      src/util.h
  8. 2
      team.md
  9. 24
      test/test_exponent.c
  10. 2
      test/test_logarithmus.c
  11. 90
      test/test_reihen.c

30
src/exponent.c

@ -4,13 +4,8 @@
#include "exponent.h"
#include "logarithmus.h"
double absD(double x){
if(x<0){
return -1 * x;
}
return x;
}
#include "reihen.h"
#include "util.h"
double p(double exp, double base){
double prod = 1.0;
@ -49,24 +44,3 @@ double powerD(double exp, double base){
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;
}

2
src/exponent.h

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

2
src/logarithmus.c

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

83
src/reihen.c

@ -0,0 +1,83 @@
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include "reihen.h"
#include "exponent.h"
#include "logarithmus.h"
#include "util.h"
#define PI pi(1e-8)
double exponential(double exp){
double sum = 0.0;
for(int i = 0; i<=21; i++){
sum += (1.0/fac(i)*(powerD(i,exp)));
}
return sum;
}
double euler(){
return exponential(1);
}
double radians(double degrees){
return degrees * PI / 180;
}
double pi(double eps){
double a = 1, b = 1/powerD(0.5, 2), t = 0.25, p = 1;
double aNew, piold, pi = 1;
do{
aNew = .5 * (a + b);
b = powerD(0.5 , a * b);
t -= p * (a - aNew) * (a - aNew);
a = aNew;
p *= 2;
piold = pi;
pi = (a + b) * (a + b) / (4 * t);
} while (absD(pi - piold) > eps);
return pi;
}
double sinD(double degrees){
if (degrees>=360.0){
degrees = squashDegreesTo360(degrees);
}
double sum = 0.0;
double radian = radians(degrees);
for (int i = 0; i <= 9; i++){
sum += powerD((double) i, -1.0) * (powerD(2.0*i+1, radian) / fac(2.0*i+1));
}
return sum;
}
double cosD(double degrees){
if (degrees>=360.0){
degrees = squashDegreesTo360(degrees);
}
double sum = 0.0;
double radian = radians(degrees);
for (int i = 0; i <= 9; i++){
sum += powerD((double) i, -1.0) * (powerD(2*i, radian) / fac(2*i));
}
return sum;
}
double tanD(double degrees){
if (degrees>=360.0){
degrees = squashDegreesTo360(degrees);
}
if(degrees == 90.0 || degrees == 270.0){
return -1.0;
}
return sinD(degrees)/cosD(degrees);
}

11
src/reihen.h

@ -0,0 +1,11 @@
#ifndef REIHEN_H
#define REIHEN_H
double exponential(double exp);
double euler();
double pi();
double radians(double degrees);
double sinD(double degrees);
double cosD(double degrees);
#endif // reihen.h

28
src/util.c

@ -0,0 +1,28 @@
#include <stdio.h>
#include <limits.h>
#include "util.h"
double absD(double x){
if(x<0){
return -1 * x;
}
return x;
}
double squashDegreesTo360(double degrees){
int multiple = degrees / 360;
return degrees - (360*multiple);
}
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;
}

8
src/util.h

@ -0,0 +1,8 @@
#ifndef UTIL_H
#define UTIL_H
double absD(double x);
double squashDegreesTo360(double degrees);
unsigned long long fac(int x);
#endif // util.h

2
team.md

@ -1,4 +1,4 @@
- Jan Heidekrüger, fdai7848
- fdai7848, fdai7848
- Inna Röhrig, fdai7851
- Justin Trausch, fdai7720
- Sophia Weber, fdai7828

24
test/test_exponent.c

@ -4,6 +4,8 @@
#include "exponent.h"
#include "logarithmus.h"
#include "reihen.h"
#include "util.h"
void setUp(void)
{
@ -101,26 +103,4 @@ void test_exponent_edge_case(void){
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

2
test/test_logarithmus.c

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

90
test/test_reihen.c

@ -0,0 +1,90 @@
#ifdef TEST
#include "unity.h"
#include "reihen.h"
#include "exponent.h"
#include "logarithmus.h"
#include "util.h"
void setUp(void)
{
}
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);
}
void test_pi(void){
double r1;
r1 = pi(1e-8);
TEST_ASSERT_DOUBLE_WITHIN(0.0000001, 3.14159265, r1);
}
void test_degrees_to_radians(){
double r1, r2, r3, r4;
r1 = radians(90.0);
r2 = radians(180.0);
r3 = radians(51.6);
r4 = radians(36.97);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.570796, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, pi(1e-8), r2);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.900589, r3);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.645248, r4);
}
void test_sin(void){
double r1, r2, r3, r4;
r1 = sinD(50.0);
r2 = sinD(90.0);
r3 = sinD(36.97);
r4 = sinD(180.0);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.766044, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.0, r2);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.601396, r3);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r4);
}
void test_cos(void){
double r1, r2, r3, r4;
r1 = cosD(50.0);
r2 = cosD(90.0);
r3 = cosD(36.97);
r4 = cosD(180.0);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.642787, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r2);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.798950, r3);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, -1.0, r4);
}
#endif // TEST
Loading…
Cancel
Save