トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Python 数学 記号



目次



記事一覧

キーワード

Python 数学

[Python] [数学][Python 数学]


  • 必要に応じ、import math する

 記号


指数

>>> pow(2,10)
1024
>>> 2 ** 10
1024

累乗根 (平方根、立方根)

平方根
>>> math.sqrt(16)
4.0
n乗根 (指数形式に変換)
>>> math.pow(16, (1.0/2.0))
4.0

対数

math.log(真数,底)
>>> math.log(1024,2)
10.0
自然対数
>>> math.e ** 3         # e の 3乗
20.085536923187664
>>> math.log(20.085536923187664)
3.0
常用対数
>>> 10 ** 3             # 10 の 3乗
1000
>>> math.log10(1000)
3.0

絶対値

math.fabs は floatで結果を返す
>>> math.fabs(-2)
2.0
>>> abs(-2)
2
>>> abs(-2.0)
2.0

数列の和

1 〜 10 までの和
>>> sum(range(1,11))
55
1 〜 10 まで奇数の和
>>> sum(range(1,11,2))
25

階乗

>>> math.factorial(5)
120

三角関数

数学 Python
正弦 sinθ math.sin
余弦 cosθ math.cos
正接 tanθ math.tan
逆正弦 arcsin math.asin
逆余弦 arccos math.acos
逆正接 arctan math.atan
度→ ラジアン math.radians
ラジアン→ 度 math.degrees
円周率(π)の値
>>> math.pi
3.1415926535897931
30°をラジアンに換算
  • 360°= 2πR なので
>>> 30 * math.pi / 180
0.52359877559829882
  • 関数を使う
>>> math.radians(30)
0.52359877559829882
60°をラジアンに換算
>>> math.radians(60)
1.0471975511965976
>>> 60 * math.pi / 180
1.0471975511965976
π/6を度に換算
  • R = 180°
>>> 180 / 6
30
  • 関数を使う
>>> math.degrees(math.pi / 6)
29.999999999999996
  • Decimalを使ってみる
>>> from decimal import Decimal
>>> math.degrees(Decimal(str(math.pi)) / Decimal('6'))
30.000000000001975
sin 30°の値
  • math.sin(radians) なので
>>> math.sin(30.0 / 180.0 * math.pi)
0.49999999999999994
  • Decimalを使ってみる
>>> math.sin(Decimal(str(math.pi)) / Decimal('6.0'))
0.50000000000002986
sinπ/6の値
>>> math.sin(math.pi / 6.0)
0.49999999999999994
arcsin(1/2)の値
>>> math.asin(1.0 / 2.0)
0.52359877559829893
  • arcsin は sin の逆関数
>>> math.sin(math.asin(1.0 / 2.0))
0.5
arcsin(1/2)の演算結果を度単位に換算
>>> math.degrees(math.asin(1.0 / 2.0))
30.000000000000004
  • arcsin は sin の逆関数
>>> math.sin(math.radians(30))
0.49999999999999994
ネイピア数(e)の値
>>> math.e
2.7182818284590451



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.