Aug 28, 2016

はじめの Python プログラム

コンピュータプログラムとは、コンピュータに対して命令を記述したものである。
上記の定義は WiKi から引用したものです。

Python のプログラムを書けるツールはたくさんがあります。例えば、Eclipse, notepad++ など、私は vim を利用します。






では、Python 実際にどのようにProgram を実行しているか?下記のグラフをご覧ください
Python ファイルを実行した際、Compiler は .pyc を変更させ、PVMで実行します。

数学関数

数学を計算するとき、Python がよく利用している Module は Math です。Math Module は Python の標準 Module であり、インストールしなくでも利用できます。

さて、Math Module を試しましょう!

HanBindeiMac:~ hanbin$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import math
>>> 
>>> math.pi
3.141592653589793
>>> 
Python でModule を引用するため、import というキーワードを使います。また、Mathの Module はどんな関数を持っているかを確認するため、dir() 関数で確認できます。

>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> 

さらに、help関数を利用し、各関数の詳細を確認することができます。例えば、

>>> help(math.pow)
Help on built-in function pow in module math:

pow(...)
    pow(x, y)
    
    Return x**y (x to the power of y).
(END)

>>> math.pow(2,3)
8.0
>>> math.log10(1000)
3.0
>>>

また、Python で標準的Module 以外ものをインストールしたい場合、pip の利用を勧めです。詳細に関しては、下記のURL をご参照ください
http://uxmilk.jp/12691 

Aug 27, 2016

割り算

まず、Pythonを入って、下記の内容を実行しましょう!

>>> 2/4
0
>>> 2.0/4
0.5
>>> 2/4.0
0.5
>>> 2.0/4.0
0.5

Python 2.7 の中に整数割整数の結果は整数となります。まだ、小数点を入った場合、結果は小数点を付け数値となります。

まだ、Python で、下記の内容を実行すると、下記のような変な結果を出ます。

>>> 10.0/3
3.3333333333333335

普通の数学の理論を使えば、正しい結果は 3.3333333333... べきです。なぜ Python で上記のような変な結果を出ます。もしかしてこれは Python の bug でしょうか? 実は、これは Python の問題ではんなく、他のプログラム言語にも同じ問題があります。問題の原因は 10進数と 2進数の変換となります。Computer が計算を実施している際、2進数で利用しております。10進数から 2進数に変換した時、小数点の問題を出ました。
例えば、10進数の 0.1 は、2進数で変換した場合、下記のような結果となります。
0.0001100110011001100110011001100110011001100110011...
つまり, 2進数で変換した結果は 10進数の 0.1 にイコールではないです。従いましてPython で上記のような結果となります。

Pythonで整数割整数の正しい結果を表示したい場合は、module を引用する方法があります。さて、Python で下記の内容を実行しましょう

>>> from __future__ import division
>>> 5 / 2
2.5

まだ、情報工学の分野の中に modulo の演算があります。Python で下記のように実行することができます

>>> 5 % 2
1
>>> 7 % 4
3
>>> divmod(5,2)
(2, 1)
>>> divmod(7,4)
(1, 3)
>>> 

divmod() は built-in function と言います。divmod の詳細の定義を確認したい場合、Python のhelp をご利用いただけますと思います。

>>> help(divmod)

Help on built-in function divmod in module __builtin__:

divmod(...)
    divmod(x, y) -> (quotient, remainder)
    
    Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x.

最後にもう一つの関数 - round()を紹介したいと思います。早速にこの関数を試します

>>> round(2.675,2)
2.67

へい〜〜、この結果は bug ではないですか?Python のDocumentの中に下記の説明があります。

Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

やっぱり、根本原因は 10進数から 2進数へ変更した際問題がありますね。