Commit 68404083 authored by Navayos Pratumthong's avatar Navayos Pratumthong

week04 assignment

parent e804cd29
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.2 (/usr/bin/python3.5)" 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="jdk" jdkName="Python 3.5.2 (/usr/bin/python3.5)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="projectConfiguration" value="Nosetests" />
<option name="PROJECT_TEST_RUNNER" value="Nosetests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="bc3859da-9355-4074-b99b-1617ce4d97d6" name="Default" comment="" />
<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 SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="hello.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/week01/hello.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="hellomodule.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/week02/hellomodule.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Chapter 03 Numerics and Error Analysis.ipynb.ipynb" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.ipynb">
<provider selected="true" editor-type-id="ipnb-editor">
<state>
<selected id="0" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="assignment.py" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/week03/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="126">
<caret line="7" column="0" lean-forward="true" selection-start-line="7" selection-start-column="0" selection-end-line="7" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="PREVIOUS_COMMIT_AUTHORS">
<list>
<option value="navayos" />
</list>
</option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/week01/hello.py" />
<option value="$PROJECT_DIR$/week02/hellomodule.py" />
<option value="$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.ipynb" />
<option value="$PROJECT_DIR$/week03/assigment.py" />
<option value="$PROJECT_DIR$/week03/assignment.py" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="75" />
<option name="y" value="16" />
<option name="width" value="1281" />
<option name="height" value="732" />
</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="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="week01" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="numer-60-2" type="b2602c69:ProjectViewProjectNode" />
<item name="numer-60-2" type="462c0819:PsiDirectoryNode" />
<item name="week02" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="numer-60-2" type="b2602c69:ProjectViewProjectNode" />
<item name="numer-60-2" type="462c0819:PsiDirectoryNode" />
<item name="week03" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/.." />
</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" selected="Python.assigment">
<configuration name="assigment" 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$/week03" />
<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$/week03/assignment.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
</configuration>
<recent_temporary>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="Python.assigment" />
</list>
</recent_temporary>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="bc3859da-9355-4074-b99b-1617ce4d97d6" name="Default" comment="" />
<created>1517453263409</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1517453263409</updated>
</task>
<task id="LOCAL-00001" summary="ทำจาก pycharm ide เป็นครั้งแรก">
<created>1517454909014</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1517454909014</updated>
</task>
<task id="LOCAL-00002" summary="ทำจาก pycharm ide เป็นครั้งแรก">
<created>1517455193812</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1517455193812</updated>
</task>
<task id="LOCAL-00003" summary="ส่งการบ้าน assignment">
<created>1517463260269</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1517463260269</updated>
</task>
<task id="LOCAL-00004" summary="ส่งการบ้าน assignment">
<created>1517463360449</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1517463360449</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="ToolWindowManager">
<frame x="75" y="16" width="1303" height="780" 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.24980783" 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="7" side_tool="true" 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="7" 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="7" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32882884" sideWeight="0.5" order="2" 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="7" 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="2" side_tool="true" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.3993994" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Data View" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" 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="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="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="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="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="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="VcsManagerConfiguration">
<MESSAGE value="ทำจาก pycharm ide เป็นครั้งแรก" />
<MESSAGE value="ส่งการบ้าน assignment" />
<option name="LAST_COMMIT_MESSAGE" value="ส่งการบ้าน assignment" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/week01/hello.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week02/hellomodule.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week03/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.ipynb">
<provider selected="true" editor-type-id="ipnb-editor">
<state>
<selected id="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week01/hello.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.txt" />
<entry file="file://$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.py" />
<entry file="file://$PROJECT_DIR$/week02/hellomodule.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="18">
<caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="1" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week03/Chapter 03 Numerics and Error Analysis.ipynb.ipynb">
<provider selected="true" editor-type-id="ipnb-editor">
<state>
<selected id="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/week03/assignment.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="126">
<caret line="7" column="0" lean-forward="true" selection-start-line="7" selection-start-column="0" selection-end-line="7" 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$"
]
}
],
"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
}
#1 homework
def single_prec(v):
return (-1)**v[0]*sum([v[i]*2**(8-i) for i in range(1,32)])
def double_prec(v): ##การบ้าน
return None
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