This question is incomplete, the complete questions are;
Calculate the sum (not the partial sum) of the first 10 million terms in the harmonic series
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + ... + 1/n + ...
using both double-precision and single-precision numbers. Compare the results. Explain why they are different. (You may include your explanation as a comment at the end of the code for this problem.
Answer:
harsum =
16.6953
harmsum =
16.1122
(harsum is 16.6953 and harmsum is 16.1122) so there is a difference in the sum of double and single type because a double type is more precise than the singe type.
Step-by-step explanation:
Given that;
the terms of the harmonics series are;
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + ... + 1/n + ...
we have to find the harmonic series value for 10 milli0n terms
we have to specify the numbers in both single and double type precision
the MATLAB code used in finding the sum is given by the following expression
n=double( 1:1e7 );
har=1./n;
harsum=sum( har )
m=single( 1:1e7 )
harm=1./m;
harmsum=sum( harm )
MATLAB OUTPUT
harsum =
16.6953
harmsum =
16.1122
Therefore there is a difference in the sum of double and single type because a double type is more precise than the singe type.