裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

bigq クラスの数学関数

2011年02月17日 | ブログラミング

sqrt はまあ実用的

sqrt.bigq <- function(x, eps=1e-50)
{
    x2 <- as.bigq(sqrt(as.double(x)))
    repeat {
        x3 <- (x2+x/x2)/2
        if (abs(x2-x3) < eps) break
        x2 <- x3
    }
    return(x3)
}
# as.double(sqrt.bigq(5000.45789))^2

# sqrt(as.bigq(3.456))

3 乗根

qubic.root <- function(x, eps=1e-50)
{
    x2 <- as.bigq(as.double(x)^(1/3))
    repeat {
        x3 <- (x/(x2*x2)+2*x2)/3
        if (abs(x2-x3) < eps) break
        x2 <- x3
    }
    return(x3)
}
# as.double(qubic.root(123.4566789))^3

exp は取りあえず使えればよいものということで

exp.bigq <- function(x, eps=1e-50, n.term=1000)
{
    ans <- xp <- fac <- as.bigq(1)
    for (i in 1:n.term) {
        xp <- xp*x
        fac <- fac*i
        old <- ans
        ans <- ans+xp/fac
        if (abs(old-ans) < eps) {
            return(ans)
        }
    }
    warnings("収束せず")
    return(ans)
}
# log(as.double(exp.bigq(40.235)))

# exp(as.bigq(1.234))

 

 

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« 文字列型日時ののN秒後時間取得 | トップ | 与えられた数字のケタ数 »
最新の画像もっと見る

コメントを投稿

ブログラミング」カテゴリの最新記事