pythonでmathモジュールでよく使われる方法の整理

9709 ワード

Ceil:x以上の最小整数値をとり、xが整数であればxを返します.
copysign:yの正負をxの前に加算し、0を使用できます.
cos:xの余弦を求めて、xは弧でなければなりません
degrees:xをアークから角度に変換する
e:定数を表す
exp:mathを返します.e,すなわち2.71828のx次方
expm 1:mathを返す.eのx(その値は2.71828)次の方の値から1を減らす
fabs:xの絶対値を返す
factorial:xの乗算値をとる
floor:x以下の最大整数値をとり、xが整数であれば自身を返します.
fmod:x/yの残数が得られ、その値は浮動小数点数である
frexp:xがそれぞれ0.5と1を除いて1つの値の範囲を得るメタグループ(m,e)を返します.
fsum:反復器の各要素の和を求める操作
gcd:xとyの最大公約数を返す
Hypot:xが無限大の数字であるかどうかはTrue、そうでない場合Falseを返します
isfinite:xが正の無限大または負の無限大の場合はTrueを返します.そうでない場合はFalseを返します.
isinf:xが正の無限大または負の無限大の場合はTrueを返し、そうでない場合はFalseを返します.
isnan:xが数値Trueでない場合、Falseを返します.
ldexp:x*(2**i)の値を返します.
log:xの自然対数を返し、デフォルトはeを基数とし、baseパラメータはタイミングを与え、xの対数を与えられたbaseに返し、計算式はlog(x)/log(base)である.
log 10:xの10をベースとした対数を返す
log 1 p:x+1の自然対数(基数e)を返す値
log 2:xを返すベース2対数
modf:xの小数部と整数部からなるメタグループを返す
pi:数値定数、円周率
pow:xのy次方、すなわちx**yを返す
radians:角度xを円弧に変換する
sin:x(xは弧)の正弦波値を求める
sqrt:xの平方根を求めます
tan:x(xは円弧)の正接値を返します
trunc:xの整数部分を返す
ceil
#     x       ,  x     ,   x
ceil(x)
Return the ceiling of x as an int.
This is the smallest integral value >= x.
>>> math.ceil(4.01)
5
>>> math.ceil(4.99)
5
>>> math.ceil(-3.99)
-3
>>> math.ceil(-3.01)
-3

copysign
# y      x  ,    0
copysign(x, y)
Return a float with the magnitude (absolute value) of x but the sign 
of y. On platforms that support signed zeros, copysign(1.0, -0.0) 
returns -1.0.
>>> math.copysign(2,3)
2.0
>>> math.copysign(2,-3)
-2.0
>>> math.copysign(3,8)
3.0
>>> math.copysign(3,-8)
-3.0

cos
# x   ,x     
cos(x)
Return the cosine of x (measured in radians).
#math.pi/4    ,      45 
>>> math.cos(math.pi/4)
0.7071067811865476
math.pi/3    ,      60 
>>> math.cos(math.pi/3)
0.5000000000000001
math.pi/6    ,      30 
>>> math.cos(math.pi/6)
0.8660254037844387

degrees
# x        
degrees(x)
Convert angle x from radians to degrees.
>>> math.degrees(math.pi/4)
45.0
>>> math.degrees(math.pi)
180.0
>>> math.degrees(math.pi/6)
29.999999999999996
>>> math.degrees(math.pi/3)
59.99999999999999

e
#      
>>> math.e
2.718281828459045

exp
#  math.e,   2.71828 x  
exp(x)
Return e raised to the power of x.
>>> math.exp(1)
2.718281828459045
>>> math.exp(2)
7.38905609893065
>>> math.exp(3)
20.085536923187668

expm1
#  math.e x(   2.71828)     1
expm1(x)
Return exp(x)-1.
This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
>>> math.expm1(1)
1.718281828459045
>>> math.expm1(2)
6.38905609893065
>>> math.expm1(3)
19.085536923187668

fabs
#  x    
fabs(x)
Return the absolute value of the float x.
>>> math.fabs(-0.003)
0.003
>>> math.fabs(-110)
110.0
>>> math.fabs(100)
100.0

factorial
# x     
factorial(x) -> Integral
Find x!. Raise a ValueError if x is negative or non-integral.
>>> math.factorial(1)
1
>>> math.factorial(2)
2
>>> math.factorial(3)
6
>>> math.factorial(5)
120
>>> math.factorial(10)
3628800

floor
#     x       ,  x     ,     
floor(x)
Return the floor of x as an int.
This is the largest integral value <= x.
>>> math.floor(4.1)
4
>>> math.floor(4.999)
4
>>> math.floor(-4.999)
-5
>>> math.floor(-4.01)
-5

fmod
#  x/y   ,        
fmod(x, y)
Return fmod(x, y), according to platform C.  x % y may differ.
>>> math.fmod(20,3)
2.0
>>> math.fmod(20,7)
6.0

