having problem storing a number, the program gives us a completley wrong answer
ID: 3793829 • Letter: H
Question
having problem storing a number, the program gives us a completley wrong answer compared to what the calculators give us given that the program gives us the wrong answer, probably because its not storing the whole number because its too big
Answer should be ; Aproximatelly 3.990454x10^14 but the program gives us 553402642
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
long long periodTime;
const double UNIVERSAL_GRAVITATION = 0.00000000006673;
const long long int EARTH_MASS = 5980000000000000000000000;
periodTime = (UNIVERSAL_GRAVITATION * EARTH_MASS);
cout << periodTime << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
//Earth mass is too large for supporting in a single variable
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
long double periodTime;
const long double UNIVERSAL_GRAVITATION = 0.00000000006673;
const long double EARTH_MASS = 598.0;
const int EARTH_MASS_EXP = 22;
int c=0;
periodTime = (UNIVERSAL_GRAVITATION * EARTH_MASS);
for(int i=1;i<=EARTH_MASS_EXP;i++)
{
while(periodTime<1){
periodTime*=10.0;
c++;
}
}
cout << periodTime <<"+e^"<<(EARTH_MASS_EXP-c)<< endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.