Commit 2c358181 authored by sumet's avatar sumet

ส่งงาน week04

parent fb62a644
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/numer-60-2.iml" filepath="$PROJECT_DIR$/.idea/numer-60-2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="projectConfiguration" value="py.test" />
<option name="PROJECT_TEST_RUNNER" value="py.test" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="e60e080a-b7e5-4ca0-8e57-f54b36d0f897" name="Default" comment="">
<change beforePath="" afterPath="$PROJECT_DIR$/.idea/vcs.xml" />
<change beforePath="" afterPath="$PROJECT_DIR$/week04/assignment.py" />
<change beforePath="$PROJECT_DIR$/Week03/Chapter 03 Numerics and Error Analysis.ipynb" afterPath="" />
<change beforePath="$PROJECT_DIR$/week03/assignment.py" afterPath="$PROJECT_DIR$/week03/assignment.py" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="assignment.py" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/week04/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="108">
<caret line="6" column="0" lean-forward="true" selection-start-line="6" selection-start-column="0" selection-end-line="6" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/week03/assignment.py" />
<option value="$PROJECT_DIR$/week04/assignment.py" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="-1301" />
<option name="y" value="-4" />
<option name="width" value="1301" />
<option name="height" value="772" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="Scratches" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="numer-60-2" type="b2602c69:ProjectViewProjectNode" />
<item name="numer-60-2" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="numer-60-2" type="b2602c69:ProjectViewProjectNode" />
<item name="numer-60-2" type="462c0819:PsiDirectoryNode" />
<item name="week04" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<property name="last_opened_file_path" value="$PROJECT_DIR$/.." />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/week04" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/week04" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="assignment" type="PythonConfigurationType" factoryName="Python" temporary="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/week04" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<module name="numer-60-2" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/week04/assignment.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
</configuration>
<recent_temporary>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="Python.assignment" />
</list>
</recent_temporary>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="e60e080a-b7e5-4ca0-8e57-f54b36d0f897" name="Default" comment="" />
<created>1518057275500</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1518057275500</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="-1301" y="-4" width="1301" height="772" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.24596465" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.32882884" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Python Console" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/week03/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="144">
<caret line="8" column="0" lean-forward="true" selection-start-line="8" selection-start-column="0" selection-end-line="8" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week04/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="108">
<caret line="6" column="0" lean-forward="true" selection-start-line="6" selection-start-column="0" selection-end-line="6" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</component>
</project>
\ No newline at end of file
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# บทที่ 1 ตัวเลขและการวิเคราะห์ความคลาดเคลื่อน\n",
"\n",
"> Mathematically correct programming bug is **hard** to spot.\n",
"\n",
"> สมการถูกแต่...โปรแกรมผิด\n",
"\n",
">> Paulgramming - 2017\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## C++\n",
"\n",
"```c++ \n",
"#include <iostream>\n",
"using namespace std;\n",
"main() {\n",
" float x = 1.0; \n",
" float y = x / 3.0;\n",
" cout << ((x == y * 3.0)?\"\": \"Not\") << \" Equal\" << endl;\n",
"}\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## Java\n",
"\n",
"```java\n",
"public class Test {\n",
" public static void main(String[] a) {\n",
" float x = 1.0;\n",
" float y = x / 3.0;\n",
" System.out.println( ((x == y * 3.0)?\"\": \"Not\") + \" Equal\" );\n",
" }\n",
"}\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# ตัวเลขและความคลาดเคลื่อนในโปรแกรม\n",
"## Python\n",
"\n",
"```python\n",
"x = 1.0\n",
"y = x / 3.0\n",
"print(\"Equal\" if x == y*3.0 else \"Not Equal\")\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# มีหลายตัวเลขที่ไม่\n",
"x = 1.0\n",
"y = x / 3.0\n",
"#print(\"Equal\" if x == y*3.0 else \"Not Equal\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# มีหลายตัวเลขที่คอมเก็บค่าไ่ม่ได้\n",
"\n",
"* ตัวเลขที่คอมพิวเตอร์ต้องประมาณการ (Inexact numbers)\n",
" * ค่าอตรรกยะ (irrational numbers) $\\pi, \\e, ...$\n",
" * ค่าตรรกยะ (rational numbers) ที่ไม่สามารถแปลงเป็นเลขฐานสองได้\n",
"```python\n",
"0.1 + 0.2 == 0.3\n",
"```\n",
"\n",
"* ตัวเลขที่คอมพิวเตอร์สามารถเก็บค่าได้ตรง (Exact numbers)\n",
"> ตัวเลขที่สามารถแปลงให้เป็นเลขฐานสองได้"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Floating-Point Binary\n",
"\n",
"> ตัวเลข(number) เขียนแทนด้วยสายของเลขเดี่ยว(digit) โดยจำนวนของเลขเดี่ยว ขึ้นอยู่กับ เลขฐาน(base)\n",
"\n",
"ในระบบจำนวนนับของคนเราจะเป็นเลขฐาน 10 โดยมี digit ที่ใช้ได้ได้แก่ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n",
"ยกตัวอย่างตัวเลขในเลขฐาน 10 เช่น 128 77 12100 เป็นต้น\n",
"\n",
"ตัวเลขที่ใช้ในคอมพิวเตอร์จะเป็นเลขฐาน 2 โดยมี digit ที่ใช้ได้ ได้แก่ 0 และ 1 \n",
"ยกตัวอย่างตัวเลขในเลขฐาน 2 เช่น 1, 101, 1101 เป็นต้น"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Terminology\n",
"** Bit **\n",
": 0 or 1\n",
"\n",
"** Byte ** \n",
": 8 bits\n",
"\n",
"** Real **\n",
": 4 bytes **single precision**\n",
": 8 bytes **double precision**\n",
"\n",
"** Integer **\n",
": 1, 2, 4, or 8 byte signed\n",
": 1, 2, 4, or 8 byte unsigned\n",
"\n",
"> ขึ้นอยู่กับภาษาโปรแกรมว่าจะใช้สายที่มีความยาวเท่าไหร่ 8, 16, 32 หรือ 64 เป็นต้น\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# ข้อตกลง\n",
"\n",
"* เพื่อง่ายต่อการคำนวณเราจะใช้สายที่มีความยาว 8 และเขียนแยกเป็นกลุ่มละ 4 เพื่อง่ายต่อการคำนวน \n",
"\n",
"> เช่น 1 = 0000 0001\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary $\\rightarrow$ Integer\n",
"\n",
"| | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |\n",
"|$2^7$ |$2^6$ |$2^5$ |$2^4$ |$2^3$ |$2^2$ |$2^1$ |$2^0$ |\n",
"\n",
"$1\\times2^7 + 1\\times2^6 + 1\\times2^5 + 0\\times2^4 + 0\\times2^3 + 1\\times2^2 + 1\\times2^1 + 1\\times2^0$\n",
"\n",
"$1\\times 128+ 1\\times 64 + 1\\times 32 + 0\\times 16 + 0\\times 8 + 1\\times 4 + 1\\times 2 + 1 \\times 1$\n",
"\n",
"\n",
"$128 + 64 + 32 + 0 + 0 + 4 + 2 + 1$\n",
"\n",
"**Answer**\n",
"> $231$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# เขียนโปรแกรมทดสอบ\n",
"\n",
"* คำนวณหาผลลัพธ์\n",
"```python\n",
"1*2**7 + 1*2**6 + 1*2**5 + 0*2**4 + 0*2**3 + 1*2**2 + 1*2**1 + 1*2**0\n",
"```\n",
"\n",
"* คำสั่งแปลงเลขฐานต่างๆ\n",
"```python\n",
"int('11100111', 2) # แปลง str '11100111' ฐาน 2 เป็น int\n",
"oct(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 8\n",
"hex(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 16\n",
"bin(35) # แปลง 35 ฐานสิบ ให้เป็นฐาน 2\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"#int('11100111', 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Exercise\n",
"จงแสดงการแปลงตัวเลขฐานสองต่อไปนี้เป็นจำนวนเต็ม\n",
"* 10011001\n",
"* 00010011\n",
"* 00011111"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Binary $\\rightarrow$ Float\n",
"กำหนดตำแหน่งที่เลขสองกำลังเป็นศูนย์ (fix-point representation)\n",
"\n",
"| | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |\n",
"|$2^5$ |$2^4$ |$2^3$ |$2^2$ |$2^1$ |$2^0$ |$2^-1$ |$2^-2$ |\n",
"\n",
"$1\\times 2^5+1\\times 2^4+1\\times 2^3+0\\times 2^2+9\\times 2^1+1\\times 2^0+1\\times 2^{-1}+1\\times 2^{-2}$\n",
"\n",
"$1\\times2^5 + 1\\times2^4 + 1\\times2^3 +0\\times 2^2+0\\times2^1+1\\times 1+\\frac{1}{2}+\\frac{1}{4}$\n",
"\n",
"$32 + 16 + 8 + 0 + 0 + 1 + 0.5 + 0.25$\n",
"\n",
"**Answer**\n",
"> 57.75"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"#1*2**5 + 1*2**4 + 1*2**3 + 0*2**2 + 0*2**1 + 1*2**0 + 1*2**(-1) + 1*2**(-2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Binary Fixed-Point Arithmetic\n",
"| | | | | | | | | |\n",
"|------|------|------|------|------|------|------|------|------|\n",
"| 1 | 1 | 1 | ... | 0 | 0 | ... | 1 | 1 |\n",
"| sign ||$2^{m-1}$ |$2^{m-2}$ | ... |$2^0$ |$2^{-1}$ | ... |$2^{-n+1}$ |$2^{-n}$ |\n",
"\n",
"* Parameters: $m, n \\in Z$\n",
"* $m$ ความยาวของสาย(จำนวน bit) ที่ใช้เป็นจำนวนเต็ม(integer portion)\n",
"* $n$ ความยาวของสาย(จำนวน bit) ที่ใช้เป็นตัวหาร(fractional portion)\n",
"* ความยาวทั้งหมดของสายเป็น $m+n+1$ รวมกับ bit ที่ใช้บอกเครื่องหมาย (+/-)\n",
"* ความรู้เพิ่มเติม: [Fixed-Point Numbers](https://en.wikibooks.org/wiki/Floating_Point/Fixed-Point_Numbers)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Error in Binary Fixed-Point Arithmetic\n",
"\n",
"** สมมติว่ามีแค่ 2 bit ให้ใช้ ** \n",
"\n",
"> $m = 1, n=1$ โดยที่ไม่คิด sign bit\n",
"\n",
"$0.1 \\times 0.1 = 0.01 \\approx 0.0$ \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary Floating-Point Arithmetic\n",
"1. IEEE single precision format\n",
"\n",
"| 0 | 1-8 | 9-31 |\n",
"|:----:|:-------------:|:-----------------:|\n",
"| $s$ | $e$ | $f$ |\n",
"| $0$ | $00001001$ | $0011011...01$ |\n",
"\n",
"$ = (-1)^s \\times 2^{e-127} \\times 1.f $\n",
"\n",
"โดยที่\n",
" * **sign** $s \\in {0, 1}$\n",
" * **biased exponent** $0 \\le e \\le 255$\n",
" * **exponent** $p=e-127$ so $-126 \\le p \\le 127$\n",
" * **significand** $1.f$ "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Binary Floating-Point Arithmetic \n",
"2. IEEE double precision format\n",
"\n",
"| 0 | 1-11 | 12-63 |\n",
"|:----:|:-------------:|:-----------------:|\n",
"| $s$ | $e$ | $f$ |\n",
"| $0$ | $00001000111$ | $0011011...01$ |\n",
"\n",
"$ = (-1)^s \\times 2^{e-1023} \\times 1.f $\n",
"\n",
"โดยที่\n",
" * **sign** $s \\in {0, 1}$\n",
" * **biased exponent** $0 \\le e \\le 2047$\n",
" * **exponent** $p=e-1023$ so $-1022 \\le p \\le 1024$\n",
" * **significand** $x = 1.f$ "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Example \n",
"\n",
"จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE single precision format\n",
"> 0 1111 0000 0000 1010 0000 0000 0000 000\n",
" \n",
"**Answer** \n",
"หาค่า $s, e, f$ จากโจทย์\n",
"> $s = 0$\n",
"\n",
"> $e$ = 11110000\n",
"\n",
"> $f$ = 0000101 \n",
"\n",
"```python\n",
"s = 0\n",
"e = int('11110000', 2)\n",
"x = 1 + 0*2**(-1) + 0*2**(-2) + 0*2**(-3) + 0*2**(-4) + 1*2**(-5) + 0*2**(-6) + 1*2**(-7)\n",
"print( (-1)**s * 2**(e-127) * x )\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Exercise \n",
"1. จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE single precision format\n",
" * $s = 0$\n",
" * $e$ = 1111 0000 0000\n",
" * $f$ = 0000 1010 \n",
" \n",
"2. จงเขียนคำสั่งเพื่อหาค่าของตัวเลขตามข้อต่อกำหนดต่อไปนี้ โดยใช้ IEEE double precision format\n",
" * $s = 1$\n",
" * $e$ = 0000 0011 0000\n",
" * $f$ = 0110 1010 \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Error Calculation\n",
"\n",
"* **Absolute Error** \n",
" $\\rightarrow E_{abs} = \\| x_0 - x \\|$\n",
"* **Relative Error**\n",
" $\\rightarrow E_{rel} = \\frac { E_{abs} }{x} $\n",
"* **Percentage Error**\n",
" $\\rightarrow E_{per} = E_{rel} \\times 100$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
def value(v,p=4,n=3): def value(v,p=4,n=3):
sign = -1 if v[0]==1 else 1 sign = -1 if v[0]==1 else 1
return sign*sum([v[i]*(2**(p-i)) for i in range(1,p+n+1)]) return sign*sum([v[i]*(2**(p-i)) for i in range(1,p+n+1)])
def values(v,p,n): def values(v,p=4,n=3):
V = list(map(int,v)) V = list(map(int,v))
sign = -1 if V[0]==1 else 1 sign = -1 if V[0]==1 else 1
return sign*sum([V[i]*(2**(p-i)) for i in range(1,p+n+1)]) return sign*sum([V[i]*(2**(p-i)) for i in range(1,p+n+1)])
......
def single_prec(v):
return ((-1) ** int(v[0])) * sum([int(v[i]) * (2 ** (8 - i)) for i in range(1,32)])
def double_prec(v):
return ((-1) ** int(v[0])) * sum([int(v[i]) * (2 ** (11 - i)) for i in range(1, 64)])
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