frexp
#      (m,e),      :x   0.5 1,        ,
#2**e        ,e           ,  x/(2**e),  m  
#  x  0, m e    0,m        (0.5,1)  ,   0.5 1
frexp(x)
Return the mantissa and exponent of x, as pair (m, e).
m is a float and e is an int, such that x = m * 2.**e.
If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
>>> math.frexp(10)
(0.625, 4)
>>> math.frexp(75)
(0.5859375, 7)
>>> math.frexp(-40)
(-0.625, 6)
>>> math.frexp(-100)
(-0.78125, 7)
>>> math.frexp(100)
(0.78125, 7)

fsum
#                
fsum(iterable)
Return an accurate floating point sum of values in the iterable.
Assumes IEEE-754 floating point arithmetic.
>>> math.fsum([1,2,3,4])
10.0
>>> math.fsum((1,2,3,4))
10.0
>>> math.fsum((-1,-2,-3,-4))
-10.0
>>> math.fsum([-1,-2,-3,-4])
-10.0

gcd
#  x y      
gcd(x, y) -> int
greatest common divisor of x and y
>>> math.gcd(8,6)
2
>>> math.gcd(40,20)
20
>>> math.gcd(8,12)
4

hypot
#  (x**2+y**2),    
hypot(x, y)
Return the Euclidean distance, sqrt(x*x + y*y).
>>> math.hypot(3,4)
5.0
>>> math.hypot(6,8)
10.0

isfinite
#  x         ,   True,    False
isfinite(x) -> bool
Return True if x is neither an infinity nor a NaN, and False otherwise.
>>> math.isfinite(100)
True
>>> math.isfinite(0)
True
>>> math.isfinite(0.1)
True
>>> math.isfinite("a")
>>> math.isfinite(0.0001)
True

isinf
#  x          ,   True,    False
isinf(x) -> bool
Return True if x is a positive or negative infinity, and False otherwise.
>>> math.isinf(234)
False
>>> math.isinf(0.1)
False

isnan
#  x    True,    False
isnan(x) -> bool
Return True if x is a NaN (not a number), and False otherwise.
>>> math.isnan(23)
False
>>> math.isnan(0.01)
False

ldexp
#  x*(2**i)  
ldexp(x, i)
Return x * (2**i).
>>> math.ldexp(5,5)
160.0
>>> math.ldexp(3,5)
96.0

log
#  x     ,   e   ,base     , x        base,    :log(x)/log(base)
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
>>> math.log(10)
2.302585092994046
>>> math.log(11)
2.3978952727983707
>>> math.log(20)
2.995732273553991

log10
#  x  10     
log10(x)
Return the base 10 logarithm of x.
>>> math.log10(10)
1.0
>>> math.log10(100)
2.0
# 10 1.3      20
>>> math.log10(20)
1.3010299956639813

log1p
#  x+1     (   e)  
log1p(x)
Return the natural logarithm of 1+x (base e).
The result is computed in a way which is accurate for x near zero.
>>> math.log(10)
2.302585092994046
>>> math.log1p(10)
2.3978952727983707
>>> math.log(11)
2.3978952727983707

log2
#  x  2  
log2(x)
Return the base 2 logarithm of x.
>>> math.log2(32)
5.0
>>> math.log2(20)
4.321928094887363
>>> math.log2(16)
4.0

modf
#   x               
modf(x)
Return the fractional and integer parts of x.  Both results carry the sign
of x and are floats.
>>> math.modf(math.pi)
(0.14159265358979312, 3.0)
>>> math.modf(12.34)
(0.33999999999999986, 12.0)

pi
#    ,   
>>> print(math.pi)
3.141592653589793

pow
#  x y  , x**y
pow(x, y)
Return x**y (x to the power of y).
>>> math.pow(3,4)
81.0
>>> 
>>> math.pow(2,7)
128.0

radians
#   x     
radians(x)
Convert angle x from degrees to radians.
>>> math.radians(45)
0.7853981633974483
>>> math.radians(60)
1.0471975511965976

sin
# x(x   )    
sin(x)
Return the sine of x (measured in radians).
>>> math.sin(math.pi/4)
0.7071067811865475
>>> math.sin(math.pi/2)
1.0
>>> math.sin(math.pi/3)
0.8660254037844386

sqrt
# x    
sqrt(x)
Return the square root of x.
>>> math.sqrt(100)
10.0
>>> math.sqrt(16)
4.0
>>> math.sqrt(20)
4.47213595499958

tan
#  x(x   )    
tan(x)
Return the tangent of x (measured in radians).
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257
>>> math.tan(math.pi/3)
1.7320508075688767

trunc
#  x     
trunc(x:Real) -> Integral
Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
>>> math.trunc(6.789)
6
>>> math.trunc(math.pi)
3
>>> math.trunc(2.567)
2