summaryrefslogtreecommitdiff
path: root/Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb
diff options
context:
space:
mode:
authorTrupti Kini2017-01-12 23:33:05 +0600
committerTrupti Kini2017-01-12 23:33:05 +0600
commite2ec570a69371634ec7a6b27f9891e732c3428da (patch)
tree9a1604378caf041459fa446926a3ba5cd0f31eed /Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb
parent882a643ff3426d410a375df9c18ae2f90e045728 (diff)
downloadPython-Textbook-Companions-e2ec570a69371634ec7a6b27f9891e732c3428da.tar.gz
Python-Textbook-Companions-e2ec570a69371634ec7a6b27f9891e732c3428da.tar.bz2
Python-Textbook-Companions-e2ec570a69371634ec7a6b27f9891e732c3428da.zip
Added(A)/Deleted(D) following books
A Basic_mechanical_engineering_by_Basant_Agrawal_,_C.M_Agrawal/README.txt A Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter11.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter12.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter13.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter14.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter15.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter7.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter8.ipynb A Numerical_Methods_by_E._Balaguruswamy/chapter9.ipynb A Numerical_Methods_by_E._Balaguruswamy/screenshots/greatest-precision-4.png A Numerical_Methods_by_E._Balaguruswamy/screenshots/rounding-off-4.png A Numerical_Methods_by_E._Balaguruswamy/screenshots/truncation-error-4.png A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_4JhcI7F.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_5CfOfQx.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_B30VPml.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_BE8QjoS.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Gb015bo.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_KQ8ycMr.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_LIZWeY4.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Opo6g3L.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Pq40WOu.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_QLXi0uM.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_TdOIeIQ.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_UiM06tF.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_VQabJzR.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_XKPPUxj.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_dfFlLnm.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_fegIkl6.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_g1GxlUN.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_gGvuusH.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_gZeNd4b.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_khAt4Y6.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_oJQM5Mb.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_p22tFeA.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_qNHcrb8.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_qeUxd0G.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_r0nfWNs.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_uWQUwaW.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_vkMAnbH.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_wP2wGMS.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_x09tDSO.ipynb A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_yCW2orc.ipynb
Diffstat (limited to 'Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb')
-rw-r--r--Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb1170
1 files changed, 1170 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb b/Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb
new file mode 100644
index 00000000..d4901132
--- /dev/null
+++ b/Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb
@@ -0,0 +1,1170 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Chapter 3 - Computer codes and arithmetic"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_01 Pg No. 45"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Decimal value = 13.8125\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Binary to decimal\n",
+ "\n",
+ "b = '1101.1101'\n",
+ "def parse_bin(s):\n",
+ " t = s.split('.')\n",
+ " return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])\n",
+ "\n",
+ "d = parse_bin(b) # #Integral and fractional parts\n",
+ "print 'Decimal value = ',d"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_02 Pg No. 46"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Decimal value = 4783\n"
+ ]
+ }
+ ],
+ "source": [
+ "#hexadecimal to decimal\n",
+ "\n",
+ "h = '12AF' \n",
+ "d=int(h,16)\n",
+ "print 'Decimal value = ',d"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_03 Pg No. 47"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Binary equivalent = 101011.011\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Decimal to Binary\n",
+ "\n",
+ "d = 43.375 #\n",
+ "\n",
+ "def parse_float(num):\n",
+ " exponent=0\n",
+ " shifted_num=num\n",
+ " while shifted_num != int(shifted_num): \n",
+ " shifted_num*=2\n",
+ " exponent+=1\n",
+ " if exponent==0:\n",
+ " return '{0:0b}'.format(int(shifted_num))\n",
+ " binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)\n",
+ " integer_part=binary[:-exponent]\n",
+ " fractional_part=binary[-exponent:].rstrip('0')\n",
+ " return '{0}.{1}'.format(integer_part,fractional_part)\n",
+ "\n",
+ "b = parse_float(d) \n",
+ "print 'Binary equivalent = ',b"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_04 Pg No. 48"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Octal number = 243\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Decimal to Octal\n",
+ "\n",
+ "d = 163 #\n",
+ "def base10toN(num, base):\n",
+ " \"\"\"Change ``num'' to given base\n",
+ " Upto base 36 is supported.\"\"\"\n",
+ "\n",
+ " converted_string, modstring = \"\", \"\"\n",
+ " currentnum = num\n",
+ " if not 1 < base < 37:\n",
+ " raise ValueError(\"base must be between 2 and 36\")\n",
+ " if not num:\n",
+ " return '0'\n",
+ " while currentnum:\n",
+ " mod = currentnum % base\n",
+ " currentnum = currentnum // base\n",
+ " converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string\n",
+ " return converted_string\n",
+ "\n",
+ "\n",
+ "\n",
+ "Oct = base10toN(d,8)\n",
+ "print 'Octal number = ',Oct"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_05 Pg No. 48"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Binary equivalent = 0.10100110011001100110011001100110011001100110011001101\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Decimal to binary\n",
+ "\n",
+ "d = 0.65\n",
+ "\n",
+ "def parse_float(num):\n",
+ " exponent=0\n",
+ " shifted_num=num\n",
+ " while shifted_num != int(shifted_num): \n",
+ " shifted_num*=2\n",
+ " exponent+=1\n",
+ " if exponent==0:\n",
+ " return '{0:0b}'.format(int(shifted_num))\n",
+ " binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)\n",
+ " integer_part=binary[:-exponent]\n",
+ " fractional_part=binary[-exponent:].rstrip('0')\n",
+ " return '{0}.{1}'.format(integer_part,fractional_part)\n",
+ "\n",
+ "\n",
+ "b=parse_float(d) # binary equibvalent\n",
+ "print 'Binary equivalent = ',b"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_06 Pg No. 49 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Hexadecimal equivalent of octal number 243 is : a3\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Octal to Hexadecimal \n",
+ "\n",
+ "Oct = '243' \n",
+ "dec=int(Oct,8)\n",
+ "h = hex(dec)[2:]\n",
+ "\n",
+ "print 'Hexadecimal equivalent of octal number 243 is :',h"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_07 Pg No. 49"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Octal number equivalent of Hexadecimal number 39.B8 is: 57.71875\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Hexadecimal to Octal \n",
+ "\n",
+ "\n",
+ "h = '39.B8' #\n",
+ " \n",
+ " \n",
+ "def float_to_binary(num):\n",
+ " exponent=0\n",
+ " shifted_num=num\n",
+ " while shifted_num != int(shifted_num): \n",
+ " shifted_num*=2\n",
+ " exponent+=1\n",
+ " if exponent==0:\n",
+ " return '{0:0b}'.format(int(shifted_num))\n",
+ " binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)\n",
+ " integer_part=binary[:-exponent]\n",
+ " fractional_part=binary[-exponent:].rstrip('0')\n",
+ " return '{0}.{1}'.format(integer_part,fractional_part)\n",
+ "\n",
+ "def floathex_to_binary(floathex):\n",
+ " num = float.fromhex(floathex)\n",
+ " return float_to_binary(num)\n",
+ "\n",
+ "b= floathex_to_binary(h)\n",
+ "def parse_bin(s):\n",
+ " t = s.split('.')\n",
+ " return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])\n",
+ "\n",
+ "d = parse_bin(b) # #Integral and fractional parts\n",
+ "print 'Octal number equivalent of Hexadecimal number 39.B8 is:',d\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_08 Pg No. 50"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Binary equivalent of -13 is : 10011\n"
+ ]
+ }
+ ],
+ "source": [
+ "## -ve Integer to binary\n",
+ "\n",
+ "negint = -13\n",
+ "def to_twoscomplement(bits, value):\n",
+ " if value < 0:\n",
+ " value = ( 1<<bits ) + value\n",
+ " formatstring = '{:0%ib}' % bits\n",
+ " return formatstring.format(value)\n",
+ "\n",
+ "compl_2 = to_twoscomplement(5, negint)\n",
+ "\n",
+ "print 'Binary equivalent of -13 is :',compl_2"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_09 Pg No. 51"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Binary equivalent of -32768 in a 16 bit word is: 1000000000000000\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Binary representation\n",
+ "\n",
+ "n = -32768\n",
+ "\n",
+ "def to_twoscomplement(bits, value):\n",
+ " if value < 0:\n",
+ " value = ( 1<<bits ) + value\n",
+ " formatstring = '{:0%ib}' % bits\n",
+ " return formatstring.format(value)\n",
+ "\n",
+ "binfinal = to_twoscomplement(16, n)\n",
+ "\n",
+ "\n",
+ "print 'Binary equivalent of -32768 in a 16 bit word is:',binfinal "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_10 Pg No. 52"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ " 0.00596 is expressed as 0.596000*10**-2 \n",
+ "\n",
+ "\n",
+ " 65.7452 is expressed as 0.657452*10**2 \n",
+ "\n",
+ "\n",
+ " -486.8 is expressed as -0.486800*10**3 \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Notation\n",
+ "\n",
+ "def float_notation(n):\n",
+ " m = n #\n",
+ " for i in range(1,17):\n",
+ " if abs(m) >= 1:\n",
+ " m = n/10**i\n",
+ " e = i\n",
+ " elif abs(m) < 0.1:\n",
+ " m = n*10**i\n",
+ " e = -i\n",
+ " else:\n",
+ " if i == 1:\n",
+ " e = 0\n",
+ " \n",
+ " break \n",
+ " return [m,e]\n",
+ " \n",
+ "\n",
+ "[m,e] = float_notation(0.00596)\n",
+ "print '\\n 0.00596 is expressed as %f*10**%d \\n'%(m,e)\n",
+ "[m,e] = float_notation(65.7452)\n",
+ "print '\\n 65.7452 is expressed as %f*10**%d \\n'%(m,e)\n",
+ "[m,e] = float_notation(-486.8)\n",
+ "print '\\n -486.8 is expressed as %f*10**%d \\n'%(m,e)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_11 Pg No. 53"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "25+12 = 37\n",
+ "25-12 = 13\n",
+ "12-25 = -13\n",
+ "25*12 = 300\n",
+ "25/12 = 2\n",
+ "12/25 = 0\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Interger Arithmetic\n",
+ "\n",
+ "print '25+12 =',int(25 + 12)\n",
+ "print '25-12 =',int(25 - 12) \n",
+ "print '12-25 =',int(12 - 25)\n",
+ "print '25*12 =',int(25*12)\n",
+ "print '25/12 =',int(25/12)\n",
+ "print '12/25 =',int(12/25)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_12 Pg No. 53"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(a+b)/c = 4 \n",
+ " a/c + b/c = 3\n",
+ "The results are not identical.This is because the remainder of an integer division is always truncated\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Integer Arithmetic\n",
+ "\n",
+ "a = 5 #\n",
+ "b = 7 #\n",
+ "c = 3 #\n",
+ "Lhs = int((a + b)/c)\n",
+ "Rhs = int(a/c) + int(b/c)\n",
+ "print '(a+b)/c = ',Lhs,'\\n a/c + b/c = ',Rhs\n",
+ "if Lhs != Rhs:\n",
+ " print 'The results are not identical.This is because the remainder of an integer division is always truncated'\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_13 Pg No. 54"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Ez= 5 \n",
+ " fy= 0.000964572 \n",
+ " fz= 0.587315572\n",
+ "\n",
+ " z = 0.587316 E5 \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.586351 #\n",
+ "Ex = 5 #\n",
+ "fy = 0.964572 #\n",
+ "Ey = 2 #\n",
+ "v=[Ex,Ey]\n",
+ "Ez= max(v)\n",
+ "n=v.index(Ez)+1\n",
+ "if n == 1:\n",
+ " fy = fy*10**(Ey-Ex)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1) \n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez=',Ez,'\\n fy=',fy,'\\n fz=',fz\n",
+ "else:\n",
+ " fx = fx*10**(Ex - Ey)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1)\n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez=',Ez,'\\n fy=',fy,'\\n fz=',fz\n",
+ "\n",
+ "print '\\n z = %f E%d \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_14 Pg No. 54"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Ez= 5 \n",
+ " fy= 0.635742 \n",
+ " fz= 0.1371558\n",
+ "\n",
+ " z = 0.137156 E5 \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.735816 #\n",
+ "Ex = 4 #\n",
+ "fy = 0.635742 #\n",
+ "Ey = 4 #\n",
+ "v=[Ex,Ey]\n",
+ "Ez= max(v)\n",
+ "n=v.index(Ez)+1\n",
+ "if n == 1:\n",
+ " fy = fy*10**(Ey-Ex)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1)\n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez=',Ez,'\\n fy=',fy,'\\n fz=',fz\n",
+ "else:\n",
+ " fx = fx*10**(Ex - Ey)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1) \n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez=',Ez,'\\n fy=',fy,'\\n fz=',fz\n",
+ "\n",
+ "print '\\n z = %f E%d \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_15 Pg No. 54"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Ez = -3 fz = 0.005082\n",
+ "\n",
+ " z = 0.005082 E-3 \n",
+ "\n",
+ "\n",
+ " z = 0.005082 E-3 (normalised) \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.999658 #\n",
+ "Ex = -3 #\n",
+ "fy = 0.994576 #\n",
+ "Ey = -3 #\n",
+ "Ez = max(Ex,Ey)\n",
+ "fy = fy*10**(Ey-Ex)\n",
+ "fz = fx - fy\n",
+ "print 'Ez =',Ez,'fz =',fz\n",
+ "print '\\n z = %f E%d \\n'%(fz,Ez)\n",
+ "if fz < 0.1 :\n",
+ " fz = fz*10**6 #Since we are using 6 significant digits\n",
+ " n = len(str(fz))\n",
+ " fz = fz/10**n\n",
+ " Ez = Ez + n - 6\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_16 Pg No. 55"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ " fz = 0.080000 \n",
+ " Ez = 2 \n",
+ " z = 0.080000 E2 \n",
+ "\n",
+ "\n",
+ " z = 0.800000 E1 (normalised) \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.200000 #\n",
+ "Ex = 4 #\n",
+ "fy = 0.400000 #\n",
+ "Ey = -2 #\n",
+ "fz = fx*fy\n",
+ "Ez = Ex + Ey \n",
+ "print '\\n fz = %f \\n Ez = %d \\n z = %f E%d \\n'%(fz,Ez,fz,Ez)\n",
+ "if fz < 0.1:\n",
+ " fz = fz*10\n",
+ " Ez = Ez - 1\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_17 Pg No. 55"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ " fz = 4.382715 \n",
+ " Ez = -2 \n",
+ " z = 4.382715 E-2 \n",
+ "\n",
+ "\n",
+ " z = 0.438271 E-1 (normalised) \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.876543 #\n",
+ "Ex = -5 #\n",
+ "fy = 0.200000 #\n",
+ "Ey = -3 #\n",
+ "fz = fx/fy\n",
+ "Ez = Ex - Ey \n",
+ "print '\\n fz = %f \\n Ez = %d \\n z = %f E%d \\n'%(fz,Ez,fz,Ez)\n",
+ "\n",
+ "if fz > 1:\n",
+ " fz = fz/10\n",
+ " Ez = Ez + 1\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_18 Pg No. 56"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Ez = 2 \n",
+ " fx = 50000000.0\n",
+ "\n",
+ " fz = 5000000.010000 \n",
+ " z = 5000000.010000 E2 \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.500000 #\n",
+ "Ex = 1 #\n",
+ "fy = 0.100000 #\n",
+ "Ey = -7 #\n",
+ "Ez= max([Ex,Ey])\n",
+ "n=[Ex,Ey].index(Ez)\n",
+ "if n == 1:\n",
+ " fy = fy*10**(Ey-Ex)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1) \n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez =',Ez,'\\n fy =',fy\n",
+ "else:\n",
+ " fx = fx*10**(Ex - Ey)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1)\n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " print 'Ez =',Ez,'\\n fx =',fx\n",
+ "\n",
+ "print '\\n fz = %f \\n z = %f E%d \\n'%(fz,fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_19 Pg No. 56"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ " fz = 0.175000 \n",
+ " Ez = 110 \n",
+ " z = 0.175000 E110 \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.350000 #\n",
+ "Ex = 40 #\n",
+ "fy = 0.500000 #\n",
+ "Ey = 70 #\n",
+ "fz = fx*fy\n",
+ "Ez = Ex + Ey \n",
+ "print '\\n fz = %f \\n Ez = %d \\n z = %f E%d \\n'%(fz,Ez,fz,Ez)\n",
+ "if fz < 0.1:\n",
+ " fz = fz*10\n",
+ " Ez = Ez - 1\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_20 Pg No. 56"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ " fz = 4.375000 \n",
+ " Ez = -113 \n",
+ " z = 4.375000 E-113 \n",
+ "\n",
+ "\n",
+ " z = 0.437500 E-112 (normalised) \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.875000 #\n",
+ "Ex = -18 #\n",
+ "fy = 0.200000 #\n",
+ "Ey = 95 #\n",
+ "fz = fx/fy\n",
+ "Ez = Ex - Ey \n",
+ "print '\\n fz = %f \\n Ez = %d \\n z = %f E%d \\n'%(fz,Ez,fz,Ez)\n",
+ "\n",
+ "if fz > 1:\n",
+ " fz = fz/10\n",
+ " Ez = Ez + 1\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_21 Pg No. 57"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Ez = 0 \n",
+ " fz = 2e-06\n",
+ "\n",
+ " z = 0.000002 E0 \n",
+ "\n",
+ "\n",
+ " z = 0.002000 E-3 (normalised) \n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Floating Point Arithmetic\n",
+ "\n",
+ "fx = 0.500000 #\n",
+ "Ex = 0 #\n",
+ "fy = 0.499998 #\n",
+ "Ey = 0 #\n",
+ "Ez = 0 #\n",
+ "fz = fx - fy\n",
+ "print 'Ez =',Ez,'\\n fz =',fz\n",
+ "print '\\n z = %f E%d \\n'%(fz,Ez)\n",
+ "if fz < 0.1 :\n",
+ " fz = fz*10**6\n",
+ " n = len(str(fz))\n",
+ " fz = fz/10**n\n",
+ " Ez = Ez + n - 6\n",
+ " print '\\n z = %f E%d (normalised) \\n'%(fz,Ez)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_22 Pg No. 57"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " fxy = 2.480183\n",
+ " Exy = 1 \n",
+ " fxy_z = 24.553832\n",
+ " Exy_z = 1 \n",
+ " fyz = -1.000000 \n",
+ " Eyz = -2 \n",
+ " fx_yz = -1.000000 \n",
+ " Ex_yz = 0 \n",
+ "\n",
+ " (x+y) + z != x + (y+z)\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Laws of Arithmetic\n",
+ "\n",
+ "def add_sub(fx,Ex,fy,Ey): #addition and subtraction fuction\n",
+ " if fx*fy >= 0:\n",
+ " #Addition\n",
+ " Ez = max([Ex,Ey])\n",
+ " n=[Ex,Ey].index(Ez)\n",
+ " if n == 1:\n",
+ " fy = fy*10**(Ey-Ex)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1)\n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " else:\n",
+ " fx = fx*10**(Ex - Ey)\n",
+ " fz = fx + fy\n",
+ " if fz > 1:\n",
+ " fz = fz*10**(-1) \n",
+ " Ez = Ez + 1\n",
+ " \n",
+ " \n",
+ " \n",
+ " else:\n",
+ " #Subtraction\n",
+ " Ez = max([Ex,Ey])\n",
+ " n=[Ex,Ey].index(Ez)\n",
+ " if n == 1:\n",
+ " fy = fy*10**(Ey-Ex)\n",
+ " fz = fx + fy\n",
+ " if abs(fz) < 0.1:\n",
+ " fz = fz*10**6\n",
+ " fz = floor(fz)\n",
+ " nfz = len(str(abs(fz)))\n",
+ " fz = fz/10**nfz\n",
+ " Ez = nfz - 6 \n",
+ " \n",
+ " else:\n",
+ " fx = fx*10**(Ex - Ey)\n",
+ " fz = fx + fy\n",
+ " if fz < 0.1:\n",
+ " fz = fz*10**6\n",
+ " fz = int(fz)\n",
+ " nfz = len(str(abs(fz)))\n",
+ " fz = fz/10**nfz\n",
+ " Ez = nfz - 6\n",
+ " \n",
+ " \n",
+ " \n",
+ " return [fz,Ez]\n",
+ "\n",
+ "fx = 0.456732\n",
+ "Ex = -2\n",
+ "fy = 0.243451\n",
+ "Ey = 0\n",
+ "fz = -0.24800\n",
+ "Ez = 0\n",
+ "\n",
+ "[fxy,Exy] = add_sub(fx,Ex,fy,Ey)\n",
+ "[fxy_z,Exy_z] = add_sub(fxy,Exy,fz,Ez)\n",
+ "[fyz,Eyz] = add_sub(fy,Ey,fz,Ez)\n",
+ "[fx_yz,Ex_yz] = add_sub(fx,Ex,fyz,Eyz)\n",
+ "print ' fxy = %f\\n Exy = %d \\n fxy_z = %f\\n Exy_z = %d \\n fyz = %f \\n Eyz = %d \\n fx_yz = %f \\n Ex_yz = %d \\n'%(fxy,Exy,fxy_z,Exy_z,fyz,Eyz,fx_yz,Ex_yz)\n",
+ "\n",
+ "if fxy_z != fx_yz | Exy_z != Ex_yz:\n",
+ " print ' (x+y) + z != x + (y+z)' "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_23 Pg No. 58"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "In book they have considered the maximum exponent can be only 99, since 110 is greater than 99 the result is erroneous\n",
+ "but in scilab the this value is much larger than 110 so we get a correct result \n",
+ "xy_z = 6e+78\n",
+ "x_yz = 6e+78\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Associative law\n",
+ "x = 0.400000*10**40\n",
+ "y = 0.500000*10**70\n",
+ "z = 0.300000*10**(-30)\n",
+ "print 'In book they have considered the maximum exponent can be only 99, since 110 is greater than 99 the result is erroneous'\n",
+ "print 'but in scilab the this value is much larger than 110 so we get a correct result '\n",
+ "print 'xy_z = ',(x*y)*z\n",
+ "print 'x_yz = ',x*(y*z)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example No. 3_24 Pg No. 58"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "x_yz = 4e-06\n",
+ "xy_xz = 0.0\n"
+ ]
+ }
+ ],
+ "source": [
+ "from math import floor\n",
+ "#Distributive law\n",
+ "x = 0.400000*10**1 #\n",
+ "fx = 0.400000\n",
+ "Ex = 1\n",
+ "y = 0.200001*10**0 #\n",
+ "z = 0.200000*10**0 #\n",
+ "x_yz = x*(y-z)\n",
+ "x_yz = x_yz*10**6\n",
+ "x_yz = floor(x_yz) #considering only six significant digits\n",
+ "n = len(str(x_yz))\n",
+ "fx_yz = x_yz/10**n\n",
+ "Ex_yz = n - 6\n",
+ "x_yz = fx_yz *10**Ex_yz\n",
+ "print 'x_yz = ',x_yz\n",
+ "\n",
+ "fxy = fx*y\n",
+ "fxy = fxy*10**6\n",
+ "fxy = floor(fxy) #considering only six significant digits\n",
+ "n = len(str(fxy))\n",
+ "fxy = fxy/10**n\n",
+ "Exy = n - 6\n",
+ "xy = fxy * 10**Exy\n",
+ "\n",
+ "fxz = fx*z\n",
+ "fxz = fxz*10**6\n",
+ "fxz = floor(fxz) #considering only six significant digits\n",
+ "n = len(str(fxz))\n",
+ "fxz = fxz/10**n\n",
+ "Exz = n - 6\n",
+ "xz = fxz * 10**Exz\n",
+ "\n",
+ "xy_xz = xy - xz\n",
+ "print 'xy_xz = ',xy_xz"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}