In: Computer Science
With 3-digit rounding after each operation find the value of:
a. 0.4 + 0.4 + … + 0.4 + 100 (where the 0.4 is repeated 100 times)
b. 100+0.4 + 0.4 + … + 0.4 (where the 0.4 is repeated 100 times)
c. Find the absolute and relative error in parts a and b.
In both cases we are getting absolute error as zero.
After adding 0.4 100 times we are getting 40
sum=sum+100 140
absolute error=after rounding value-approximation value
140-140
=0
relative error=(absolute error)/after rounding value i.e,0
see below c code for better understanding.
#include<stdio.h>
#include<math.h>
main()
{
int i,aerror,rerror,rvalue;
float pre=0.4,val=100.0;
float sum=0.0;
for(i=1;i<=100;i++){
sum=sum+pre;
}
sum=sum+100;
printf("%f\n",sum);
rvalue=round(sum);
printf("%f..%f\n",sum,(float)rvalue);
aerror=rvalue-sum;
rerror=aerror/rvalue;
printf("a
problem:%f..%.f",(float)aerror,(float)rerror);
}