Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Last active October 10, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-ueding/74b80d96a5cc4ac593b33d11901677af to your computer and use it in GitHub Desktop.
Save martin-ueding/74b80d96a5cc4ac593b33d11901677af to your computer and use it in GitHub Desktop.
#include "mymath.h"
#include <stdio.h>
int main(){
double t=5;
double *p;
double m;
p=&t;
m=square_to(p);
printf("%f\n%f\n", t, m);
return 0;
}
#include "mymath.h"
#include <stdio.h>
/* Betrag function */
double aabs(double x){
if(x<0) return -x;
else return x;
}
/* Cosinus function */
double cos(double x){
int i;
i=1;
double sum;
sum=1;
double summand;
summand=1;
double y;
y=1;
double z;
z=1;
int t;
while(aabs(summand)>0.00001){
y=y*x*x;
z=z*(2*i-1)*(2*i);
if(i%2==0) t=1;
else t=-1;
summand=t*y/z;
sum+=summand;
i++;
}
return sum;
}
/* signum function */
int sgn(double x){
if(x<0) return -1;
else return (x!=0);
}
/* square root function */
double sqroot(double a){
if(a<=0)return 0;
int i;
int b;
b=a;
for(i=0;i<100000;i++){
a=0.5*(a+b/a);
}
return a;
}
/* prim test */
int prim(int c){
int n;
n=2;
int p=1;
while(n*n<c){
if(c%n==0) p=0;
n++;
}
return p;
}
/* größter gemeinsamer Teiler */
int ggt(int a, int b){
if(a==0) {
return b;
}
while(b!=0){
if(a>b){
a=a-b;
}
else{
b=b-a;
}
}
return a;
}
/* e function */
double expo(double x){;
double summand=1;
double sum=0;
int i;
for(i=1;summand>0.000000001;i++){
sum+=summand;
summand*=x/i;
}
return sum;
}
/* natural log */
double ln(double x){
double summand=(x-1)/(x+1);
double sum=0;
int i;
for(i=1;summand>0.000000001;i++){
sum+=summand;
summand*=((x-1)/(x+1))*((x-1)/(x+1))*(1./(2*i+1))*(2*i-1);
}
return 2*sum;
}
/* power function with e function and ln */
double power(double x, double y){
return expo(y*ln(x));
}
/* naiv power function */
double power_naiv(double x, int y){
int i;
double p=1;
for(i=0;i<y;i++){
p*=x;
}
return p;
}
/* double and add power function */
double power_dadd(double x, int n){
if(n==0) return 1;
double t=1;
int i;
for(i=n;i>0;i--){
if(n%2){
t=t*x;
x=x*x;
i=(i-1)/2;
}
else{
x=x*x;
i=i/2;
}
}
return t;
}
/* faculty function */
int fac(int x){
if(x==0) return 1;
else return x*fac(x-1);
}
/* kth-root of a */
double kroot(double a, int k){
return power(a, 1./k);
}
/* log x to base a */
double loga(int a, double x){
return (ln(x)/ln(a));
}
/* return 3rd side with 2 given sides and their angle in a triangle */
double triangle(double a, double b, double w){
return sqroot(a*a+b*b+2*a*b*cos(w));
}
/* abc formula function */
double abcformel(double a, double b, double c){
if(a!=0){
double x;
double y;
if(b!=0){
x=0;
y=0;
x=-(b/a)/2+sqroot((b/a)*(b/a)/4-c/a);
y=-(b/a)/2-sqroot((b/a)*(b/a)/4-c/a);
}
else {
x=sqroot(-c/a);
y=-sqroot(-c/a);
}
if(x>y) return x;
else return y;
}
else return 0;
}
/* Riemann Zeta function */
double zeta(double s){
double i=0;
double sum=0;
double summand=1;
for(i=1;summand>0.00000001 && summand<10000;i++){
summand=1/power(i, s);
sum+=summand;
}
return sum;
}
/* größtes Element in Array */
double arrayMax(int *a, unsigned l){
unsigned i;
double max=0;
for(i=0;i<l;i++){
if(a[i]>max) max=a[i];
}
return max;
}
/* Array Gleichheit Test */
int arrayGTest(int *a, int *b, unsigned l){
unsigned i;
for(i=0;i<l;i++){
if(a[i]!=b[i]) return 0;
}
return 1;
}
/* vertausche jedes b in a mit c */
void arraybc(int *a, int b, int c, unsigned l){
unsigned i;
for(i=0;i<l; i++){
if(a[i]==b) a[i]=c;
}
}
/* sort array from smallest to biggest */
void sortArray(int *a, unsigned l){
unsigned i;
unsigned k;
int t;
for(i=0;i<l;i++){
for(k=0;k<l-1;k++){
if(a[k]>a[k+1]) {
t=a[k];
a[k]=a[k+1];
a[k+1]=t;
}
}
}
}
/* prints array */
void printArray(double *a, unsigned l){
unsigned i;
for(i=0;i<l;i++){
printf("%f \n", a[i]);
}
}
/* abc formula function */
int abcformel2(double a, double b, double c, double l[2]){
double x;
double y;
if(a>0 && b>0 && c>0) return 0;
if(a==0){
l[0]=-c/b;
return 1;
}
if(b!=0){
x=0;
y=0;
x=-(b/a)/2+sqroot((b/a)*(b/a)/4-c/a);
y=-(b/a)/2-sqroot((b/a)*(b/a)/4-c/a);
}
else {
x=sqroot(-c/a);
y=-sqroot(-c/a);
}
l[0]=x;
l[1]=y;
return 2;
}
double square_to(double *p){
*p=(*p)*(*p);
return *p;
}
#ifndef MYMATH_H
#define MYMATH_H
double aabs(double x);
double cos(double x);
int sgn(double x);
double sqroot(double a);
int prim(int c);
int ggt(int a, int b);
double expo(double x);
double ln(double x);
double power(double x, double y);
double power_naiv(double x, int y);
double power_dadd(double x, int n);
int fac(int x);
double kroot(double a, int k);
double loga(int a, double x);
double triangle(double a, double b, double w);
double abcformel(double a, double b, double c);
double zeta(double s);
double arrayMax(int *a, unsigned l);
int arrayGTest(int *a, int *b, unsigned l);
void arrayab(int *a, int b, int c, unsigned l);
void sortArray(int *a, unsigned l);
void printArray(double *a, unsigned l);
int abcformel2(double a, double b, double c, double l[2]);
double square_to(double *p);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment