BcMath\Number::sqrt

(PHP 8 >= 8.4.0)

BcMath\Number::sqrt任意精度数値の平方根を取得する

説明

public BcMath\Number::sqrt(?int $scale = null): BcMath\Number

$this の平方根を返します。

パラメータ

scale
計算結果オブジェクトの BcMath\Number::scale を明示的に指定します。 null の場合、計算結果の BcMath\Number::scale は自動的に設定されます。

戻り値

平方根を新しい BcMath\Number オブジェクトとして返します。

計算結果オブジェクトの BcMath\Number::scale が自動的に設定される場合、$thisBcMath\Number::scale が使用されます。ただし、割り切れない除算が発生した場合は、 計算結果オブジェクトの BcMath\Number::scale が拡張されます。 拡張は必要に応じてのみ行われ、最大で +10 まで拡張されます。 この動作は BcMath\Number::div() と同じですので、詳しくはそちらを参照して下さい。

つまり、$thisBcMath\Number::scale5 の場合、 計算結果オブジェクトの BcMath\Number::scale5 から 15 の間になります。

エラー / 例外

このメソッドは、以下の場合に ValueError をスローします:

  • $this が負の値である場合
  • scale が範囲外の値である場合
  • 結果オブジェクトの BcMath\Number::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)
}

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top