Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
51
Task/Nth-root/Bracmat/nth-root-2.bracmat
Normal file
51
Task/Nth-root/Bracmat/nth-root-2.bracmat
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
"The body of Pow will be compiled twice: once as the the code hidden in the
|
||||
UFP object called 'POW' (see below) and once as a local function of the code
|
||||
hidden in the UFP object called 'Root' (also see below)."
|
||||
& ( Pow
|
||||
= (s.x) (s.e)
|
||||
. 1:?r
|
||||
& 0:?i
|
||||
& whl
|
||||
' ( !i:<!e
|
||||
& !x*!r:?r
|
||||
& 1+!i:?i
|
||||
)
|
||||
& !r
|
||||
)
|
||||
& "The next expression is a macro expression that expands $Pow to the body of
|
||||
the Pow function defined above.
|
||||
There is another local function, called 'even'."
|
||||
&
|
||||
' ( (s.n) (s.x)
|
||||
. (Pow=$Pow)
|
||||
& ( even
|
||||
= (s.v)
|
||||
. floor$(!v*1/2):?v/2
|
||||
& subtract$(!v,2*!v/2)
|
||||
)
|
||||
& ( !x:0
|
||||
| ( !n:<1
|
||||
| !x:<0&even$!n:0
|
||||
)
|
||||
& divide$(0,0)
|
||||
| 0x1p-52*10:?EPS
|
||||
& 0x1p-52*-10:?EPS-
|
||||
& 1:?r
|
||||
& !n+-1:?n-1
|
||||
& whl
|
||||
' ( divide
|
||||
$ (divide$(!x,Pow$(!r,!n-1))+-1*!r,!n)
|
||||
: ?d
|
||||
& !d+!r:?r
|
||||
& (!d:~<!EPS|!d:~>!EPS-)
|
||||
)
|
||||
& !r
|
||||
)
|
||||
)
|
||||
: (=?root)
|
||||
& "Create two UFP objects, POW and ROOT. They are each others' inverse."
|
||||
& new$(UFP,Pow):?POW
|
||||
& new$(UFP,root):?Root
|
||||
& 15:?n
|
||||
& (POW..go)$("-3.14159",15):?x
|
||||
& out$((Root..go)$(!n,!x));
|
||||
|
|
@ -1,11 +1,7 @@
|
|||
sub nth-root ($n, $A, $p=1e-9)
|
||||
{
|
||||
my $x0 = $A / $n;
|
||||
loop {
|
||||
my $x1 = (($n-1) * $x0 + $A / ($x0 ** ($n-1))) / $n;
|
||||
return $x1 if abs($x1-$x0) < abs($x0 * $p);
|
||||
$x0 = $x1;
|
||||
}
|
||||
sub infix:<√>($n, $A) {
|
||||
.tail given $A / $n, { (($n-1) * $_+ $A / ($_** ($n-1))) / $n } ... * ≅ *;
|
||||
}
|
||||
|
||||
say nth-root(3,8);
|
||||
use Test;
|
||||
plan 9;
|
||||
is-approx ($_√pi)**$_, pi for 2 .. 10;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue