Commit 11818e66 authored by Navayos Pratumthong's avatar Navayos Pratumthong

ส่งการบ้าน assignment

parent d7af15bc
...@@ -235,6 +235,7 @@ ...@@ -235,6 +235,7 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 15,
"metadata": { "metadata": {
"collapsed": true,
"slideshow": { "slideshow": {
"slide_type": "skip" "slide_type": "skip"
} }
...@@ -289,6 +290,7 @@ ...@@ -289,6 +290,7 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 26,
"metadata": { "metadata": {
"collapsed": true,
"slideshow": { "slideshow": {
"slide_type": "skip" "slide_type": "skip"
} }
...@@ -319,6 +321,76 @@ ...@@ -319,6 +321,76 @@
"* ความรู้เพิ่มเติม: [Fixed-Point Numbers](https://en.wikibooks.org/wiki/Floating_Point/Fixed-Point_Numbers)" "* ความรู้เพิ่มเติม: [Fixed-Point Numbers](https://en.wikibooks.org/wiki/Floating_Point/Fixed-Point_Numbers)"
] ]
}, },
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.5\n"
]
},
{
"data": {
"text/plain": [
"1.5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v= [0, 0,0,0,1, 1,0,0]\n",
"sign = -1 if v[0] == 1 else 1\n",
"value = v[1]*2**3\n",
"value += v[2]*2**2\n",
"value += v[3]*2**1\n",
"value += v[4]*2**0\n",
"\n",
"value += v[5]*2**(-1)\n",
"value += v[6]*2**(-2)\n",
"value += v[7]*2**(-3)\n",
"\n",
"print(sign*value)\n",
"[ 2** i for i in range(1,8)]\n",
"[ v[i]*2**(4-i) for i in range(1,8) ]\n",
"sum([ v[i]*2**(4-i) for i in range(1,8)])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def value(v):\n",
" sign = -1 if v[0] == 1 else 1\n",
" return sign*sum([ v[i]*2**(4-i) for i in range(1,8) ])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-2.375\n"
]
}
],
"source": [
"v = [1, 0,0,1,0, 0,1,1]\n",
"print( value(v) )"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
...@@ -472,7 +544,7 @@ ...@@ -472,7 +544,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.2" "version": "3.6.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
#1 homework
def value(v, pos=4, neg=3):
sign = -1 if v[0] == 1 else 1
return sign*sum([v[i]*2**(pos-i) for i in range(1, pos+neg+1)])
#2 assignment
def values(v, pos, neg):
sign = -1 if int(v[0]) == 1 else 1
return sign * sum(int(v[i]) * 2 ** (pos - i) for i in range(1, pos + neg))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment