LoginSignup
1
3

More than 3 years have passed since last update.

VBAユーザがPython・Rを使ってみた:数学関数

Last updated at Posted at 2021-01-22

はじめに

機械学習の勉強を始めたVBAユーザです。
備忘録としてPython・Rの文法をVBAと比較しながらまとめていきたいと思います。

目次

数学関数

今回は数学関数についてまとます。

Python
Pythonでは、標準ライブラリに、 import文を使わずに使うことができる組み込み関数と、mathモジュール(数学関数)・cmathモジュール(複素数のための数学関数)で使える関数があります。

R
Rでは、特に何もしなくても数学関数が使えます。

VBA
VBAでは、VBAの関数と、WorksheetFunction(Excelのワークシート関数)が使えます。

丸めと整数化

丸め

指定した桁に丸めます。これは四捨五入とは少し違います。

Pythonの公式ドキュメントの組み込み関数roundの項からの引用:

round(number[, ndigits])
number の小数部を ndigists 桁に丸めた値を返します。ndigits が省略されたり、None だった場合、入力値に最も近い整数を返します。
round() をサポートする組み込み型では、値は 10 のマイナス ndigits 乗の倍数の中で最も近いものに丸められます; 二つの倍数が同じだけ近いなら、偶数を選ぶ方に (そのため、例えば round(0.5) と round(-0.5) は両方とも 0 に、 round(1.5) は 2 に) 丸められます。 ndigits には任意の整数値が有効となります (正の整数、ゼロ、負の整数)。 返り値は ndigits が指定されていないか None の場合は整数、そうでなければ返り値は number と同じ型です。

R-Tipsの「37. 数値計算・其の壱」からの引用:

関数 round() の丸め方は四捨五入とは微妙に異なる.IEEE では任意の桁位置における丸め処理法を次のように定めている( RjpWiki の記事,『工学のためのデータサイエンス入門』 間瀬 茂 他 著 (数理工学社) から引用した).
- 1. 一番近い丸め結果候補が1つだけなら,その数に丸める.
- 2. 一番近い丸め結果候補が2つある場合は,末尾が偶数のものに丸める(五入ばかりでなく五捨もあり得る!).
- 3. 丸め処理は1段階で行なわなければならない.
この規則は丸めによる誤差が最小になる利点がある.ただし,規則 2. が適用される場合は通常の四捨五入とは異なる結果になる場合がある.また,規則 3. は,例えば 2.45 は直に 2 と丸めるべきであって,まず 2.5 としてから,次に 3 としてはならないことを主張している.
  命令 round(2.4) round(2.5) round(3.5) round(2.51) round(3.51) round(3.46)
  結果 2     2     4     3     4      3
  規則 1. : 四捨 2. : 五捨! 2. : 五入 1. : 五入 1. : 五入 1. と 3. : 四捨

Office VBA リファレンスRound 関数の項から引用:

この VBA 関数は、主に銀行銀行の丸めと呼ばれるものを返します。 そのため、この関数を使用する前に注意してください。 予測可能な結果を得るには、Excel VBA でワークシートの Round関数を使用します。

Python

Python3
x = 1234.567890
print(round(x))    # 1235
print(round(x, 2)) # 1234.57

print(round(2.4))   # 2
print(round(2.5))   # 2
print(round(3.5))   # 4
print(round(-2.4))  # -2
print(round(-2.5))  # -2
print(round(-3.5))  # -4

R

R
x <- 1234.567890
round(x)              # 1235
round(x, digits = 2)  # 1234.57

# 指定した有効桁数に丸める(デフォルトは6桁)
signif(x, digits = 6) # 1234.57

round(2.4)   # 2
round(2.5)   # 2
round(3.5)   # 4
round(-2.4)  # -2
round(-2.5)  # -2
round(-3.5)  # -4
signif(2.4, digits = 0)   # 2
signif(2.5, digits = 0)   # 2
signif(3.5, digits = 0)   # 4
signif(-2.4, digits = 0)  # -2
signif(-2.5, digits = 0)  # -2
signif(-3.5, digits = 0)  # -4

VBA

VBA
x = 1234.56789
Debug.Print Round(x)    ' 1235
Debug.Print Round(x, 2) ' 1234.57

Debug.Print Round(2.4)  ' 2
Debug.Print Round(2.5)  ' 2
Debug.Print Round(3.5)  ' 4
Debug.Print Round(-2.4) ' -2
Debug.Print Round(-2.5) ' -2
Debug.Print Round(-3.5) ' -4

' 四捨五入
Debug.Print WorksheetFunction.Round(2.4, 0)   ' 2
Debug.Print WorksheetFunction.Round(2.5, 0)   ' 3
Debug.Print WorksheetFunction.Round(3.5, 0)   ' 4
Debug.Print WorksheetFunction.Round(-2.4, 0)  ' -2
Debug.Print WorksheetFunction.Round(-2.5, 0)  ' -3
Debug.Print WorksheetFunction.Round(-3.5, 0)  ' -4

整数化

  • $x$ の「天井」:$x$ 以上の最小の整数、$x$ の切り上げ整数化、$\lceil x \rceil$
  • $x$ の「床」 :$x$ 以下の最大の整数、$x$ の切り捨て整数化、$\lfloor x \rfloor$、ガウス記号$[x]$
  • $x$ の整数部分:$x$ のIntegral値、$x$ の「0へ向かって」整数化

Python

Python3
import math
x = 1234.567890
# 丸め
print(round(x))        # 1235
print(round(-x))       # -1235
# 天井
print(math.ceil(x))    # 1235
print(math.ceil(-x))   # -1234
# 床
print(math.floor(x))   # 1234
print(math.floor(-x))  # -1235
# 整数部分
print(math.trunc(x))   # 1234
print(math.trunc(-x))  # -1234

R

R
x <- 1234.567890
# 丸め
round(x)    # 1235
round(-x)   # -1235
# 天井
ceiling(x)  # 1235
ceiling(-x) # -1234
# 床
floor(x)    # 1234
floor(-x)   # -1235
# 整数部分
trunc(x)    # 1234
trunc(-x)   # -1234

VBA

VBA
x = 1234.56789
' 丸め
Debug.Print Round(x)    ' 1235
Debug.Print Round(-x)   ' 1235
' 天井
Debug.Print -Int(-(x))  ' 1235
Debug.Print -Int(-(-x)) '-1234
' 床
Debug.Print Int(x)      ' 1234
Debug.Print Int(-x)     ' -1235
' 整数部分(0に向かって整数化)
Debug.Print Fix(x)      ' 1234
Debug.Print Fix(-x)     ' -1234
Debug.Print Sgn(-x) * Int(Abs(-x)) ' 1234
Debug.Print Sgn(-x) * Int(Abs(-x)) ' -1234
Debug.Print WorksheetFunction.RoundDown(x, 0)  ' 1234
Debug.Print WorksheetFunction.RoundDown(-x, 0) ' -1234
' 整数部分切り上げ(0から離れて整数化)
Debug.Print WorksheetFunction.RoundUp(x, 0)    ' 1235
Debug.Print WorksheetFunction.RoundUp(-x, 0)   ' -1235

初等関数

符号と絶対値

Python

Python3
import math

# 符号反転
print(-5)            # -5
print(-(-5))         # 5

# 符号
# 符号を返す関数がない代わりに、copysign()関数がある
# copysign(x,y)は、x の大きさ (絶対値) で y と同じ符号の浮動小数点数を返す
print(math.copysign( 5,  1))  # 5.0
print(math.copysign( 5, -1))  # -5.0
print(math.copysign(-5,  1))  # 5.0
print(math.copysign(-5, -1))  # -5.0
print(math.copysign( 0,  1))  # 0.0
print(math.copysign( 0, -1))  # -0.0

# 絶対値
print(abs(-5))       # 5
print(math.fabs(-5)) # 5.0

R

R
# 符号反転
-5       # -5
-(-5)    # 5

# 符号
sign( 5) # 1
sign(-5) # -1
sign( 0) # 0


# 絶対値
abs(-5)  # 5

VBA

VBA
' 符号反転
Debug.Print -5    ' -5
Debug.Print -(-5) ' 5

' 符号
Debug.Print Sgn(5)  ' 1
Debug.Print Sgn(-5) ' -1
Debug.Print Sgn(0)  ' 0

' 絶対値
Debug.Print Abs(-5)  ' 5

べき乗と平方根

Python

Python3
import math

# べき乗
print(2 ** 10)          # 1024
print(math.pow(2, 10))  # 1024.0

# 平方根
print(2 ** (1/2))       # 1.4142135623730951
print(math.sqrt(2))     # 1.4142135623730951
print(math.pow(2, 1/2)) # 1.4142135623730951

R

R
# べき乗
2 ^ 10      # 1024

# 平方根
2 ^ (1/2)   # 1.414214
sqrt(2)     # 1.414214

VBA

VBA
' べき乗
Debug.Print 2 ^ 10   ' 1024
Debug.Print WorksheetFunction.Power(2, 10) ' 1024

' 平方根
Debug.Print Sqr(2)   ' 1.4142135623731

指数関数と対数関数

Python・Rの expm1関数とlog1p関数は、exp(x)-1log(1+x)をより正確に計算するための関数です。
なお、対数の底の変換の公式

\log_{a}{b} = \log_{c}{b} / \log_{c}{a}

も確認しています。

Python

Python3
import math

# 指数関数(exp)
print(math.exp(1))        # 2.718281828459045
# 指数関数(exp(x)-1)
print(math.exp(1e-5) - 1) # 1.0000050000069649e-05
print(math.expm1(1e-5))   # 1.0000050000166667e-05

# 指数関数
print(math.pow(2, 10))    # 1024.0

# 対数(自然対数)
print(math.log(2))        # 0.6931471805599453
# 対数(自然対数)(log(1+x))
print(math.log(1 + 1e-5)) # 9.999950000398841e-06
print(math.log1p(1e-5))   # 9.99995000033333e-06

# 対数(常用対数)
print(math.log10(2))      # 0.3010299956639812
print(math.log(2, 10))    # 0.30102999566398114

# 対数(底を2とする対数)
print(math.log2(2))       # 1.0

# 対数の底の変換
print(math.log(2) / math.log(10) - math.log(2, 10)) # 0.0

R

R
# 指数関数(exp)
exp(1)        # 2.718282
# 指数関数(exp(x)-1)
exp(1e-5) - 1 # 1.000005e-05
expm1(1e-5)   # 1.000005e-05

# 対数(自然対数)(log)
log(2)   # 0.6931472
# 対数(自然対数)(log(1+x))
log(1 + 1e-5) # 9.99995e-06
log1p(1e-5)   # 9.99995e-06

# 対数(常用対数)
log10(2)      # 0.30103
log(2, 10)    # 0.30103

# 対数(底を2とする対数)
log2(2)       # 1

# 対数の底の変換
print(log(2) / log(10) - log(2, 10)) # -5.551115e-17

VBA

VBA
' 指数関数(exp)
Debug.Print Exp(1)           ' 2.71828182845905
' WorksheetFunctionにExpはない
Debug.Print Exp(0.00001) - 1 '1.00000500000696E-05

' 対数関数(自然対数)(log)
Debug.Print Log(2)           '0.693147180559945
Debug.Print WorksheetFunction.Ln(2) ' 0.693147180559945
' 対数関数(自然対数)(log(1+x))
Debug.Print Log(1 + 0.00001) ' 9.99995000039884E-06

' 対数関数(常用対数)
Debug.Print Log(2) / Log(10)             ' 0.301029995663981
Debug.Print WorksheetFunction.Log(2)     ' 0.301029995663981
Debug.Print WorksheetFunction.Log10(2)   ' 0.301029995663981
Debug.Print WorksheetFunction.Log(2, 10) ' 0.301029995663981

' 対数関数(底を2とする対数)
Debug.Print Log(2) / Log(2)              ' 1
Debug.Print WorksheetFunction.Log(2, 2)  ' 1

三角関数と逆三角関数

三角関数 $\sin x$, $\cos x$, $\tan x$ とそれらの逆関数 $\arcsin x = \sin^{-1} x$, $\arccos x = \cos^{-1} x$, $\arctan x = \tan^{-1} x$ です。
なお、atan2関数は、Wikipedia によると「2つの引数を取るarctan(アークタンジェント)」という意味だそうです。

x = r\cos\theta \\
y = r\sin\theta \\
\theta = \arctan(y/x)

のとき、atan2(y,x) と書くか、atan2(x,y) と書くかは、言語によって異なります。

Python
Pythonは atan2(y,x) タイプです。

Python3
import math

# 三角関数
th = math.pi / 3
print(th)           # 1.0471975511965976
print(math.sin(th)) # 0.8660254037844386
print(math.cos(th)) # 0.5000000000000001
print(math.tan(th)) # 1.7320508075688767

# 逆三角関数
x = 1.0
y = math.sqrt(3)   # 1.7320508075688772
z = 2.0
print(math.asin(y / z)) # 1.0471975511965976
print(math.acos(x / z)) # 1.0471975511965976
print(math.atan(y / x)) # 1.0471975511965976
print(math.atan2(y, x)) # 1.0471975511965976

R
Pythonは atan2(y,x) タイプです。

R
# 三角関数
th <- pi / 3
th        # 1.047198
sin(th)   # 0.8660254
cos(th)   # 0.5
tan(th)   # 1.732051

# 逆三角関数
x <- 1.0
y <- sqrt(3)   # 1.732051
z <- 2.0
asin(y / z)   # 1.047198
acos(x / z)   # 1.047198
atan(y / x)   # 1.047198
atan2(y, x)   # 1.047198

VBA
VBAのWorksheetFunction.Atan2は、atan2(x,y) タイプです。

VBA
' 三角関数
' 円周率を表す関数がないので Arctan(1) * 4 で計算
Debug.Print Atn(1) * 4             ' 3.14159265358979
Debug.Print WorksheetFunction.Pi() ' 3.14159265358979
th = WorksheetFunction.Pi() / 3
Debug.Print th      ' 1.0471975511966
Debug.Print Sin(th) ' 0.866025403784439
Debug.Print Cos(th) ' 0.5
Debug.Print Tan(th) ' 1.73205080756888

' 逆三角関数
x = 1
y = Sqr(3)   ' 1.73205080756888
z = 2
' VBAの関数にはArctanしかない(Arcsin, Arccos はない)
Debug.Print WorksheetFunction.Asin(y / z)   ' 1.0471975511966
Debug.Print WorksheetFunction.Acos(x / z)   ' 1.0471975511966
Debug.Print Atn(y / x)                      ' 1.0471975511966
' Excelの関数にATAN()はあるが、WorksheetFunction.Atan はない
' Debug.Print WorksheetFunction.Atan(y / x)
Debug.Print WorksheetFunction.Atan2(x, y)   ' 1.0471975511966

双曲線関数と逆双曲線関数

双曲線関数(ハイパボリック関数, hyperbolic functions)とその逆関数の逆双曲線関数(inverse hyperbolic functions)です。

Python

Python3
import math

# 双曲線関数
print(math.sinh(0))   # 0.0
print(math.cosh(0))   # 1.0
print(math.tanh(0))   # 0.0

# 逆双曲線関数
print(math.asinh(0))  # 0.0
print(math.acosh(1))  # 0.0
print(math.atanh(0))  # 0.0

R

R
# 双曲線関数
sinh(0)   # 0
cosh(0)   # 1
tanh(0)   # 0

# 逆双曲線関数
asinh(0)  # 0
acosh(1)  # 0
atanh(0)  # 0

VBA

VBA
' 双曲線関数
' VBAの関数にはない
Debug.Print WorksheetFunction.Sinh(0)   ' 0
Debug.Print WorksheetFunction.Cosh(0)   ' 1
Debug.Print WorksheetFunction.Tanh(0)   ' 0

' 逆双曲線関数
' VBAの関数にはない
Debug.Print WorksheetFunction.Asinh(0)   ' 0
Debug.Print WorksheetFunction.Acosh(1)   ' 0
Debug.Print WorksheetFunction.Atanh(0)   ' 0

整数の関数

Python

Python3

R

R

VBA

VBA

その他の関数

ガンマ関数 $\Gamma(x)$ とガンマ関数の対数 $\log(\Gamma(x))$ 等です。

\Gamma(x) = \int_{0}^{\infty} t^{x-1} e^t dt \\
\Gamma(x+1) = x \Gamma(x), x>0 \\
\Gamma(n+1) = n!, n \in\mathbb{Z}_{>0} \\

Python

Python3
import math

# ガンマ関数
print(math.gamma(1))  # 1.0
# ガンマ関数の対数
print(math.lgamma(1)) # 0.0

R

R
# ガンマ関数
print(gamma(1))  # 1
# ガンマ関数の対数
print(lgamma(1)) # 0

VBA

VBA
' ガンマ関数
Debug.Print WorksheetFunction.Gamma(1)           ' 1
Debug.Print WorksheetFunction.GammaLn(1)         ' 0
Debug.Print WorksheetFunction.GammaLn_Precise(1) ' 0

数学定数

円周率 $\pi$、自然対数の底 $e$、タウ $\tau=2\pi$、無限大 $\infty$ など

Python

Python3
# 円周率
print(math.pi)  # 3.141592653589793
# 自然対数の底
print(math.e)   # 2.718281828459045
# タウ
print(math.tau) # 6.283185307179586
# 無限大
print(math.inf)     # inf
print(float('inf')) # inf
print(-math.inf)    # -inf
# 非数(not a number)
print(math.nan)     # nan

R

R
# 円周率
pi       # 3.141593
# 自然対数の底
exp(1)   # 2.718282

VBA

VBA
' 円周率
Debug.Print WorksheetFunction.Pi() ' 3.14159265358979
' 自然対数の底
Debug.Print Exp(1)                 ' 2.71828182845905

数ベクトル

Python(Numpy)・Rでは、数学関数をベクトルに対して使うと、ベクトルの各要素に対して関数を使った結果のベクトルが返ります。これについては、別途まとめます。

まとめ

一覧

各言語で使用する関数等を一覧にまとめます。比較のために、EXCELでの計算も示しました。

丸め・整数化

