Please explain what this code means. How would the results differet from expecta
ID: 3737020 • Letter: P
Question
Please explain what this code means. How would the results differet from expectations?
#include <iostream>
#include <cmath>
using namespace std;
#ifndef M_PI
#define M_PI (4.0 * std::atan2(1.0, 1.0))
#endif
int main() {
int ngon = 3;
double insngon = 0.0 ;
double cirngon = 0.0;
cout << "N" << " " << "ins-ngon" << " " << "cirngon" << " " << " ";
while(ngon <= 30)
{
insngon = 2*ngon*sin(M_PI/ngon);
cirngon = 2*ngon*tan(M_PI/ngon) ;
printf("%d %.14lf %.14lf ",ngon,insngon,cirngon);
ngon++;
if(cirngon-insngon <= pow(10.0,-15)) break;
}
}
Explanation / Answer
Here the M_PI is fixed with a particular value
The table goes for the polygon sides less than equal to 30.And here the loop continues until (cirngon-insngon <= pow(10.0,-15) ) is not satisfied .
THe actual result may differ in case if the radian value is considered for the calculation.