This program demonstrates an arbitrary-precision function to compute
the Nth root of any real number to any specified number of decimals.
BC stands for Binary Calculator.
N = The root (positive integer > 1) being computed.
2 = Square root, 3 = Cube root, ... etc.
x = The number for which we are computing the Nth root.
Enter (x) argument as a string such as: '11.5'
d = The number of decimals in the root value computed.
/*
Here is PHP function computes the Nth root of real number (x)
to a specified number of decimals.
It is intended for programmers who may need more than the default
double-precision (16 significant digits) of the native functions.
Author: Jay Tanner
https://neoprogrammics.com/new-menu/PHP-Nth-Root-of-X-Function/
*/
function BC_Nth_Root_of_x ($N, $xStr, $Decimals=16)
{
$x = trim($xStr);
$a = SPrintF('%1.16f', pow($x, 1/$N)); // 1st approximation
$n = $N - 1;
$q = $Decimals;
$Q = $q + 10;
for ($i=1; $i <= 50; $i++)
{
$b = bcDiv(bcAdd(bcMul($n,$a,$Q),
bcDiv($x,bcPow($a,$n,$Q),$Q),$Q),$N,$Q);
if (bcComp($a,$b,$Q) == 0) {break;} else {$a = $b;}
}
return RTrim(RTrim(bcAdd($b, '0.'.Str_Repeat(0,$q).'5',$q),'0'),'.');
}
==============================================================================
Example calls to the arbitrary-precision Nth Root of (x) function:
$NthRootX = bcNth_Root_Of_x ($N, $x, $d);
N = 3 (3rd = cube) root of x = '10191' to d = 50 decimals:
21.6806484376434463279115971018638378159519917950179
N = 5 (5th) root of x = '100' to d = 75 decimals:
2.511886431509580111085032067799327394158518100782475428679888420908243247724
N = 7 (7th) root of x = '1843657209.657209318793497885200195' to d = 24 decimals:
21.070197020795303981693594
Below is a PNG graphic of the formula being applied.