add approximation of pi
This commit is contained in:
commit
6298288d58
2 changed files with 26 additions and 0 deletions
3
Makefile
Normal file
3
Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
CFLAGS=-std=c99
|
||||
|
||||
all: approximate-pi
|
23
approximate-pi.c
Normal file
23
approximate-pi.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
double approximate_pi(unsigned int steps) {
|
||||
double approximate_pi = 4.0;
|
||||
bool subtract = true;
|
||||
|
||||
for(int i=2; i<=steps; i++) {
|
||||
if (subtract) {
|
||||
approximate_pi -= (4.0/(i*2-1));
|
||||
} else {
|
||||
approximate_pi += (4.0/(i*2-1));
|
||||
}
|
||||
subtract = !subtract;
|
||||
}
|
||||
|
||||
return approximate_pi;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// printf("sizeof(int) = %d\n", sizeof(int));
|
||||
printf("pi = %8.7f\n", approximate_pi(10000000));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue