Advertisement
jukaukor

BourweinPlouffePi.cpp

Aug 5th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. /***************************************************************
  2. * Computing pi with BBP formula.
  3. **************************************************************/
  4. #include <math.h>
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. using namespace std;
  10.  
  11. class Bbp
  12. {
  13. // Declaration
  14. int d; // Digits to compute
  15. double pi; // Pi
  16. char pi_hex[14]; // Pi(Hex)
  17. clock_t t0, t1; // Time
  18. double S(int); // Compute S
  19. long compModExp(int, int, int); // Computer Modular Exponentiation
  20. void convHex(double, char[]); // Convert Pi to Hex-string
  21.  
  22. public:
  23. Bbp(int); // Constructor
  24. void compPi(); // Compute PI
  25. };
  26.  
  27. /*
  28. * Constructor
  29. */
  30. Bbp::Bbp(int d)
  31. {
  32. cout << "**** PI Computation ( digit: " << d << " )" << endl;
  33. this->d = d - 1;
  34. }
  35.  
  36. /*
  37. * Compute PI
  38. */
  39. void Bbp::compPi()
  40. {
  41. // Time (start)
  42. t0 = clock();
  43.  
  44. // Compute Pi
  45. pi = 4.0 * S(1) - 2.0 * S(4) - S(5) - S(6);
  46. pi = pi - (int)pi + 1.0;
  47. convHex(pi, pi_hex);
  48. cout << "Hex digit: " << (char)pi_hex[0] << endl;
  49. printf("FRACTION : %.15f\n", pi);
  50. printf("HEX DIGITS: %10.10s\n", pi_hex);
  51.  
  52. // Time (end of computation)
  53. t1 = clock();
  54. cout << "( TIME: " << (double)(t1 - t0) / CLOCKS_PER_SEC
  55. << " seconds )" << endl;
  56. }
  57.  
  58. /*
  59. * Compute S
  60. */
  61. double Bbp::S(int j)
  62. {
  63. double s = 0.0; // Summation of Total, Left
  64. double t; // Each term of right summation
  65. int r; // Denominator
  66. int k; // Loop index
  67. double EPS = 1.0e-17; // Loop-exit accuration of the right summation
  68.  
  69. // Left Sum (0 ... d)
  70. for (k = 0; k <= d; k++) {
  71. r = 8 * k + j;
  72. t = (double)compModExp(16, d - k, r);
  73. t /= r;
  74. s += t - (int)t;
  75. s -= (int)s;
  76. }
  77.  
  78. // Right sum (d + 1 ...)
  79. while (1) {
  80. r = 8 * k + j;
  81. t = pow(16.0, (double)(d - k));
  82. t /= (double)r;
  83. if (t < EPS) break;
  84. s += t;
  85. s -= (int)s;
  86. k ++;
  87. }
  88.  
  89. return s;
  90. }
  91.  
  92. /*
  93. * Compute Modular Exponentiation
  94. */
  95. long Bbp::compModExp(int b, int e, int m)
  96. {
  97. long ans;
  98.  
  99. if (e == 0) return 1;
  100.  
  101. ans = compModExp(b, e / 2, m);
  102. ans = (ans * ans) % m;
  103. if ((e % 2) == 1) ans = (ans * b) % m;
  104.  
  105. return ans;
  106. }
  107.  
  108. /*
  109. * Convert Pi to Hex-strings
  110. */
  111. void Bbp::convHex(double x, char chx[]) {
  112. double y;
  113. int i;
  114. const char hx[] = "0123456789ABCDEF";
  115.  
  116. y = fabs(x);
  117. for (i = 0; i < 16; i++) {
  118. y = 16.0 * (y - floor(y));
  119. chx[i] = hx[(int)(y)];
  120. }
  121. }
  122.  
  123. int main()
  124. {
  125. int d;
  126. cout << "kuinka mones Piin desimaali?";
  127. cin >> d;
  128.  
  129.  
  130. // Instantiation
  131. Bbp objMain(d);
  132.  
  133. // Compute PI
  134. objMain.compPi();
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement