?In C language!!!! Calculate e. Write a program to calculate the mathematical co
ID: 3576824 • Letter: #
Question
?In C language!!!!
Calculate e. Write a program to calculate the mathematical constant e using the following series. You should put your code into a function having the following prototype: float calculate_e(void); Use the series: e = 1 + 1/1! + 1/2! + 1/3! + Your output should be in the form of a table listing the index of each term, the value of that particular term, and the current value of e. Print your floats using 30 digits after the decimal point. Run your program for at least 50 iterations. In your paper output, use a highlighter pen to indicate which values OVERFLOW or UNDERFLOW and when this happens. Which overflows FIRST, the int term or the float e ???Explanation / Answer
#include<iostream>
using namespace std;
size_t highestOneBitPosition(int a) {
size_t bits=0;
while (a>0) {
++bits;
a>>=1;
};
return bits;
}
float calculate_e(void){
float e = 0;
for(int i = 1; i <= 50; i++)
{
int result = 1;
for(int j = 1; j <= i; j++)
{
size_t a_bits=highestOneBitPosition(result), b_bits=highestOneBitPosition(j);
if(a_bits+b_bits>32)
cout << " Int overflow Detected ";
result = result * j;
}
e += 1 / (float)result;
cout << i << " " << result << " " << e << endl;
}
}
int main(){
cout.precision(30);
cout << "Final value of e " << calculate_e();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.