Real constants and functions: Difference between revisions
Content added Content deleted
(Added Axe) |
(→sqrt (optimized): added/changed comments and whitespace.) |
||
Line 1,573: | Line 1,573: | ||
===sqrt (optimized)=== |
===sqrt (optimized)=== |
||
A [principal] square root (SQRT) function for REXX (arbitrary precision): |
A [principal] square root (SQRT) function for REXX (with arbitrary precision): |
||
<lang rexx>/*──────────────────────────────────SQRT |
<lang rexx>/*──────────────────────────────────SQRT subroutine───────────────────────────*/ |
||
sqrt: procedure; |
sqrt: procedure; parse arg x; if x=0 then return 0 /*handle 0 case.*/ |
||
⚫ | |||
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.*/ |
|||
⚫ | |||
d=digits() /*get the current numeric precision. */ |
|||
m.=9 /*technique uses just enough digits. */ |
|||
h=d+6 /*use extra decimal digits for accuracy*/ |
|||
numeric digits 9 /*use "small" precision at first. */ |
|||
numeric form /*force scientific form of the number. */ |
|||
if fuzz()\==0 then numeric fuzz 0 /*just in case invoker has a FUZZ set.*/ |
|||
/*──────────────────────────────────SQRT_ subroutine────────────────────*/ |
|||
parse value format(x,2,1,,0) 'E0' with g 'E' _ . /*get the X's exponent.*/ |
|||
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.*/ |
|||
⚫ | |||
/*Note: to insure enough accuracy for */ |
|||
/* the result, the precision during */ |
|||
/* the SQRT calculations is increased */ |
|||
/* by two extra decimal digits. */ |
|||
do j=0 while h>9; m.j=h; h=h%2+1 /*compute the sizes (digs) of precision*/ |
|||
end /*j*/ /* [↑] precisions are stored in M. */ |
|||
⚫ | |||
parse value format(x,2,1,,0) 'E0' with g 'E' _ . /*get X's exponent.*/ |
|||
do k=j+5 to 0 by -1 /*compute the √ with increasing digs.*/ |
|||
numeric digits m.k /*each iteration, increase the digits. */ |
|||
g=(g+x/g) * .5 /*perform the nitty-gritty calculations*/ |
|||
end /*k*/ /* [↑] * .5 is faster than / 2 */ |
|||
/* |
/* [↓] normalize √ ──► original digits*/ |
||
numeric digits d /* [↓] make answer complex if X < 0. */ |
|||
⚫ | |||
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. */ |
|||
╔═╝ __ ╚═╗ |
|||
║ √ ║ |
|||
║ ║ |
|||
⚫ | |||
g=(g+x/g) * .5 /*do the nitty-gritty calculation*/ |
|||
⚫ | |||
end /*k*/ /* [↑] .5* is faster than /2 */ |
|||
⚫ | |||
/* [↓] normalize√──►original dig*/ |
|||
║ ║ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
<lang rexx>/*┌────────────────────────────────────────────────────────────────────┐ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
║ Each iteration of K (approximately) doubles the number of digits, ║ |
|||
⚫ | |||
║ but takes almost four times longer to compute (actually, around 3.8). ║ |
|||
⚫ | |||
║ ║ |
|||
⚫ | |||
║ The REXX code could be streamlined (pruned) by removing the ║ |
|||
⚫ | |||
║ The NUMERIC FUZZ 0 statement can be removed if it is known ║ |
|||
⚫ | |||
║ that it is already set to zero. (which is the default). ║ |
|||
⚫ | |||
║ ║ |
|||
║ 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)=== |
===sqrt (simple)=== |