Advertisement
gabbyshimoni

calculate PI by Leibniz

Dec 3rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. /*
  2.  * Calculate PAI by Leibniz infinite number serie
  3.  * 3.141592653589793238462643383279502884197169399375105820974944592307816406286 ...
  4.  * The program sums with each loop the result of 4*[(-1)^(n+1)]/(2*n-1)
  5.  * to the value of pai
  6.  *
  7.  */
  8. float pai = 0; // the calculated value
  9. float oddNum = 1; // the value of the nominator
  10. int sign = 1;// the sign of the current sum
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14. }
  15.  
  16. void loop() {
  17.   pai = pai + 4 * sign / oddNum;
  18.   sign = -sign;
  19.   oddNum = oddNum + 2;
  20.   Serial.println(pai,10);
  21.  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement