0

I'm trying to write a code in c to approximate the value of pi using a while loop. I know it is much easier to do so with a for loop but I'm trying to do so using while. the formula I'm using to do so is in link below: https://www.paulbui.net/wl/Taylor_Series_Pi_and_e and the code I wrote looks like this:

#include <stdio.h> #include <math.h> int main(){ long n=10; while(n>0){ double a=0; a+=((pow(-1,n))/((2*n)+1)); n=n-1; printf("%ld",4*a); } return 0; } 

the reason I used long and double type is that I wanted to do the approximation to a good preciseness but first I should do st for this problem. thanks in advance.

8
  • 2
    What's your question? Do you have working code that uses a for loop? If so, it's a simple mechanical process to change it into a while loop. Also, why are you setting a to 0 on every iteration of the loop? Commented Nov 9, 2021 at 7:06
  • 2
    If you want to add up multiple values in your loop, you should not initialize double a=0; in each iteration. Do this before your start the loop. Also it is very uncommon to start the sum from highest value. Finally: The sum should go from n=0, 1, 2.... while you exclude n==0 Commented Nov 9, 2021 at 7:08
  • 3
    Also, the proper format specifier for double is %f. Commented Nov 9, 2021 at 7:10
  • 4
    BTW, using (pow(-1,n) is a terrible idea... The idiomatic way if to initialize a int coeff=1; and then in the loop use coeff = -coeff;. Much simpler... Commented Nov 9, 2021 at 7:18
  • 1
    @Gerhardh It's not really that uncommon to start from the highest index. Please note that there can be numerical reasons to start adding the smaller values so that their values are comparable in magnitudes to the growing sum. In this case, though, the sum is telescopic and the previous trick doesn't really reduces the propagation of errors. It may be worth trying to add the positive separately from the negative terms and only then subtract the two sums. Commented Nov 9, 2021 at 9:17

2 Answers 2

1

You have to move a initialization before loop and make stop condition - for example, evaluating current summand. Also it is worth to calculate sign incrementally without using pow:

double a=0; double eps= 1.0e-6; //note this series has rather slow convergence n = 0; double tx = 1.0; double t = 1.0; while(abs(tx)>eps){ tx = t / (2*n+1)); a+= tx; printf("%f",4*a); n++; t = - t; } 
Sign up to request clarification or add additional context in comments.

Comments

0

The posted code does not cleanly compile!

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o" untitled.c: In function ‘main’: untitled.c:7:19: warning: conversion from ‘long int’ to ‘double’ may change value [-Wconversion] 7 | a+=((pow(-1,n))/((2*n)+1)); | ^ untitled.c:7:22: warning: conversion from ‘long int’ to ‘double’ may change value [-Wconversion] 7 | a+=((pow(-1,n))/((2*n)+1)); | ^ untitled.c:9:17: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘double’ [-Wformat=] 9 | printf("%ld",4*a); | ~~^ ~~~ | | | | | double | long int | %f 

Compilation finished successfully.

Note: when there are warnings, fix those warnings. Also, when there are warnings, the compiler outputs it's best guess which is not necessarily what you wanted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.