(PHP 8 >= 8.4.0)
BcMath\Number::sqrt — 任意精度数値の平方根を取得する
scalenull の場合、計算結果の BcMath\Number::scale は自動的に設定されます。
平方根を新しい BcMath\Number オブジェクトとして返します。
計算結果オブジェクトの BcMath\Number::scale が自動的に設定される場合、$this の
BcMath\Number::scale が使用されます。ただし、割り切れない除算が発生した場合は、
計算結果オブジェクトの BcMath\Number::scale が拡張されます。
拡張は必要に応じてのみ行われ、最大で +10 まで拡張されます。
この動作は BcMath\Number::div() と同じですので、詳しくはそちらを参照して下さい。
つまり、$this の BcMath\Number::scale が 5 の場合、
計算結果オブジェクトの BcMath\Number::scale は 5 から
15 の間になります。
このメソッドは、以下の場合に ValueError をスローします:
scale が範囲外の値である場合例1 BcMath\Number::sqrt() の例
<?php
var_dump(
new BcMath\Number('2')->sqrt(),
new BcMath\Number('2')->sqrt(3),
new BcMath\Number('4')->sqrt(),
new BcMath\Number('4')->sqrt(3),
);
?>上の例の出力は以下となります。
object(BcMath\Number)#2 (2) {
["value"]=>
string(12) "1.4142135623"
["scale"]=>
int(10)
}
object(BcMath\Number)#3 (2) {
["value"]=>
string(5) "1.414"
["scale"]=>
int(3)
}
object(BcMath\Number)#4 (2) {
["value"]=>
string(1) "2"
["scale"]=>
int(0)
}
object(BcMath\Number)#5 (2) {
["value"]=>
string(5) "2.000"
["scale"]=>
int(3)
}