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 fp2_(double base,int exponent){
  ...
» if (exponent==1) return base; else if (exponent%2==0) return fp2_(base*base, exponent/2); » return base*fp2_(base*base, exponent/2);
} double fp2(double base,int exponent){
  ...
» if (exponent<0) { » exponent=-exponent; » base=1.0/base; } else if (exponent==0) return 1; » return fp2_(base, exponent);
} double fastexp(double base,int exponent){
  ...
» unsigned int ord=1; » int bitsdone=0;// Diagnostic » double result=1.0; » if (exponent<0) { » exponent=-exponent; » base=1.0/base; } » for ( double 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, resok, resa; » 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); » resa=fp2(base, exponent); // For testing: » resok=pow(base, exponent); » printf("%.12g %.12g (diff=%.12g)\n", res1, resok, res1-resok); » printf("%.12g %.12g (diff=%.12g)\n", resa, resok, resa-resok); » printf("%.12g\n", resa-res1); } » return 0;
}
Memory

fp2_-31()

128
129
130
131
132
133
134
135
base = 4.28560962e+46
160
161
162
163
exponent = 1

fp2_-30()

192
193
194
195
196
197
198
199
base = 2.0701714e+23
224
225
226
227
exponent = 2

fp2_-29()

256
257
258
259
260
261
262
263
base = 4.54991362e+11
288
289
290
291
exponent = 4

fp2_-28()

320
321
322
323
324
325
326
327
base = 674530.476
352
353
354
355
exponent = 9

fp2_-27()

384
385
386
387
388
389
390
391
base = 821.298043
416
417
418
419
exponent = 18

fp2_-26()

448
449
450
451
452
453
454
455
base = 28.658298
480
481
482
483
exponent = 36

fp2_-25()

512
513
514
515
516
517
518
519
base = 5.35334456
544
545
546
547
exponent = 73

fp2_-24()

576
577
578
579
580
581
582
583
base = 2.31372958
608
609
610
611
exponent = 147

fp2_-23()

640
641
642
643
644
645
646
647
base = 1.52109486
672
673
674
675
exponent = 294

fp2_-22()

704
705
706
707
708
709
710
711
base = 1.23332675
736
737
738
739
exponent = 588

fp2_-21()

768
769
770
771
772
773
774
775
base = 1.11055245
800
801
802
803
exponent = 1177

fp2_-20()

832
833
834
835
836
837
838
839
base = 1.05382752
864
865
866
867
exponent = 2354

fp2_-19()

896
897
898
899
900
901
902
903
base = 1.02656102
928
929
930
931
exponent = 4709

fp2_-18()

960
961
962
963
964
965
966
967
base = 1.01319348
992
993
994
995
exponent = 9419

fp2_-17()

1024
1025
1026
1027
1028
1029
1030
1031
base = 1.00657512
1056
1057
1058
1059
exponent = 18838

fp2_-16()

1088
1089
1090
1091
1092
1093
1094
1095
base = 1.00328217
1120
1121
1122
1123
exponent = 37676

fp2_-15()

1152
1153
1154
1155
1156
1157
1158
1159
base = 1.00163974
1184
1185
1186
1187
exponent = 75352

fp2_-14()

1216
1217
1218
1219
1220
1221
1222
1223
base = 1.00081954
1248
1249
1250
1251
exponent = 150704

fp2_-13()

1280
1281
1282
1283
1284
1285
1286
1287
base = 1.00040968
1312
1313
1314
1315
exponent = 301408

fp2_-12()

1344
1345
1346
1347
1348
1349
1350
1351
base = 1.00020482
1376
1377
1378
1379
exponent = 602816

fp2_-11()

1408
1409
1410
1411
1412
1413
1414
1415
base = 1.00010241
1440
1441
1442
1443
exponent = 1205632

fp2_-10()

1472
1473
1474
1475
1476
1477
1478
1479
base = 1.0000512
1504
1505
1506
1507
exponent = 2411265

fp2_-9()

1536
1537
1538
1539
1540
1541
1542
1543
base = 1.0000256
1568
1569
1570
1571
exponent = 4822530

fp2_-8()

1600
1601
1602
1603
1604
1605
1606
1607
base = 1.0000128
1632
1633
1634
1635
exponent = 9645061

fp2_-7()

1664
1665
1666
1667
1668
1669
1670
1671
base = 1.0000064
1696
1697
1698
1699
exponent = 19290123

fp2_-6()

1728
1729
1730
1731
1732
1733
1734
1735
base = 1.0000032
1760
1761
1762
1763
exponent = 38580246

fp2_-5()

1792
1793
1794
1795
1796
1797
1798
1799
base = 1.0000016
1824
1825
1826
1827
exponent = 77160493

fp2_-4()

1856
1857
1858
1859
1860
1861
1862
1863
base = 1e+240
1888
1889
1890
1891
exponent = 1

fp2_-4()

1856
1857
1858
1859
1860
1861
1862
1863
base = 100000000
1888
1889
1890
1891
exponent = 1

fp2_-4()

1856
1857
1858
1859
1860
1861
1862
1863
base = 1.0000008
1888
1889
1890
1891
exponent = 154320986

fp2_-4()

1856
1857
1858
1859
1860
1861
1862
1863
base = 256
1888
1889
1890
1891
exponent = 1

