From 6298288d58c54c6005137a28e3791153908bfc51 Mon Sep 17 00:00:00 2001 From: neingeist Date: Thu, 9 May 2013 19:18:10 +0200 Subject: [PATCH] add approximation of pi --- Makefile | 3 +++ approximate-pi.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Makefile create mode 100644 approximate-pi.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..03e1c60 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +CFLAGS=-std=c99 + +all: approximate-pi diff --git a/approximate-pi.c b/approximate-pi.c new file mode 100644 index 0000000..b25a369 --- /dev/null +++ b/approximate-pi.c @@ -0,0 +1,23 @@ +#include +#include + +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)); +}