Commit a67d19e9 authored by Esben's avatar Esben
Browse files

Update repository

parents
File added
#include<stdio.h>
#include<math.h>
#include<limits.h>
#include<float.h>
#include"equal.h"
int main(void){
int a = 1;
while(a<a+1){a=a+1;};
printf("The maximum integer found using a while loop is: %i\n",a);
int b = 1;
do{b=b+1;}
while(b<b+1);
printf("The maximum integer found using a do-while loop is: %i\n",b);
int c;
for(c = 1; c<c+1;c=c+1);
printf("The maximum integer found using a for loop is: %i\n",c);
int d = 1;
while(d>d-1){d=d-1;};
printf("The minimum integer found using a while loop is: %i\n",d);
int e = 1;
do{e=e-1;}
while(e>e-1);
printf("The minimum integer found using a do-while loop is: %i\n",e);
int f;
for(f=1; f>f-1;f=f-1);
printf("The minimum integer found using a for loop is: %i\n",f);
float g =1;
while(1+g!=1){g/=2;};
g*=2;
printf("\nThe machine epsilon for float is: %g\n",g);
double h =1.0;
while(1.0+h!=1.0){h/=2;};
h*=2;
printf("The machine epsilon for double is: %g\n",h);
long double i =1.0;
while(1.0+i!=1.0){i/=2;};
i*=2;
printf("The machine epsilon for long double is: %Lg\n",i);
printf("The encoded values ought to be:\n");
printf("Float: %g\n",FLT_EPSILON);
printf("Double: %g\n",DBL_EPSILON);
printf("Long double: %LG\n",LDBL_EPSILON);
//This was as high as I could go and still have a reasonable
//computing speed on a virtualbox setup.
int max = INT_MAX/128;
float j = max;
float sum_down = 0;
do{sum_down=sum_down+1/j;}
while(--j);
float k=1;
float sum_up = 0;
while(k!=max+1){
sum_up = sum_up+1/k;
k=k+1.0;
};
double l = max;
double sum_down_d = 0;
do{sum_down_d=sum_down_d+1/l;}
while(--l);
double m=1;
double sum_up_d = 0;
while(m!=max+1){
sum_up_d = sum_up_d+1/m;
m=m+1.0;
};
printf("\nUsing float the sum of the inverse of all integers is:\n");
printf("Summing up: %.15g\n",sum_up);
printf("Summing down: %.15g\n",sum_down);
printf("\nUsing doubles instead yields higher numbers:\n");
printf("Summing up: %.15g\n",sum_up_d);
printf("Summing down: %.15g\n",sum_down_d);
printf("\nThe actual infinite sum diverges.\n");
printf(" \u221e 1\n");
printf(" \u03a3 - = \u221e\n");
printf("n=1 n\n");
printf("\nSumming up the highest numbers are added first,\n so once n becomes large further parts of the sum \n become irrelevant and are rounded off.\n When summing up the series converges.\n When summing down the problem is lesser,\n and rounding up is the problem.\n Using doubles instead of floats reduces rounding errors significantly.\n");
return 0;
}
#include<stdio.h>
#include<math.h>
#include"equal.h"
int equal(double a, double b, double tau, double epsilon){
double diff = fabs(a-b);
double asum =fabs(a)+fabs(b);
double rdiff= diff/asum;
if (diff<tau){return 1;};
if (2*rdiff<epsilon){return 1;};
return 0;
};
void name_digit(int i){
switch(i){
case 0 :
printf("zero\n");
break;
case 1 :
printf("one\n");
break;
case 2 :
printf("two\n");
break;
case 3 :
printf("three\n");
break;
case 4 :
printf("four\n");
break;
case 5 :
printf("five\n");
break;
case 6 :
printf("six\n");
break;
case 7 :
printf("seven\n");
break;
case 8 :
printf("eight\n");
break;
case 9 :
printf("nine\n");
break;
default : printf("That is not a digit\n");
};
};
int equal(double,double,double,double);
void name_digit(int);
File added
#CFLAGS -Wall
main : out.txt
cat out.txt
out.txt : epsilon.exe
./epsilon.exe > out.txt
epsilon.exe : epsilon.o equal.o
gcc epsilon.o equal.o -o epsilon.exe -lm
equal.o : equal.c
gcc -c equal.c -m64
epsilon.o : epsilon.c
gcc -c epsilon.c -O -std=c99 -m64
clean:
rm epsilon.o epsilon equal.o out.txt
\ No newline at end of file
The maximum integer found using a while loop is: 2147483647
The maximum integer found using a do-while loop is: 2147483647
The maximum integer found using a for loop is: 2147483647
The minimum integer found using a while loop is: -2147483648
The minimum integer found using a do-while loop is: -2147483648
The minimum integer found using a for loop is: -2147483648
The machine epsilon for float is: 1.19209e-007
The machine epsilon for double is: 2.22045e-016
The machine epsilon for long double is: 1.13299e-317
The encoded values ought to be:
Float: 1.19209e-007
Double: 2.22045e-016
Long double: 1.13299E-317
Using float the sum of the inverse of all integers is:
Summing up: 15.4036827087402
Summing down: 17.2327079772949
Using doubles instead yields higher numbers:
Summing up: 17.2127479685356
Summing down: 17.2127479685383
The actual infinite sum diverges.
∞ 1
Σ - = ∞
n=1 n
Summing up the highest numbers are added first,
so once n becomes large further parts of the sum
become irrelevant and are rounded off.
When summing up the series converges.
When summing down the problem is lesser,
and rounding up is the problem.
Using doubles instead of floats reduces rounding errors significantly.
#include <stdio.h>
int main(void){
printf("Hello world!\n");
return 0;
}
File added
File added
show : out.txt
cat out.txt
out.txt : hello.exe
./hello.exe > out.txt
hello.exe : hello.o
gcc hello.o -o hello.exe
hello.o : hello.c
gcc -c hello.c
Hello world!
#include"komplex.h"
#include<math.h>
#include<stdio.h>
void k_print (char* s,komplex z){
printf(s);
if(z.re!=0 && z.im >= 0){
printf("%g+%gi\n",z.re,z.im);
}
else if(z.re != 0 && z.im < 0){
printf("%g-%gi\n",z.re,-z.im);
}
else if(z.im == 0){
printf("%g\n",z.re);
}
else{
printf("%gi\n",z.im);
};
};
void k_set (komplex* z, double x, double y){
z->re = x;
z->im = y;
};
komplex k_new(double x, double y){
komplex z;
z.re = x;
z.im = y;
return z;
};
komplex k_add(komplex a, komplex b){
komplex z;
z.re = a.re + b.re;
z.im = a.im + b.im;
return z;
};
komplex k_sub(komplex a, komplex b){
komplex z;
z.re = a.re - b.re;
z.im = a.im - b.im;
return z;
};
komplex k_inv(komplex a){
komplex z;
z.re = a.re/((a.re*a.re)+(a.im*a.im));
z.im = -a.im/((a.re*a.re)+(a.im*a.im));
return z;
};
komplex k_mul(komplex a, komplex b){
komplex z;
z.re = (a.re * b.re) - (a.im * b.im);
z.im = (a.re * b.im) + (a.im * b.re);
return z;
};
komplex k_div(komplex a, komplex b){
komplex z = k_mul(a,k_inv(b));
return z;
};
komplex k_conj(komplex z){
return k_new(z.re,-z.im);
};
double k_abs(komplex z){
return (pow((z.re*z.re)+(z.im*z.im),0.5));
};
komplex k_exp(komplex z){
return k_new(exp(z.re)*cos(z.im),exp(z.re)*sin(z.im));
};
komplex k_cos(komplex z){
komplex eiz = k_exp(k_mul(z,k_i));
komplex cosz = k_mul(k_new(0.5,0),(k_add(eiz,k_inv(eiz))));
return cosz;
};
komplex k_sin(komplex z){
komplex eiz = k_exp(k_mul(z,k_i));
komplex sinz = k_mul(k_new(0.5,0),(k_sub(k_inv(eiz),eiz)));
return sinz;
};
#ifndef HAVE_KOMPLEX_H
struct komplex {double re; double im;};
typedef struct komplex komplex;
#define k_i k_new(0,1) //i as a komplex number.
#define k_1 k_new(1,0) //1 as a komplex number.
#define k_pi k_new(M_PI,0) //pi as a komplex number.
#define k_ipi k_new(0,M_PI) //i pi as a komplex number.
void k_print(char* s, komplex z); //Prints a string and the komplex number
void k_set(komplex* z, double x, double y); //Changes the value at the pointer
komplex k_new(double x, double y); //Returns the complex number x+iy
komplex k_add(komplex a, komplex b); //complex addition
komplex k_sub(komplex a, komplex b); //complex subtraction
komplex k_mul(komplex a, komplex b); //complex multiplication
komplex k_inv(komplex a); //returns 1/a, used for division
komplex k_div(komplex a, komplex b); //complex division
komplex k_conj(komplex z); //Returns the complex conjugate
double k_abs(komplex z); //Returns the absoulte value as a double
komplex k_exp(komplex z); //The various functions on komplex data.
komplex k_sin(komplex z);
komplex k_cos(komplex z);
#define HAVE_KOMPLEX_H
#endif
#include<stdio.h>
#include"komplex.h"
int main(void){
komplex x = k_new(13,36);
komplex y = k_new(47,-16);
//k_set(&x,0,0);
printf("\nGiven two complex numbers:\n");
k_print("x = ",x);
k_print("y = ",y);
printf("\nTheir sum is:\n");
k_print("x+y = ",k_add(x,y));
printf("\nTheir difference is:\n");
k_print("x-y = ",k_sub(x,y));
printf("\nTheir product is:\n");
k_print("x*y = ",k_mul(x,y));
printf("\nTheir quotient is:\n");
k_print("x/y = ",k_div(x,y));
printf("\nThey have complex conjugates:\n");
k_print("x* = ",k_conj(x));
k_print("y* = ",k_conj(y));
printf("\nTheir absoulte values are:\n|x| = %g\n|y| = %g\n",k_abs(x),k_abs(y));
printf("\nUsing x and y, various usefull fubnctions have the values:\n");
k_print("exp(x) = ",k_exp(x));
k_print("exp(y) = ",k_exp(y));
k_print("sin(x) = ",k_sin(x));
k_print("sin(y) = ",k_sin(y));
k_print("cos(x) = ",k_cos(x));
k_print("cos(y) = ",k_cos(y));
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment