summaryrefslogtreecommitdiff
path: root/Teach_Yourself_C_in_24_Hours/hour9.ipynb
blob: c52a1ab258b35dd20fde65f6d8b7d37185ec32a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
 "metadata": {
  "name": "",
  "signature": "sha256:9985251f9ed527f5f6157524d1241d5f81fd588714602adf2cf638e46fdde236"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Hour 9: Working with data and Math functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.1, Page No.144"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Value in the textbook for 0xFF is wrong\n",
      "ch = \"0xFF\"\n",
      "x = \"0xFFFF\"\n",
      "y = \"0xFFFF\"\n",
      "print \"The decimal of signed 0xFF is \", int(ch, 16)\n",
      "print \"The decimal of signed 0xFFFF is \", int(x, 16)\n",
      "print \"The decimal of unsigned 0xFFFF is \", int(y, 16)\n",
      "print \"The hex of decimal 12345 is 0x%x\" %(12345)\n",
      "#answer in textbook for -12345 is wrong\n",
      "print \"The hex of decimal -12345 is 0x%x\" %(-12345)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The decimal of signed 0xFF is  255\n",
        "The decimal of signed 0xFFFF is  65535\n",
        "The decimal of unsigned 0xFFFF is  65535\n",
        "The hex of decimal 12345 is 0x3039\n",
        "The hex of decimal -12345 is 0x-3039\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.2, Page No.146"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "print \"The size of short int is:\",sys.getsizeof(65535)\n",
      "print \"The size of long int is:\",sys.getsizeof(65535*65535)\n",
      "print \"The size of float is:\",sys.getsizeof(0.0)\n",
      "# As python has no datatype like double, so that portion of program has not writeen here."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The size of short int is: 12\n",
        "The size of long int is: 18\n",
        "The size of float is: 16\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exmaple 9.3, Page No.147"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import array\n",
      "x=0xFFFF\n",
      "y=0xFFFF\n",
      "s=0xFFFFFFFFl\n",
      "t=0xFFFFFFFFL\n",
      "print \"The short int of 0xFFFF is {:d}\".format(x)\n",
      "print \"The unsigned int of 0xFFFF is {:d}\".format(y)\n",
      "print \"The long int of 0xFFFFFFFFl is {:d}\".format(s)\n",
      "print \"The long int of 0xFFFFFFFFl is {:d}\".format(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The short int of 0xFFFF is 65535\n",
        "The unsigned int of 0xFFFF is 65535\n",
        "The long int of 0xFFFFFFFFl is 4294967295\n",
        "The long int of 0xFFFFFFFFl is 4294967295\n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.4, Page No.150"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "x=45.0\n",
      "x=x*3.141593/180.0\n",
      "print \"The sine of 45 is: \",math.asin(x)\n",
      "print \"The sine of 45 is: \",math.acos(x)\n",
      "print \"The tangent of 45 is: \",math.atan(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The sine of 45 is:  0.903339250676\n",
        "The sine of 45 is:  0.667457076119\n",
        "The tangent of 45 is:  0.665773803591\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9.5, Page No.151"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "x=64.0\n",
      "y=3.0\n",
      "z=0.5\n",
      "print \"pow(64.0,3.0) returns {:7.0f}\".format(math.pow(x,y))\n",
      "print \"sqrt(64.0) returns {:2.0f}\".format(math.sqrt(x))\n",
      "print \"pow(64.0,0.5) returns {:2.0f}\".format(math.pow(x,z))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "pow(64.0,3.0) returns  262144\n",
        "sqrt(64.0) returns  8\n",
        "pow(64.0,0.5) returns  8\n"
       ]
      }
     ],
     "prompt_number": 36
    }
   ],
   "metadata": {}
  }
 ]
}