Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<iomanip>
  4.  
  5. using namespace std;
  6.  
  7. // double constant to store the sq root to 60 decimal places
  8. const double sq_rt_2 = 1.41421356237309504880;
  9.  
  10. // function to calculate the factorial using double type
  11. double factorial1(int n)
  12. {
  13. if (n == 0)
  14. return (double)1;
  15. return (double)n * factorial1((double)(n - 1));
  16. }
  17.  
  18. // function to calculate the factorial using float type
  19. float factorial2(int n)
  20. {
  21. if (n == 0)
  22. return (float)1;
  23. return (float)n * factorial2((float)(n - 1));
  24. }
  25.  
  26. // function to get the terms of square root power series using double type
  27. void getSqRootTermsWithDouble(double terms[], int n)
  28. {
  29. int k;
  30. double sum = 0;
  31. for (k = 0; k < n; k++)
  32. {
  33. double term = factorial1(2 * k + 1) / (pow(2, (3 * k + 1)) * pow(factorial1(k), 2));
  34. terms[k] = term;
  35. }
  36. }
  37.  
  38. // function to get the terms of square root power series using float type
  39. void getSqRootTermsWithFloat(float terms[], int n)
  40. {
  41. int k;
  42. float sum = 0;
  43. for (k = 0; k < n; k++)
  44. {
  45. float term = factorial2(2 * k + 1) / (pow(2, (3 * k + 1)) * pow(factorial2(k), 2));
  46. terms[k] = term;
  47. }
  48. }
  49.  
  50. // function to print the forward and back approximation sum of sq root 2
  51. // along with % error of double type
  52. void printSqRoot2Double(double terms[], int n)
  53. {
  54. double forwardSum = 0.0;
  55. double backSum = 0.0;
  56. int i;
  57. for (i = 0; i < n; i++)
  58. {
  59. // check for NaN or inf values
  60. if (!(isnan(abs(terms[i])) || isinf(terms[i])))
  61. forwardSum += terms[i];
  62.  
  63. }
  64.  
  65. for (i = n - 1; i >= 0; i--)
  66. {
  67. // check for NaN or inf values
  68. if (!(isnan(abs(terms[i])) || isinf(terms[i])))
  69. backSum += terms[i];
  70. }
  71.  
  72. double forwardErr = abs(forwardSum - sq_rt_2) * 100 / sq_rt_2;
  73. double backErr = abs(backSum - sq_rt_2) * 100 / sq_rt_2;
  74.  
  75. cout << "Forward Approx: " << forwardSum << endl;
  76. cout << "Forward %error: " << forwardErr << endl;
  77. cout << "Backward Approx: " << backSum << endl;
  78. cout << "Backward %error: " << backErr << endl;
  79. }
  80.  
  81. // function to print the forward and back approximation sum of sq root 2
  82. // along with % error of float type
  83. void printSqRoot2Float(float terms[], int n)
  84. {
  85. float forwardSum = 0.0;
  86. float backSum = 0.0;
  87. int i;
  88. for (i = 0; i < n; i++)
  89. {
  90. // check for NaN or inf values
  91. if (!(isnan(terms[i]) || isinf(terms[i])))
  92. forwardSum += terms[i];
  93.  
  94. }
  95.  
  96.  
  97. for (i = n - 1; i >= 0; i--)
  98. {
  99. // check for NaN or inf values
  100. if (!(isnan(terms[i]) || isinf(terms[i])))
  101. backSum += terms[i];
  102. }
  103.  
  104.  
  105. float forwardErr = abs(forwardSum - sq_rt_2) * 100 / sq_rt_2;
  106. float backErr = abs(backSum - sq_rt_2) * 100 / sq_rt_2;
  107.  
  108. cout << "Forward Approx: " << forwardSum << endl;
  109. cout << "Forward %error: " << forwardErr << endl;
  110. cout << "Backward Approx: " << backSum << endl;
  111. cout << "Backward %error: " << backErr << endl;
  112. }
  113.  
  114. // main function
  115. int main()
  116. {
  117. int n = 80;
  118. float fTerms[20];
  119. double dTerms[80];
  120. getSqRootTermsWithFloat(fTerms, n);
  121. getSqRootTermsWithDouble(dTerms, n);
  122. cout << "Float Results:" << endl;
  123. cout << "-------------" << endl;
  124. // setting the precision to 20 decimal places
  125. cout << fixed << setprecision(20);
  126. printSqRoot2Float(fTerms, n);
  127. cout << endl;
  128. cout << "Double Results:" << endl;
  129. cout << "--------------" << endl;
  130. printSqRoot2Double(dTerms, n);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement