Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
126 ira 1
/* Copyright: Ira W. Snyder
2
 * Start Date: 2005-09-28
3
 * End Date: 2005-10-12
4
 * License: Public Domain
5
 *
6
 * Changelog Follows:
7
 *
8
 * 2005-09-28:
9
 * - Added the code to calculate the change in volumes in single
10
 *   and double precision.
11
 *
12
 * 2005-09-29:
13
 * - Added the code to calculate the volumes in single and double precision.
14
 * - Added the code to calculate the change in volumes using the surface area
15
 *   method, in single and double precision.
16
 *
17
 * 2005-10-12:
18
 * - Prettified output.
19
 * - Code cleanups.
20
 * - Added "Change in vol" subtraction in Part 1.
21
 * - Print the radii in Part 2.
22
 *
23
 */
24
 
25
#include <cstdio>
26
#include <cstdlib>
27
#include <cmath>
28
using namespace std;
29
 
30
#define PI 3.14159265358979323846264
31
 
32
float volume_single (float radius)
33
{
34
    /* Implements:
35
     * Vol = (4/3) * PI * r^3
36
     * in single precision. */
37
 
38
    float vol = (4.0F/3.0F) * (float)PI;
39
    vol *= powf (radius, 3.0);
40
 
41
    return vol;
42
}
43
 
44
float volume_double (double radius)
45
{
46
    /* Implements:
47
     * Vol = (4/3) * PI * r^3
48
     * in double precision. */
49
 
50
    double vol = (4.0F/3.0F) * (double)PI;
51
    vol *= pow (radius, 3.0);
52
 
53
    return vol;
54
}
55
 
56
float change_volume_single (float rad_old, float rad_new)
57
{
58
    /* Implements:
59
     * Change_in_vol = (4/3) * PI * (r_new - r_old) * (r_new^2 + r_new*r_old + r_old^2)
60
     * in single precision. */
61
 
62
    float vol = (4.0F/3.0F) * (float)PI;
63
    vol *= (rad_new - rad_old);
64
    vol *= (powf (rad_new, 2.0) + (rad_new * rad_old) + powf (rad_old, 2.0));
65
 
66
    return vol;
67
}
68
 
69
double change_volume_double (double rad_old, double rad_new)
70
{
71
    /* Implements:
72
     * Change_in_vol = (4/3) * PI * (r_new - r_old) * (r_new^2 + r_new*r_old + r_old^2)
73
     * in double precision. */
74
 
75
    double vol = (4.0F/3.0F) * (double)PI;
76
    vol *= (rad_new - rad_old);
77
    vol *= (pow (rad_new, 2.0) + (rad_new * rad_old) + pow (rad_old, 2.0));
78
 
79
    return vol;
80
}
81
 
82
float approx_change_volume_single (float radius, float r_change)
83
{
84
    /* Implements:
85
     * Change_in_vol = 4 * PI * r_old^2 * r_change
86
     * in single precision; */
87
 
88
    float vol = 4.0F * (float)PI * powf(radius, 2.0) * r_change;
89
 
90
    return vol;
91
}
92
 
93
double approx_change_volume_double (double radius, double r_change)
94
{
95
    /* Implements:
96
     * Change_in_vol = 4 * PI * r_old^2 * r_change
97
     * in double precision; */
98
 
99
    double vol = 4.0F * (double)PI * pow(radius, 2.0) * r_change;
100
 
101
    return vol;
102
}
103
 
104
int main (void)
105
{
106
    /* Constants */
107
    const float  r_change_single  = 1.0F / 12.0F / 5280.0F; //convert 0.1 inch to miles
108
    const double r_change_double  = 1.0  / 12.0  / 5280.0;  //convert 0.1 inch to miles
109
    const double rad_earth_1900   = 4000.0;
110
    const float  rad_earth_2000_s = rad_earth_1900 + r_change_single;
111
    const double rad_earth_2000_d = rad_earth_1900 + r_change_double;
112
 
113
    float temp_f1, temp_f2;
114
    double temp_d1, temp_d2;
115
 
116
    /* Part 1: Volumes in single and double precision */
117
    printf ("Part 1: Volumes in single and double precision\n");
118
    printf ("============================================================\n");
119
    printf ("Single Precision:\n");
120
 
121
    temp_f1 = volume_single (rad_earth_1900);
122
    printf ("Volume of the Earth in 1900AD: %.20e\n", temp_f1);
123
 
124
    temp_f2 = volume_single (rad_earth_2000_s);
125
    printf ("Volume of the Earth in 2000AD: %.20e\n", temp_f2);
126
 
127
    printf ("Change in volume: %.20e cubic miles\n\n", temp_f2 - temp_f1);
128
 
129
    printf ("Double Precision:\n");
130
 
131
    temp_d1 = volume_double (rad_earth_1900);
132
    printf ("Volume of the Earth in 1900AD: %.20e\n", temp_d1);
133
 
134
    temp_d2 = volume_double (rad_earth_2000_d);
135
    printf ("Volume of the Earth in 2000AD: %.20e\n", temp_d2);
136
 
137
    printf ("Change in volume: %.20e cubic miles\n\n", temp_d2 - temp_d1);
138
 
139
    /* Part 2: Change in volume of the Earth in single and double precision */
140
    printf ("Part 2: Change in volume of the Earth in single and double precision\n");
141
    printf ("============================================================\n");
142
    printf ("Single Precision:\n");
143
 
144
    printf ("Radius of the Earth in 1900AD: %.20e\n", rad_earth_1900);
145
    printf ("Radius of the Earth in 2000AD: %.20e\n", rad_earth_2000_s);
146
 
147
    temp_f1 = change_volume_single (rad_earth_1900, rad_earth_2000_s);
148
    printf ("Change in vol = %.20e cubic miles\n\n", temp_f1);
149
 
150
    printf ("Double Precision:\n");
151
 
152
    printf ("Radius of the Earth in 1900AD: %.20e\n", rad_earth_1900);
153
    printf ("Radius of the Earth in 2000AD: %.20e\n", rad_earth_2000_d);
154
 
155
    temp_d1 = change_volume_double (rad_earth_1900, rad_earth_2000_d);
156
    printf ("Change in vol = %.20e cubic miles\n\n", temp_d1);
157
 
158
    /* Part 3: Approximation of the change in volume using the surface area method,
159
     * in single and double precision */
160
    printf ("Part 3: Approximation of the change in volume of the Earth using\n");
161
    printf ("the surface area method, in single and double precision\n");
162
    printf ("============================================================\n");
163
    printf ("Single Precision:\n");
164
 
165
    temp_f1 = approx_change_volume_single (rad_earth_1900, r_change_single);
166
    printf ("Change in vol = %.20e cubic miles\n\n", temp_f1);
167
 
168
    printf ("Double Precision:\n");
169
 
170
    temp_d1 = approx_change_volume_double (rad_earth_1900, r_change_double);
171
    printf ("Change in vol = %.20e cubic miles\n\n", temp_d1);
172
 
173
    return 0;
174
}
175