Real constants and functions: Difference between revisions

Content added Content deleted
(Added Axe)
(→‎sqrt (optimized): added/changed comments and whitespace.)
Line 1,573:
 
===sqrt (optimized)===
A [principal] square root (SQRT) function for REXX   (with arbitrary precision):
<lang rexx>/*──────────────────────────────────SQRT subroutine─────────────────────subroutine───────────────────────────*/
sqrt: procedure; parse r= arg x; if x=0 then return 0 /*returns principal SQRThandle of0 argscase.*/
if \datatype(x,'N') then return '[n/a]' /*not numberic?Not Applicable ───if not applicablenumeric.*/
do j=1 for arg() /*process each argument specified*/
i=; if x<0 then do; x=-x; i='i'; end /*handle complex numbers if X is < 0.*/
a=arg(j) /*extract the argument specified*/
d=digits() do k=1 for words(a) /*processget eachthe numbercurrent specifiednumeric precision. */
m.=9 r=r sqrt_(word(a,k)) /*calculatetechnique sqrt,uses addjust toenough resultsdigits. */
h=d+6 end /*k*/ /* [↑] process/*use eachextra #decimal indigits Nthfor argaccuracy*/
numeric digits 9 end /*j*/ /*use "small" precision at first. /* [↑] process each #s in args. */
return r numeric form /*returnforce listscientific form of SQRTsthe number. calculated*/
if fuzz()\==0 then numeric fuzz 0 /*just in case invoker has a FUZZ set.*/
/*──────────────────────────────────SQRT_ subroutine────────────────────*/
sqrt_: procedure; parse argvalue format(x;,2,1,,0) 'E0' with g 'E' if_ x=0. then/*get returnthe 0 X's /*handle 0exponent.*/
g=(g * .5) || 'e' || (_ % 2) /*1st guesstimate for the square root. */
if pos(',',x)\==0 then x=space(translate(x,,","),0) /*elide comma.*/
/* g= g * .5 'e' (_ % 2) */ /*a shorter & concise version of above.*/
if \datatype(x,'N') then return '[n/a]' /*not numberic? not applicable*/
ox=x /*saveNote: theto originalinsure valueenough ofaccuracy for X. */
x=abs(x) /*just use positivethe result, the valueprecision ofduring X. */
d=digits() /*get the current precision.the SQRT calculations is increased */
m.=11 /*technique uses justby two extra decimal digits. enough digs*/
numeric digits m.do j=0 while h>9; m.j=h; h=h%2+1 /*compute the sizes (digs) /*use "small"of precision at first.*/
numeric form end /*j*/ /*force scientific[↑] precisions are stored in M. form of number*/
a=arg(j) /*extractnow, we start to do the argumentheavy specifiedlifting*/
parse value format(x,2,1,,0) 'E0' with g 'E' _ . /*get X's exponent.*/
g=g * .do k=j+5'E'_ % 2to 0 by -1 /*compute the /*1st guesstimate forwith squareincreasing rootdigs.*/
p=d + d%4 + 2 numeric digits m.k /*#each ofiteration, iterationsincrease (calculations)the digits. */
g=(g+x/g) * .5 /*Note: toperform insurethe enoughnitty-gritty accuracycalculations*/
end /*k*/ /* [↑] * .5 is /*forfaster thethan result, the precsion/ 2 */
/*during the[↓] SQRT calcuationsnormalize is ──► original digits*/
numeric digits d /* [↓] make answer complex if X < /*increased by two extra digits0. */
return (g/1)left('i',ox<0) /*normalize, and add possible I suffix.*/</lang>
do j=0 while p>9; m.j=p; p=p%2+1 /*compute the sizes of precision.*/
<lang rexx> ╔════════════════════════════════════════════════════════════════════╗
end /*j*/ /* [↑] precisions stored in M. */
╔═╝ __ /* [↓] da rubber meets da road. */ ╚═╗
do k=j+5 to 0 by -1 /*computewith increasing digs.*/
numeric digits m.k /*each iteration, increase digits*/
While the above REXX code seems like it's doing a lot of extra work,
g=(g+x/g) * .5 /*do the nitty-gritty calculation*/
it saves a substantial amount of processing time when the precision
end /*k*/ /* [↑] .5* is faster than /2 */
(DIGITs) is a lot greater than the default (whichdefault is nine digits).
/* [↓] normalize√──►original dig*/
numeric digits d /*restore the original precision.*/
Indeed, when computing square roots in the hundreds (even thousands)
return (g/1)left('i',ox<0) /*normalize, add possible suffix.*/</lang>
of digits, this technique reduces the amount of CPU processing time
<lang rexx>/*┌────────────────────────────────────────────────────────────────────┐
by keeping the length of the computations to a minimum (due to a large
┌─┘ √ └─┐
precision), while the accuracy at the beginning isn't important for
│ While the above REXX code seems like it's doing a lot of extra work, │
calculating the (first) guesstimate (the running square root guess).
│ it saves a substantial amount of processing time when the precision │
│ (DIGITs) is a lot greater than the default (which is nine digits). │
║ Each iteration of K (approximately) doubles the number of digits, ║
│ │
║ but takes almost four times longer to compute (actually, around 3.8). ║
│ Indeed, when computing square roots in the hundreds (even thousands) │
║ ║
│ of digits, this technique reduces the amount of CPU processing time │
║ The REXX code could be streamlined (pruned) by removing the ║
│ by keeping the length of the computations to a minimum (due to a large │
║ The NUMERIC FUZZ 0 statement can be removed if it is known ║
│ precision), while the accuracy at the beginning isn't important for │
║ that it is already set to zero. (which is the default). ║
│ calculating the (first) guesstimate (the running square root guess). │
└─┐ ┌─┘
║ Also, the NUMERIC FORM statement can be removed if it is known ║
└────────────────────────────────────────────────────────────────────┘*/</lang>
║ that the form is SCIENTIFIC (which is the default). ║
║ __ ║
┌─┘╚═╗└─┐╔═╝
╚════════════════════════════════════════════════════════════════════╝</lang>
 
===sqrt (simple)===