Python R VBA EXCEL
丸め round(x,n) round(x,n) Round(x, n) =ROUND(X,n)
四捨五入 WorksheetFunction
.Round(x, n)
=ROUND(X,N)
天井 math.ceil(x) ceiling(x) -Int(-(x)) =-INT(-X)
math.floor(x) floor(x) Int(x) =INT(X)
整数部分 math.trunc(x) trunc(x) Fix(x)
WorksheetFunction
.RoundDown(x, 0)
=ROUNDDOWN(X,0)
整数部分切り上げ WorksheetFunction
.RoundUp(X, 0)
=ROUNDDOWN(X,0)

注意)丸めと四捨五入の違いについては、上記を参照。
・天井      :切り上げ整数化
・床       :切り捨て整数化
・整数部分    :0に向かって整数化
・整数部分切り上げ:0から離れて整数化

符号と絶対値

Python R VBA EXCEL
符号反転 -x -x -x =-X
符号 sign(x) Sgn(x) =SIGN(X)
絶対値 abs(x)
math.fabs(x)
abs(x) Abs(x) =ABS(X)

べき乗と平方根

Python R VBA EXCEL
べき乗 x ** n
math.pow(x, n)
x ^ n x ^ n =X^N
平方根 math.sqrt(x)
x ** (1/2)
math.pow(x, 1/2)
sqrt(x)
x ^ (1/2)
x ^ (1/2)
WorksheetFunction
.Power(2, 10)
=SQRT(X)
=X^(1/2)
=POWER(X,1/2)

指数関数と対数関数

Python R VBA EXCEL
指数関数 $e^x$ math.exp(x) exp(x) Exp(x) =EXP(X)
$e^x-1$ math.exp(x)-1
math.expm1(x)
exp(x)-1
expm1(x)
Exp(x) - 1 =EXP(X) -1
$x^n$ math.pow(x,n)
x ** n
x ^ n x ^ n =X^N
自然対数 $\log x$ math.log(x) log(x) Log(x)
WorksheetFunction
.Ln(x)
=LN(X)
$\log (1+x)$ math.log(1+x)
math.log1p(x)
log(1+x)
log1p(x)
Log(1 + x) =LN(1+X)
常用対数 $\log_{10} x$ math.log10(x)
math.log(x,10)
log10(x)
log(x,10)
Log(x) / Log(10) =LOG(X)
=LOG(X,10)
$\log_{2} x$ math.log2(x)
math.log(x,2)
log2(x)
log(x, 2)
log10(x)
log(x, 2)
=LOG(X,2)
$\log_{a} x$ math.log(x,a) log(x,a) log10(x)
log(a, 10)
=LOG(X,A)

注意)対数関数のLogとLnについて、Excelワークシート関数のみ自然対数にLn、常用対数にLogを用いることに注意。

三角関数と逆三角関数

Python R VBA EXCEL
$\sin t$ math.sin(t) sin(t) Sin(t) =SIN(T)
$\cos t$ math.cos(t) cos(t) Cos(t) =COS(T)
$\tan t$ math.tan(t) tan(t) Tan(t) =TAN(T)
$\arcsin (y/z)$ math.asin(y/z) asin(y/z) WorksheetFunction
.Asin(y / z)
=ASIN(Y/Z)
$\arccos (x/z)$ math.acos(x/z) acos(x/z) WorksheetFunction
.Acos(x / z)
=ACOS(X/Z)
$\arctan (y/x)$ math.acos(x/z)
math.atan2(y,x)
atan(y/x)
atan2(y,x)
Atn(y / x)
WorksheetFunction
.Atan2(x, y)
=ATAN(Y/X)
=ATAN2(X,Y)

注意)最後の atan2 は、Python・RとExcelワークシート関数で引数の順序が異なることに注意。

双曲線関数と逆双曲線関数

Python R VBA EXCEL
$\sinh x$ math.sinh(x) sinh(x) WorksheetFunction
.Sinh(x)
=SINH(X)
$\cosh x$ math.cosh(x) cosh(x) WorksheetFunction
.Cosh(x)
=COSH(X)
$\tanh x$ math.tanh(0) tanh(x) WorksheetFunction
.Tanh(x)
=TANH(X)
$\sinh^{-1}x$ math.asinh(x) asinh(x) WorksheetFunction
.Asinh(x)
=ASINH(X)
$\cosh^{-1}x$ math.acosh(x) acosh(x) WorksheetFunction
.Acosh(x)
=ACOSH(X)
$\tanh^{-1}x$ math.atanh(x) atanh(x) WorksheetFunction
.Atanh(x)
=ATANH(X)

ガンマ関数等

