Blame | Last modification | View Log | RSS feed
/* Copyright: Ira W. Snyder* Start Date: 2005-09-28* End Date: 2005-10-12* License: Public Domain** Changelog Follows:** 2005-09-28:* - Added the code to calculate the change in volumes in single* and double precision.** 2005-09-29:* - Added the code to calculate the volumes in single and double precision.* - Added the code to calculate the change in volumes using the surface area* method, in single and double precision.** 2005-10-12:* - Prettified output.* - Code cleanups.* - Added "Change in vol" subtraction in Part 1.* - Print the radii in Part 2.**/#include <cstdio>#include <cstdlib>#include <cmath>using namespace std;#define PI 3.14159265358979323846264float volume_single (float radius){/* Implements:* Vol = (4/3) * PI * r^3* in single precision. */float vol = (4.0F/3.0F) * (float)PI;vol *= powf (radius, 3.0);return vol;}float volume_double (double radius){/* Implements:* Vol = (4/3) * PI * r^3* in double precision. */double vol = (4.0F/3.0F) * (double)PI;vol *= pow (radius, 3.0);return vol;}float change_volume_single (float rad_old, float rad_new){/* Implements:* Change_in_vol = (4/3) * PI * (r_new - r_old) * (r_new^2 + r_new*r_old + r_old^2)* in single precision. */float vol = (4.0F/3.0F) * (float)PI;vol *= (rad_new - rad_old);vol *= (powf (rad_new, 2.0) + (rad_new * rad_old) + powf (rad_old, 2.0));return vol;}double change_volume_double (double rad_old, double rad_new){/* Implements:* Change_in_vol = (4/3) * PI * (r_new - r_old) * (r_new^2 + r_new*r_old + r_old^2)* in double precision. */double vol = (4.0F/3.0F) * (double)PI;vol *= (rad_new - rad_old);vol *= (pow (rad_new, 2.0) + (rad_new * rad_old) + pow (rad_old, 2.0));return vol;}float approx_change_volume_single (float radius, float r_change){/* Implements:* Change_in_vol = 4 * PI * r_old^2 * r_change* in single precision; */float vol = 4.0F * (float)PI * powf(radius, 2.0) * r_change;return vol;}double approx_change_volume_double (double radius, double r_change){/* Implements:* Change_in_vol = 4 * PI * r_old^2 * r_change* in double precision; */double vol = 4.0F * (double)PI * pow(radius, 2.0) * r_change;return vol;}int main (void){/* Constants */const float r_change_single = 1.0F / 12.0F / 5280.0F; //convert 0.1 inch to milesconst double r_change_double = 1.0 / 12.0 / 5280.0; //convert 0.1 inch to milesconst double rad_earth_1900 = 4000.0;const float rad_earth_2000_s = rad_earth_1900 + r_change_single;const double rad_earth_2000_d = rad_earth_1900 + r_change_double;float temp_f1, temp_f2;double temp_d1, temp_d2;/* Part 1: Volumes in single and double precision */printf ("Part 1: Volumes in single and double precision\n");printf ("============================================================\n");printf ("Single Precision:\n");temp_f1 = volume_single (rad_earth_1900);printf ("Volume of the Earth in 1900AD: %.20e\n", temp_f1);temp_f2 = volume_single (rad_earth_2000_s);printf ("Volume of the Earth in 2000AD: %.20e\n", temp_f2);printf ("Change in volume: %.20e cubic miles\n\n", temp_f2 - temp_f1);printf ("Double Precision:\n");temp_d1 = volume_double (rad_earth_1900);printf ("Volume of the Earth in 1900AD: %.20e\n", temp_d1);temp_d2 = volume_double (rad_earth_2000_d);printf ("Volume of the Earth in 2000AD: %.20e\n", temp_d2);printf ("Change in volume: %.20e cubic miles\n\n", temp_d2 - temp_d1);/* Part 2: Change in volume of the Earth in single and double precision */printf ("Part 2: Change in volume of the Earth in single and double precision\n");printf ("============================================================\n");printf ("Single Precision:\n");printf ("Radius of the Earth in 1900AD: %.20e\n", rad_earth_1900);printf ("Radius of the Earth in 2000AD: %.20e\n", rad_earth_2000_s);temp_f1 = change_volume_single (rad_earth_1900, rad_earth_2000_s);printf ("Change in vol = %.20e cubic miles\n\n", temp_f1);printf ("Double Precision:\n");printf ("Radius of the Earth in 1900AD: %.20e\n", rad_earth_1900);printf ("Radius of the Earth in 2000AD: %.20e\n", rad_earth_2000_d);temp_d1 = change_volume_double (rad_earth_1900, rad_earth_2000_d);printf ("Change in vol = %.20e cubic miles\n\n", temp_d1);/* Part 3: Approximation of the change in volume using the surface area method,* in single and double precision */printf ("Part 3: Approximation of the change in volume of the Earth using\n");printf ("the surface area method, in single and double precision\n");printf ("============================================================\n");printf ("Single Precision:\n");temp_f1 = approx_change_volume_single (rad_earth_1900, r_change_single);printf ("Change in vol = %.20e cubic miles\n\n", temp_f1);printf ("Double Precision:\n");temp_d1 = approx_change_volume_double (rad_earth_1900, r_change_double);printf ("Change in vol = %.20e cubic miles\n\n", temp_d1);return 0;}