Nth root: Difference between revisions

→‎{{header|Bracmat}}: Added floating point solution.
(→‎{{header|Bracmat}}: Added floating point solution.)
Line 1,219:
[https://mlochbaum.github.io/BQN/try.html#code=X3doaWxlXyDihpAge/CdlL3ijZ/wnZS+4oiY8J2UvV/wnZWjX/CdlL7iiJjwnZS94o2f8J2UvvCdlal9ClJvb3Qg4oaQIOKImgpSb290MSDihpAg4ouG4p+cw7fLnApSb290MiDihpAgewogIG4g8J2ViiBh4oC/cHJlYzoKICAx4oqRewogICAgcOKAv3g6CiAgICDin6gKICAgICAgeAogICAgICAoKHAgw5cgbiAtIDEpICsgYSDDtyBwIOKLhiBuIC0gMSkgw7cgbgogICAg4p+pCiAgfSBfd2hpbGVfIHsKICAgIHDigL94OgogICAgcHJlYyDiiaQgfCBwIC0geAogIH0g4p+oYSwg4oyKYcO3buKfqTsKICDwnZWoIPCdlYog8J2VqTog8J2VqCDwnZWKIPCdlanigL8xRcKvNQp9CgrigKJTaG93IDMgUm9vdCA1CuKAolNob3cgMyBSb290MSA1CuKAolNob3cgMyBSb290MiA1CuKAolNob3cgMyBSb290MiA14oC/MUXCrzE2Cgo= Try It!]
=={{header|Bracmat}}==
Until 2023, Bracmat doesdid not have floating point numbers as primitive type. Instead we havehad to use rational numbers. This code is not fast!
<syntaxhighlight lang="bracmat">( ( root
= n a d x0 x1 d2 rnd 10-d
Line 1,254:
2^(1/2)=1,4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727*10E0
5642^(1/125)=1,07154759194476751170*10E0</pre>
 
This is a floating point reimplementation of the C solution:
 
<syntaxhighlight lang="bracmat"> "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));</syntaxhighlight>
 
Output:
 
<pre>-3.1415899999999999E+00</pre>
 
=={{header|C}}==
10

edits