Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start main()

Try stepping through the code


  Value and type of last evaluated expression: Address of array element: Address of array elementx:  (none)

Your browser does not support the canvas element which will make some the features unavailable.

If you are using Internet Explorer within the University of Exeter try going to the Settings menu (probably the gear shape at the top right of this page), selecting "Compatibility View settings", unchecking "Display intranet sites in Compatibility View" and reloading this page.

Code

 Header...
double fastexp(double base,int exponent){
  ...
» unsigned int ord=1; » int bitsdone=0;// Diagnostic » float result=1.0; » if (exponent<0) { » exponent=-exponent; » base=1.0/base; } » for ( float e2=base;; e2*=e2) { » if (exponent&ord) { » ++bitsdone;// Just out of interest » result*=e2; } » ord<<=1; // We do the test here to avoid an extra multiplication » if (ord>exponent) break; } » printf("%d bits used\n", bitsdone); » if (!isfinite(result)) printf("Warning: under or overflow\n"); » return result;
} int main(){
  ...
» double base, res1, res2; » int exponent=-3; » while (1) { » printf("Please enter the base and exponent\n" "make the exponent == 0 to quit\n"); » scanf("%lg %d", &base, &exponent); » if (exponent==0) break; » res1=fastexp(base, exponent); // For testing: » res2=pow(base, exponent); » printf("%.12g %.12g (diff=%.12g)\n", res1, res2, res1-res2); } » return 0;
}
Memory

fastexp()

128
129
130
131
132
133
134
135
base = 0.5
144
145
146
147
148
149
150
151
?
?
?
?
ord = 1
152
153
154
155
156
157
158
159
bitsdone = 0
result = 1
192
193
194
195
exponent = -13

fastexp()

128
129
130
131
132
133
134
135
base = 10
144
145
146
147
148
149
150
151
?
?
?
?
ord = 1
152
153
154
155
156
157
158
159
bitsdone = 0
result = 1
192
193
194
195
exponent = 9

fastexp()

128
129
130
131
132
133
134
135
base = 1.0000001
144
145
146
147
148
149
150
151
?
?
?
?
ord = 1
152
153
154
155
156
157
158
159
bitsdone = 0
result = 1
192
193
194
195
exponent = 1234567890

fastexp()

128
129
130
131
132
133
134
135
base = 1e-30
144
145
146
147
148
149
150
151
?
?
?
?
ord = 1
152
153
154
155
156
157
158
159
bitsdone = 0
result = 1
192
193
194
195
exponent = -13

fastexp(): for()

160
161
162
163
e2 = 1.00000002e+30

fastexp(): for()

160
161
162
163
e2 = 10

fastexp(): for()

160
161
162
163
e2 = 1.00000012

fastexp(): for()

160
161
162
163
e2 = 2

main()

228
229
230
231
exponent = -3
232
233
234
235
236
237
238
239
base = -2.05155976e+267
240
241
242
243
244
245
246
247
res1 = -8.57014155e+266
248
249
250
251
252
253
254
255
res2 = 4.94714252e-270

NB: the actual memory address of each variable is the
address shown plus 4289324584 (0xFFA9E628).

Show Advanced options

.

Input was:

1.0000001 1234567890
10 9 
0.5 -13 
1e-30 -13 
3 0

Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

Please enter the base and exponent
make the exponent == 0 to quit
12 bits used
Warning: under or overflow
inf 4.13618129872e+53 (diff=inf)
Please enter the base and exponent
make the exponent == 0 to quit
2 bits used
1000000000 1000000000 (diff=0)
Please enter the base and exponent
make the exponent == 0 to quit
3 bits used
8192 8192 (diff=0)
Please enter the base and exponent
make the exponent == 0 to quit
3 bits used
Warning: under or overflow
inf inf (diff=-nan)
Please enter the base and exponent
make the exponent == 0 to quit
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top