fp2_-3()

1920
1921
1922
1923
1924
1925
1926
1927
base = 16
1952
1953
1954
1955
exponent = 3

fp2_-3()

1920
1921
1922
1923
1924
1925
1926
1927
base = 1.0000004
1952
1953
1954
1955
exponent = 308641972

fp2_-3()

1920
1921
1922
1923
1924
1925
1926
1927
base = 10000
1952
1953
1954
1955
exponent = 2

fp2_-3()

1920
1921
1922
1923
1924
1925
1926
1927
base = 1e+120
1952
1953
1954
1955
exponent = 3

fp2_-2()

1984
1985
1986
1987
1988
1989
1990
1991
base = 1.0000002
2016
2017
2018
2019
exponent = 617283945

fp2_-2()

1984
1985
1986
1987
1988
1989
1990
1991
base = 1e+60
2016
2017
2018
2019
exponent = 6

fp2_-2()

1984
1985
1986
1987
1988
1989
1990
1991
base = 4
2016
2017
2018
2019
exponent = 6

fp2_-2()

1984
1985
1986
1987
1988
1989
1990
1991
base = 100
2016
2017
2018
2019
exponent = 4

fp2_()

2048
2049
2050
2051
2052
2053
2054
2055
base = 1e+30
2080
2081
2082
2083
exponent = 13

fp2_()

2048
2049
2050
2051
2052
2053
2054
2055
base = 10
2080
2081
2082
2083
exponent = 9

fp2_()

2048
2049
2050
2051
2052
2053
2054
2055
base = 2
2080
2081
2082
2083
exponent = 13

fp2_()

2048
2049
2050
2051
2052
2053
2054
2055
base = 1.0000001
2080
2081
2082
2083
exponent = 1234567890

fastexp()

2064
2065
2066
2067
2068
2069
2070
2071
base = 0.5
2072
2073
2074
2075
2076
2077
2078
2079
ord = 1
bitsdone = 0
2080
2081
2082
2083
2084
2085
2086
2087
result = 1
2128
2129
2130
2131
exponent = -13

fastexp()

2064
2065
2066
2067
2068
2069
2070
2071
base = 10
2072
2073
2074
2075
2076
2077
2078
2079
ord = 1
bitsdone = 0
2080
2081
2082
2083
2084
2085
2086
2087
result = 1
2128
2129
2130
2131
exponent = 9

fastexp()

2064
2065
2066
2067
2068
2069
2070
2071
base = 1e-30
2072
2073
2074
2075
2076
2077
2078
2079
ord = 1
bitsdone = 0
2080
2081
2082
2083
2084
2085
2086
2087
result = 1
2128
2129
2130
2131
exponent = -13

fastexp()

2064
2065
2066
2067
2068
2069
2070
2071
base = 1.0000001
2072
2073
2074
2075
2076
2077
2078
2079
ord = 1
bitsdone = 0
2080
2081
2082
2083
2084
2085
2086
2087
result = 1
2128
2129
2130
2131
exponent = 1234567890

fastexp(): for()

2088
2089
2090
2091
2092
2093
2094
2095
e2 = 1.0000001

fastexp(): for()

2088
2089
2090
2091
2092
2093
2094
2095
e2 = 2

fastexp(): for()

2088
2089
2090
2091
2092
2093
2094
2095
e2 = 10

fastexp(): for()

2088
2089
2090
2091
2092
2093
2094
2095
e2 = 1e+30

fp2()

2096
2097
2098
2099
2100
2101
2102
2103
base = 1.0000001
2128
2129
2130
2131
exponent = 1234567890

fp2()

2096
2097
2098
2099
2100
2101
2102
2103
base = 10
2128
2129
2130
2131
exponent = 9

fp2()

2096
2097
2098
2099
2100
2101
2102
2103
base = 0.5
2128
2129
2130
2131
exponent = -13

fp2()

2096
2097
2098
2099
2100
2101
2102
2103
base = 1e-30
2128
2129
2130
2131
exponent = -13

main()

2156
2157
2158
2159
exponent = -3
2160
2161
2162
2163
2164
2165
2166
2167
base = 4.24399158e-312
2168
2169
2170
2171
2172
2173
2174
2175
res1 = -2.49089256e+267
2176
2177
2178
2179
2180
2181
2182
2183
resok = -9.69870043e+266
2184
2185
2186
2187
2188
2189
2190
2191
resa = 4.96028434e-270

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

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
4.13618127022e+53 4.13618129872e+53 (diff=-2.84991688616e+45)
4.13618127022e+53 4.13618129872e+53 (diff=-2.84991697124e+45)
-8.50705917302e+37
Please enter the base and exponent
make the exponent == 0 to quit
2 bits used
1000000000 1000000000 (diff=0)
1000000000 1000000000 (diff=0)
0
Please enter the base and exponent
make the exponent == 0 to quit
3 bits used
8192 8192 (diff=0)
8192 8192 (diff=0)
0
Please enter the base and exponent
make the exponent == 0 to quit
3 bits used
Warning: under or overflow
inf inf (diff=-nan)
inf inf (diff=-nan)
-nan
Please enter the base and exponent
make the exponent == 0 to quit
                                                                                                                                                                                                                                                                       

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