Python R VBA EXCEL
$\Gamma(x)$ math.gamma(x) gamma(x) WorksheetFunction
.Gamma(x)
=GAMMA(X)
$\log(\Gamma( x))$ math.lgamma(x) lgamma(x) WorksheetFunction
.GammaLn(x)
WorksheetFunction
.GammaLn_Precise(x)
=GAMMALN(X)
=GAMMALN.PRECISE(X)

数学定数

Python R VBA EXCEL
円周率 $\pi$ math.pi pi WorksheetFunction
.Pi()
=PI()
自然対数の底 $e$ math.e exp(1) Exp(1) =EXP(1)
タウ $\tau$ math.tau `` `` =
無限大 $\infty$ math.inf `` `` =
非数(not a number) math.nan `` `` =

プログラム全体

参考までに使ったプログラムの全体を示します。

Python

Python3
import math

# 丸め

x = 1234.567890
print(round(x))    # 1235
print(round(x, 2)) # 1234.57

print(round(2.4))   # 2
print(round(2.5))   # 2
print(round(3.5))   # 4
print(round(-2.4))  # -2
print(round(-2.5))  # -2
print(round(-3.5))  # -4


# 整数化

x = 1234.567890
# 丸め
print(round(x))        # 1235
print(round(-x))       # -1235
# 天井
print(math.ceil(x))    # 1235
print(math.ceil(-x))   # -1234
# 床
print(math.floor(x))   # 1234
print(math.floor(-x))  # -1235
# 整数部分
print(math.trunc(x))   # 1234
print(math.trunc(-x))  # -1234


# 符号と絶対値

# 符号反転
print(-5)            # -5
print(-(-5))         # 5

# 符号
# 符号を返す関数がない代わりに、copysign()関数がある
# copysign(x,y)は、x の大きさ (絶対値) で y と同じ符号の浮動小数点数を返す
print(math.copysign( 5,  1))  # 5.0
print(math.copysign( 5, -1))  # -5.0
print(math.copysign(-5,  1))  # 5.0
print(math.copysign(-5, -1))  # -5.0
print(math.copysign( 0,  1))  # 0.0
print(math.copysign( 0, -1))  # -0.0

# 絶対値
print(abs(-5))       # 5
print(math.fabs(-5)) # 5.0


# べき乗と平方根

# べき乗
print(2 ** 10)          # 1024
print(math.pow(2, 10))  # 1024.0

# 平方根
print(2 ** (1/2))       # 1.4142135623730951
print(math.sqrt(2))     # 1.4142135623730951
print(math.pow(2, 1/2)) # 1.4142135623730951


# 指数関数と対数関数

# 指数関数(exp)
print(math.exp(1))        # 2.718281828459045
# 指数関数(exp(x)-1)
print(math.exp(1e-5) - 1) # 1.0000050000069649e-05
print(math.expm1(1e-5))   # 1.0000050000166667e-05

# 指数関数
print(math.pow(2, 10))    # 1024.0

# 対数(自然対数)
print(math.log(2))        # 0.6931471805599453
# 対数(自然対数)(log(1+x))
print(math.log(1 + 1e-5)) # 9.999950000398841e-06
print(math.log1p(1e-5))   # 9.99995000033333e-06

# 対数(常用対数)
print(math.log10(2))      # 0.3010299956639812
print(math.log(2, 10))    # 0.30102999566398114

# 対数(底を2とする対数)
print(math.log2(2))       # 1.0

# 対数の底の変換
print(math.log(2) / math.log(10) - math.log(2, 10)) # 0.0


# 三角関数と逆三角関数

# 三角関数
th = math.pi / 3
print(th)           # 1.0471975511965976
print(math.sin(th)) # 0.8660254037844386
print(math.cos(th)) # 0.5000000000000001
print(math.tan(th)) # 1.7320508075688767

# 逆三角関数
x = 1.0
y = math.sqrt(3)   # 1.7320508075688772
z = 2.0
print(math.asin(y / z)) # 1.0471975511965976
print(math.acos(x / z)) # 1.0471975511965976
print(math.atan(y / x)) # 1.0471975511965976
print(math.atan2(y, x)) # 1.0471975511965976


# 双曲線関数と逆双曲線関数

# 双曲線関数
print(math.sinh(0))   # 0.0
print(math.cosh(0))   # 1.0
print(math.tanh(0))   # 0.0

# 逆双曲線関数
print(math.asinh(0))  # 0.0
print(math.acosh(1))  # 0.0
print(math.atanh(0))  # 0.0


# ガンマ関数等

# ガンマ関数
print(math.gamma(1))  # 1.0
# ガンマ関数の対数
print(math.lgamma(1)) # 0.0


# 数学定数

# 円周率
print(math.pi)  # 3.141592653589793
# 自然対数の底
print(math.e)   # 2.718281828459045
# タウ
print(math.tau) # 6.283185307179586
# 無限大
print(math.inf)     # inf
print(float('inf')) # inf
print(-math.inf)    # -inf
# 非数(not a number)
print(math.nan)     # nan

R

R
# 丸め

x <- 1234.567890
round(x)              # 1235
round(x, digits = 2)  # 1234.57

# 指定した有効桁数に丸める(デフォルトは6桁)
signif(x, digits = 6) # 1234.57

round(2.4)   # 2
round(2.5)   # 2
round(3.5)   # 4
round(-2.4)  # -2
round(-2.5)  # -2
round(-3.5)  # -4
signif(2.4, digits = 0)   # 2
signif(2.5, digits = 0)   # 2
signif(3.5, digits = 0)   # 4
signif(-2.4, digits = 0)  # -2
signif(-2.5, digits = 0)  # -2
signif(-3.5, digits = 0)  # -4


# 整数化

x <- 1234.567890
# 丸め
round(x)    # 1235
round(-x)   # -1235
# 天井
ceiling(x)  # 1235
ceiling(-x) # -1234
# 床
floor(x)    # 1234
floor(-x)   # -1235
# 整数部分
trunc(x)    # 1234
trunc(-x)   # -1234


# 符号と絶対値

# 符号反転
-5       # -5
-(-5)    # 5

# 符号
sign( 5) # 1
sign(-5) # -1
sign( 0) # 0


# 絶対値
abs(-5)  # 5


# べき乗と平方根

# べき乗
2 ^ 10      # 1024

# 平方根
2 ^ (1/2)   # 1.414214
sqrt(2)     # 1.414214


# 指数関数と対数関数

# 指数関数(exp)
exp(1)        # 2.718282
# 指数関数(exp(x)-1)
exp(1e-5) - 1 # 1.000005e-05
expm1(1e-5)   # 1.000005e-05

# 対数(自然対数)(log)
log(2)   # 0.6931472
# 対数(自然対数)(log(1+x))
log(1 + 1e-5) # 9.99995e-06
log1p(1e-5)   # 9.99995e-06

# 対数(常用対数)
log10(2)      # 0.30103
log(2, 10)    # 0.30103

# 対数(底を2とする対数)
log2(2)       # 1

# 対数の底の変換
print(log(2) / log(10) - log(2, 10)) # -5.551115e-17


# 三角関数と逆三角関数

# 三角関数
th <- pi / 3
th        # 1.047198
sin(th)   # 0.8660254
cos(th)   # 0.5
tan(th)   # 1.732051

# 逆三角関数
x <- 1.0
y <- sqrt(3)   # 1.732051
z <- 2.0
asin(y / z)   # 1.047198
acos(x / z)   # 1.047198
atan(y / x)   # 1.047198
atan2(y, x)   # 1.047198


# 双曲線関数と逆双曲線関数

# 双曲線関数
sinh(0)   # 0
cosh(0)   # 1
tanh(0)   # 0

# 逆双曲線関数
asinh(0)  # 0
acosh(1)  # 0
atanh(0)  # 0


# ガンマ関数等

# ガンマ関数
print(gamma(1))  # 1
# ガンマ関数の対数
print(lgamma(1)) # 0


# 数学定数

# 円周率
pi       # 3.141593
# 自然対数の底
exp(1)   # 2.718282

VBA

VBA
Sub test_math()
Dim x As Double
Dim y As Double
Dim z As Double
Dim th As Double


' 丸め

x = 1234.56789
Debug.Print Round(x)    ' 1235
Debug.Print Round(x, 2) ' 1234.57

Debug.Print Round(2.4)  ' 2
Debug.Print Round(2.5)  ' 2
Debug.Print Round(3.5)  ' 4
Debug.Print Round(-2.4) ' -2
Debug.Print Round(-2.5) ' -2
Debug.Print Round(-3.5) ' -4

' 四捨五入
Debug.Print WorksheetFunction.Round(2.4, 0)   ' 2
Debug.Print WorksheetFunction.Round(2.5, 0)   ' 3
Debug.Print WorksheetFunction.Round(3.5, 0)   ' 4
Debug.Print WorksheetFunction.Round(-2.4, 0)  ' -2
Debug.Print WorksheetFunction.Round(-2.5, 0)  ' -3
Debug.Print WorksheetFunction.Round(-3.5, 0)  ' -4


' 整数化

x = 1234.56789
' 丸め
Debug.Print Round(x)    ' 1235
Debug.Print Round(-x)   ' 1235
' 天井
Debug.Print -Int(-(x))  ' 1235
Debug.Print -Int(-(-x)) '-1234
' 床
Debug.Print Int(x)      ' 1234
Debug.Print Int(-x)     ' -1235
' 整数部分(0に向かって整数化)
Debug.Print Fix(x)      ' 1234
Debug.Print Fix(-x)     ' -1234
Debug.Print Sgn(-x) * Int(Abs(-x)) ' 1234
Debug.Print Sgn(-x) * Int(Abs(-x)) ' -1234
Debug.Print WorksheetFunction.RoundDown(x, 0)  ' 1234
Debug.Print WorksheetFunction.RoundDown(-x, 0) ' -1234
' 整数部分切り上げ(0から離れて整数化)
Debug.Print WorksheetFunction.RoundUp(x, 0)    ' 1235
Debug.Print WorksheetFunction.RoundUp(-x, 0)   ' -1235


' 符号と絶対値

' 符号反転
Debug.Print -5    ' -5
Debug.Print -(-5) ' 5

' 符号
Debug.Print Sgn(5)  ' 1
Debug.Print Sgn(-5) ' -1
Debug.Print Sgn(0)  ' 0

' 絶対値
Debug.Print Abs(-5)  ' 5


' べき乗と平方根

' べき乗
Debug.Print 2 ^ 10   ' 1024
Debug.Print WorksheetFunction.Power(2, 10) ' 1024

' 平方根
Debug.Print Sqr(2)   ' 1.4142135623731


' 指数関数と対数関数

' 指数関数(exp)
Debug.Print Exp(1)           ' 2.71828182845905
' WorksheetFunctionにExpはない
Debug.Print Exp(0.00001) - 1 '1.00000500000696E-05

' 対数関数(自然対数)(log)
Debug.Print Log(2)           '0.693147180559945
Debug.Print WorksheetFunction.Ln(2) ' 0.693147180559945
' 対数関数(自然対数)(log(1+x))
Debug.Print Log(1 + 0.00001) ' 9.99995000039884E-06

' 対数関数(常用対数)
Debug.Print Log(2) / Log(10)             ' 0.301029995663981
Debug.Print WorksheetFunction.Log(2)     ' 0.301029995663981
Debug.Print WorksheetFunction.Log10(2)   ' 0.301029995663981
Debug.Print WorksheetFunction.Log(2, 10) ' 0.301029995663981

' 対数関数(底を2とする対数)
Debug.Print Log(2) / Log(2)              ' 1
Debug.Print WorksheetFunction.Log(2, 2)  ' 1


' 三角関数と逆三角関数

' 三角関数
' 円周率を表す関数がないので Arctan(1) * 4 で計算
Debug.Print Atn(1) * 4             ' 3.14159265358979
Debug.Print WorksheetFunction.Pi() ' 3.14159265358979
th = WorksheetFunction.Pi() / 3
Debug.Print th      ' 1.0471975511966
Debug.Print Sin(th) ' 0.866025403784439
Debug.Print Cos(th) ' 0.5
Debug.Print Tan(th) ' 1.73205080756888

' 逆三角関数
x = 1
y = Sqr(3)   ' 1.73205080756888
z = 2
' VBAの関数にはArctanしかない(Arcsin, Arccos はない)
Debug.Print WorksheetFunction.Asin(y / z)   ' 1.0471975511966
Debug.Print WorksheetFunction.Acos(x / z)   ' 1.0471975511966
Debug.Print Atn(y / x)                      ' 1.0471975511966
' Excelの関数にATAN()はあるが、WorksheetFunction.Atan はない
' Debug.Print WorksheetFunction.Atan(y / x)
Debug.Print WorksheetFunction.Atan2(x, y)   ' 1.0471975511966


' 双曲線関数と逆双曲線関数

' 双曲線関数
' VBAの関数にはない
Debug.Print WorksheetFunction.Sinh(0)   ' 0
Debug.Print WorksheetFunction.Cosh(0)   ' 1
Debug.Print WorksheetFunction.Tanh(0)   ' 0

' 逆双曲線関数
' VBAの関数にはない
Debug.Print WorksheetFunction.Asinh(0)   ' 0
Debug.Print WorksheetFunction.Acosh(1)   ' 0
Debug.Print WorksheetFunction.Atanh(0)   ' 0


' ガンマ関数
Debug.Print WorksheetFunction.Gamma(1)           ' 1
Debug.Print WorksheetFunction.GammaLn(1)         ' 0
Debug.Print WorksheetFunction.GammaLn_Precise(1) ' 0


' 数学定数

' 円周率
Debug.Print WorksheetFunction.Pi() ' 3.14159265358979
' 自然対数の底
Debug.Print Exp(1)                 ' 2.71828182845905

End Sub

参考

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3