# CHAPTER 3 : Approximations and Round off Errors¶

## Ex : 3.1 Pg : 56¶

In [2]:
from __future__ import division
lbm=9999# #cm, measured length of bridge
lrm=9##cm, measured length of rivet
lbt=10000##cm, true length of bridge
lrt=10##cm,true length of rivet
#calculating true error below#
Etb=lbt-lbm##cm, true error in bridge
Etr=lrt-lrm##cm, true error in rivet
#calculating percent relative error below
etb=Etb*100/lbt##percent relative error for bridge
etr=Etb*100/lrt##percent relative error for rivet
print "a. The true error is"
print "for the bridge : ",Etb,"cm"
print "for the rivet : ",Etr,"cm"
print "b. The percent relative error is"
print "for the bridge : ",etb,"cm"
print "for the rivet : ",etr,"cm"

a. The true error is
for the bridge :  1 cm
for the rivet :  1 cm
b. The percent relative error is
for the bridge :  0.01 cm
for the rivet :  10.0 cm


## Ex 3.2 : Pg : 58¶

In [18]:
from __future__ import division
from math import factorial
n=3##number of significant figures
es=0.5*(10**(2-n))##percent, specified error criterion
x=0.5#
f=[]
f.append(1)##first estimate f=e**x = 1
ft=1.648721##true value of e**0.5=f
et=[]
et.append((ft-f[0])*100/ft)
ea=[]
ea.append(100)
i=1
while ea[i-1]>=es:
f.append(f[(i-1)]+(x**(i-1))/(factorial(i-1)))
et.append((ft-f[(i)])*100/ft)
ea.append((f[(i)]-f[(i-1)])*100/f[(i)])
i=i+1#

print "Terms\t\t\tResult\t\t\t\tet(%)\t\t\t\tea(%)"
print '-'*100
for j in range(0,i-1):
print j+1,'\t\t\t%0.5f'%f[j],'\t\t\t',et[j],'\t\t\t',ea[j]
print '-'*100

Terms			Result				et(%)				ea(%)
----------------------------------------------------------------------------------------------------
1 			1.00000 			39.3469240702 			100
----------------------------------------------------------------------------------------------------
2 			2.00000 			-21.3061518595 			50.0
----------------------------------------------------------------------------------------------------
3 			2.50000 			-51.6326898244 			20.0
----------------------------------------------------------------------------------------------------
4 			2.62500 			-59.2143243156 			4.7619047619
----------------------------------------------------------------------------------------------------
5 			2.64583 			-60.4779300642 			0.787401574803
----------------------------------------------------------------------------------------------------
6 			2.64844 			-60.6358807827 			0.0983284169125
----------------------------------------------------------------------------------------------------


## Ex 3.3 Pg: 60¶

In [1]:
n=16##no of bits
num=0#
for i in range(0,(n-1)):
num=num+(1*(2**i))#

print "Thus a 16-bit computer word can store decimal integers ranging from",(-1*num),"to",num

Thus a 16-bit computer word can store decimal integers ranging from -32767 to 32767


## Ex 3.4: Pg :63¶

In [2]:
n=7##no. of bits
#the maximum value of exponents is given by
max=1*(2**1)+1*(2**0)#
#mantissa is found by
mantissa=1*(2**-1)+0*(2**-3)+0*(2**-3)#
num=mantissa*(2**(max*-1))##smallest possible positive number for this system
print "The smallest possible positive number for this system is : ",num

The smallest possible positive number for this system is :  0.0625


## Ex 3.5: Pg :65¶

In [3]:
b=2##base
t=3##number of mantissa bits
E=2**(1-t)##epsilon
print "value of epsilon=",E

value of epsilon= 0.25


## Ex 3.6: Pg :68¶

In [4]:
num=input("Input a number: ")
Sum=0#
for i in range(0,100000):
Sum=Sum+num#

print "The number summed up 100,000 times is=",Sum

Input a number: 15
The number summed up 100,000 times is= 1500000


## Ex 3.7: Pg :71¶

In [5]:
a=1#
b=3000.001#
c=3#
#the roots of the quadratic equation x**2+3000.001*x+3=0 are found as
D=(b**2)-4*a*c#
x1=(-b+(D**0.5))/(2*a)#
x2=(-b-(D**0.5))/(2*a)#
print "The roots of the quadratic equation (x**2)+(3000.001*x)+3=0 are = ",x1,'&',x2

The roots of the quadratic equation (x**2)+(3000.001*x)+3=0 are =  -0.000999999999976 & -3000.0


## Ex 3.8: Pg :73¶

In [6]:
from math import exp
def f(x):
y=exp(x)
return y
Sum=1#
test=0#
i=0#
term=1#
x=input("Input value of x:")
while Sum!=test:
print "sum:",Sum,"term:",term,"i:",i
print "-------------------------------------"
i=i+1#
term=term*x/i#
test=Sum#
Sum=Sum+term#

print "Exact Value:",f(x)

Input value of x:1.25
sum: 1 term: 1 i: 0
-------------------------------------
sum: 2.25 term: 1.25 i: 1
-------------------------------------
sum: 3.03125 term: 0.78125 i: 2
-------------------------------------
sum: 3.35677083333 term: 0.325520833333 i: 3
-------------------------------------
sum: 3.45849609375 term: 0.101725260417 i: 4
-------------------------------------
sum: 3.48392740885 term: 0.0254313151042 i: 5
-------------------------------------
sum: 3.4892255995 term: 0.0052981906467 i: 6
-------------------------------------
sum: 3.49017170497 term: 0.000946105472625 i: 7
-------------------------------------
sum: 3.49031953395 term: 0.000147828980098 i: 8
-------------------------------------
sum: 3.49034006576 term: 2.05318027913e-05 i: 9
-------------------------------------
sum: 3.49034263223 term: 2.56647534892e-06 i: 10
-------------------------------------
sum: 3.49034292388 term: 2.91644926013e-07 i: 11
-------------------------------------
sum: 3.49034295426 term: 3.03796797931e-08 i: 12
-------------------------------------
sum: 3.49034295718 term: 2.92112305703e-09 i: 13
-------------------------------------
sum: 3.49034295744 term: 2.60814558663e-10 i: 14
-------------------------------------
sum: 3.49034295746 term: 2.17345465553e-11 i: 15
-------------------------------------
sum: 3.49034295746 term: 1.69801144963e-12 i: 16
-------------------------------------
sum: 3.49034295746 term: 1.24853783061e-13 i: 17
-------------------------------------
sum: 3.49034295746 term: 8.67040160146e-15 i: 18
-------------------------------------
sum: 3.49034295746 term: 5.7042115799e-16 i: 19
-------------------------------------
Exact Value: 3.49034295746