日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php里面的 n算是,用PHP中的bcmath计算第N个根

發布時間:2025/3/11 php 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php里面的 n算是,用PHP中的bcmath计算第N个根 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

好吧,似乎PHP和BC lib有一些限制,在網上搜索后我發現這個

interesting article/code:

所以你應該使用這個功能:

function NRoot($num, $n) {

if ($n<1) return 0; // we want positive exponents

if ($num<=0) return 0; // we want positive numbers

if ($num<2) return 1; // n-th root of 1 or 2 give 1

// g is our guess number

$g=2;

// while (g^n < num) g=g*2

while (bccomp(bcpow($g,$n),$num)==-1) {

$g=bcmul($g,"2");

}

// if (g^n==num) num is a power of 2, we're lucky, end of job

if (bccomp(bcpow($g,$n),$num)==0) {

return $g;

}

// if we're here num wasn't a power of 2 :(

$og=$g; // og means original guess and here is our upper bound

$g=bcdiv($g,"2"); // g is set to be our lower bound

$step=bcdiv(bcsub($og,$g),"2"); // step is the half of upper bound - lower bound

$g=bcadd($g,$step); // we start at lower bound + step , basically in the middle of our interval

// while step!=1

while (bccomp($step,"1")==1) {

$guess=bcpow($g,$n);

$step=bcdiv($step,"2");

$comp=bccomp($guess,$num); // compare our guess with real number

if ($comp==-1) { // if guess is lower we add the new step

$g=bcadd($g,$step);

} else if ($comp==1) { // if guess is higher we sub the new step

$g=bcsub($g,$step);

} else { // if guess is exactly the num we're done, we return the value

return $g;

}

}

// whatever happened, g is the closest guess we can make so return it

return $g;

}

echo NRoot("18446744073709551616","64");

?>

希望這有用……

總結

以上是生活随笔為你收集整理的php里面的 n算是,用PHP中的bcmath计算第N个根的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。