Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You should know from ECE2020 that a digital representation of certain real numbe

ID: 3745838 • Letter: Y

Question

You should know from ECE2020 that a digital representation of certain real numbers is only an approximation. For example, irrational numbers such as , e, v2, etc.. have only a finite number of digits that can be represented in a digital computer. The two main representations that we will use in C++ will be single precision (32-bit) and double precision (64-bits) numbers. In each of the number formats, a certain number of bits is dedicated to the exponent (8 bits for single precision and 11 bits for double precision), the mantissa (23 bits for single precision and 52 bits for double precision), and 1 bit for the sign bit. I would like for you to use a C++ program to calculate the roots of the following quadratic equation x2 + 3000.00|x + 3 = 0 Please use the following conventional quadratic equation in your program to solve for the roots In fact, I would like for you to use a global function that has four parameters that you pass to it (i.e. a flag for plus or minus, a, b, and c). You should make one function using only float's and the other only double's so that you can compare the results. To calculate the square root, you can use the sqrt(x) function that is a part of the cmath library. To include this in your program you must have the following preprocessor directive #include The actual exact roots of this equation are X1 =0.001 and X2 =-3000. Please compare how your program calculates this with first float types and then with doubles. You can calculate the error given by % error-100"(actual -approximation)/actual In your Labl directory, please call your source code lablpart3.cc. Possible sample output is as follows Using the float data type the roots are x1- % error-you calculate> x2 = % error-you calculate> Using the double data type the roots are x1 = % error-you calculate> x2- % error-you calculate>

Explanation / Answer

ScreenShot

--------------------------------------------------

Program

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
//Function prototype
float rootFindingFloat(bool, double, double, double);
double rootFindingDouble(bool, double, double, double);
int main()
{
   //Vaariables for calculation and result
   double x1, x2, actualX1 = -0.001, actualX2 = -3000,error1,error2;
   float x3, x4;
   //Float display
   cout << "Using float data type the roots are:" << endl;
   //Call function float
   x3 = rootFindingFloat(true, 1, 3000.001, 3);
   x4= rootFindingFloat(false, 1, 3000.001, 3);
   //Error calculator
   error1 = 100 * (actualX1 - x3) / actualX1;
   error2=100 * (actualX2 - x4) / actualX2;
   cout << "X1=" << x3 << "     % error=" << error1 << endl;
   cout << "X2=" << x4 << "     % error=" << error2 << endl;
   //Double display
   cout << "Using double data type the roots are:" << endl;
   x1 = rootFindingDouble(true, 1, 3000.001, 3);
   x2 = rootFindingDouble(false, 1, 3000.001, 3);
   error1 = 100 * (actualX1 - x1) / actualX1;
   error2 = 100 * (actualX2 - x2) / actualX2;
   cout << "X1=" << x1 << "     % error=" << error1 << endl;
   cout << "X2=" << x2 << "     % error=" << error2 << endl;
  
    return 0;
}
//Float root finding function
float rootFindingFloat(bool flag, double a, double b, double c) {
   if (flag == true) {
       return (-b + (sqrt((b*b) - (4 * a*c)))) / (2 * a);
   }
   else {
       return (-b - (sqrt((b*b) - (4 * a*c)))) / (2 * a);
   }
}
//Double root finding function
double rootFindingDouble(bool flag, double a, double b, double c) {
   if (flag == true) {
       return (-b + (sqrt((b*b) - (4 * a*c)))) / (2 * a);
   }
   else {
       return (-b - (sqrt((b*b) - (4 * a*c)))) / (2 * a);
   }
}

-------------------------------------------

Output

Using float data type the roots are:
X1=-0.001     % error=-4.74975e-06
X2=-3000     % error=-0
Using double data type the roots are:
X1=-0.001     % error=2.36469e-09
X2=-3000     % error=-0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote