Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- #include<iomanip>
- using namespace std;
- // double constant to store the sq root to 60 decimal places
- const double sq_rt_2 = 1.41421356237309504880;
- // function to calculate the factorial using double type
- double factorial1(int n)
- {
- if (n == 0)
- return (double)1;
- return (double)n * factorial1((double)(n - 1));
- }
- // function to calculate the factorial using float type
- float factorial2(int n)
- {
- if (n == 0)
- return (float)1;
- return (float)n * factorial2((float)(n - 1));
- }
- // function to get the terms of square root power series using double type
- void getSqRootTermsWithDouble(double terms[], int n)
- {
- int k;
- double sum = 0;
- for (k = 0; k < n; k++)
- {
- double term = factorial1(2 * k + 1) / (pow(2, (3 * k + 1)) * pow(factorial1(k), 2));
- terms[k] = term;
- }
- }
- // function to get the terms of square root power series using float type
- void getSqRootTermsWithFloat(float terms[], int n)
- {
- int k;
- float sum = 0;
- for (k = 0; k < n; k++)
- {
- float term = factorial2(2 * k + 1) / (pow(2, (3 * k + 1)) * pow(factorial2(k), 2));
- terms[k] = term;
- }
- }
- // function to print the forward and back approximation sum of sq root 2
- // along with % error of double type
- void printSqRoot2Double(double terms[], int n)
- {
- double forwardSum = 0.0;
- double backSum = 0.0;
- int i;
- for (i = 0; i < n; i++)
- {
- // check for NaN or inf values
- if (!(isnan(abs(terms[i])) || isinf(terms[i])))
- forwardSum += terms[i];
- }
- for (i = n - 1; i >= 0; i--)
- {
- // check for NaN or inf values
- if (!(isnan(abs(terms[i])) || isinf(terms[i])))
- backSum += terms[i];
- }
- double forwardErr = abs(forwardSum - sq_rt_2) * 100 / sq_rt_2;
- double backErr = abs(backSum - sq_rt_2) * 100 / sq_rt_2;
- cout << "Forward Approx: " << forwardSum << endl;
- cout << "Forward %error: " << forwardErr << endl;
- cout << "Backward Approx: " << backSum << endl;
- cout << "Backward %error: " << backErr << endl;
- }
- // function to print the forward and back approximation sum of sq root 2
- // along with % error of float type
- void printSqRoot2Float(float terms[], int n)
- {
- float forwardSum = 0.0;
- float backSum = 0.0;
- int i;
- for (i = 0; i < n; i++)
- {
- // check for NaN or inf values
- if (!(isnan(terms[i]) || isinf(terms[i])))
- forwardSum += terms[i];
- }
- for (i = n - 1; i >= 0; i--)
- {
- // check for NaN or inf values
- if (!(isnan(terms[i]) || isinf(terms[i])))
- backSum += terms[i];
- }
- float forwardErr = abs(forwardSum - sq_rt_2) * 100 / sq_rt_2;
- float backErr = abs(backSum - sq_rt_2) * 100 / sq_rt_2;
- cout << "Forward Approx: " << forwardSum << endl;
- cout << "Forward %error: " << forwardErr << endl;
- cout << "Backward Approx: " << backSum << endl;
- cout << "Backward %error: " << backErr << endl;
- }
- // main function
- int main()
- {
- int n = 80;
- float fTerms[20];
- double dTerms[80];
- getSqRootTermsWithFloat(fTerms, n);
- getSqRootTermsWithDouble(dTerms, n);
- cout << "Float Results:" << endl;
- cout << "-------------" << endl;
- // setting the precision to 20 decimal places
- cout << fixed << setprecision(20);
- printSqRoot2Float(fTerms, n);
- cout << endl;
- cout << "Double Results:" << endl;
- cout << "--------------" << endl;
- printSqRoot2Double(dTerms, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement