トップ «前の日記(2012-06-03(Sun)) 最新 次の日記(2012-06-07(Thu))» 編集

屑俺日記

僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。


2012-06-04(Mon) 月食は無理みたいだ

分数

を扱うモジュールのはなしが dive into python3 に出てきた。

>>> import fractions
 
>>> dir(fractions)
['Fraction', '_RATIONAL_FORMAT', '__all__', '__builtins__',
 '__doc__', '__file__', '__name__', '__package__', 'gcd',
 'math', 'numbers', 'operator', 're']
 
>>> frac = fractions.Fraction(1, 5)
 
>>> frac
Fraction(1, 5)
 
>>> frac * 5
Fraction(1, 1)
 
>>> int(frac * 5)
1
 
>>> float(frac * 5)
1.0
 
>>> float(frac * 3)
0.6
 
>>> int(frac * 3)
0
 
>>> int(frac * 6)
1
 
>>> frac * (frac - frac)
Fraction(0, 1)
 
>>> frac / (frac - frac)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.1/fractions.py", line 314, in forward
    return monomorphic_operator(a, b)
  File "/usr/lib/python3.1/fractions.py", line 364, in _div
    a.denominator * b.numerator)
  File "/usr/lib/python3.1/fractions.py", line 114, in __new__
    raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
ZeroDivisionError: Fraction(1, 0)
 
>>> (frac - frac) / frac
Fraction(0, 1)

愚にもつかない...か?

>>> ((True + True) * (True + True)) ** ((True + True) * (True + True))
256

まだまだ、馬鹿馬鹿しさが足りないことだけは確か。


リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。

index.htmlは ここから。