Anonymous user
Generalised floating point addition: Difference between revisions
Generalised floating point addition (view source)
Revision as of 18:43, 22 December 2020
, 3 years ago→any base: changed the output for this example to base 72, added/changed whitespace and comments.
m (→base ten only: separated the boxed comments to the REXX section header, added/changed whitespace and comments.) |
(→any base: changed the output for this example to base 72, added/changed whitespace and comments.) |
||
Line 1,101:
===any base===
<lang rexx>/*REXX
parse arg base . /*obtain optional argument from the CL.*/
if base=='' | base=="," then base= 10 /*Not specified? Then use the default.*/
maxW= linesize() - 1 /*maximum width allowed for displays. */
/*Not all REXXes have the LINESIZE BIF.*/
_123= 012345679; reps= 0; mult= 63 /*vars used to construct test cases. */
say ' # addend uncompressed (zoned) BCD number' /*display the header*/
say left('── ────── ─', maxW, '─') /* " header sep*/
do j=-7 to 21 /*traipse through the test cases. */
reps= reps + 1 /*increase number of repetitions. */
BCD.j= strip(copies(_123, reps)'^'mult,'L',0) /*construct a zoned BCD. */
if j//3==0 then BCD.J= '+'BCD.j /*add a leading plus sign every 3rd #. */
addend.j= '1e'pow /*build exponent addend the hard way. */
_= right(j, 2) right(addend.j, 6) /*construct the prefix for a line. */
aLine= _ BCD.j /*construct a line for the output. */
if length(aLine)<maxW then say aLine /*Does it fit on a line? Display it. */
maxDigs= length(BCD.j) + abs(pow) + 5 /*compute the maximum precision needed.*/
if maxDigs>digits() then numeric digits maxDigs /*increase digits if needed.*/
end /*j*/
say copies('═', maxW) /*display a fence for separation. */
times= 81 /*the number of times to add it. */
do k=-7 to 21 /*traipse through the test cases. */
parse var BCD.k mantissa '^'
sum= 0 /*prepare for the 81 additions. */
do times
sum= sum + x /*multiplying the hard way,
end
sum= (sum + addend.k) / 1 /*
_= format(sum, , , , 0) /*force sum
say right(k,3) 'sum=' translate(
end /*k*/ /*output is in base
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure; parse arg x 1 s 2 1 ox,tt,ii,left_,right_; f= 'BASE'
@#= 0123456789; @abc='abcdefghijklmnopqrstuvwxyz'; @abcu= @abc; upper @abcu
$= $basex()
m= length($) - 1
c= left_\=='' | right_\==''
if tt=='' then tt= 10
if ii=='' then ii= 10
i= abs(ii)
t= abs(tt)
if t==999 | t=="*" then t= m
if t>m & \c then call er81 t,2 m f'-to'
if i>m then call er81 i,2 m f'-from'
if \c then do
!= substr($, 1 + 10*(tt<0), t)
if tt<0 then != 0 ||
end
if x=='' then if c then return left_ || t || right_
else return left(!, t)
@= substr($, 1 + 10*(ii<0), i)
oS= /*the original sign placeholder. */
if s='-' | s="+" then do
x= substr(x,2)
oS= s
end
if (ii>10 & ii<37) | (ii<0 & ii>-27) then upper x
/*
if pos('-', x)\==0 |,
pos('+', x)\==0 |,
x==
x=='' then call er53 ox
parse var x w '.' g
if pos(
items.1= 0
items.2= 0
if c then do
parse var w w.1 (left_) w.2 (right_) w
do j=1
end /*j*/
end /*while*/
parse var g g.1 (left_) g.2 (right_) g
do j=1
end /*j*/
end /*while*/
_= 0;
do j=items.1 to 1 by -1; _= _ + item.1.j * (i**p)
p= p + 1
end /*j*/
w=_;
do j=1 to items.2; _=_+item.2.j/i**p
p= p + 1
end /*j*/
g= strip( strip(_, 'L', 0), ,
if g=0 then g=
end
__= w ||
_= verify(__,@'.')
if _\==0 then call er48,ox,substr(__, _, 1) '[for' f i"]"
if i\==10 then do /*convert
/*... but only if not base 10. */
_= 0; p= 0
do j=length(w) to 1 by -1 while w\==''
_= _ + ((pos( substr(w, j, 1), @) - 1) * i **p)
p= p + 1
end /*j*/
w= _; _= 0; p= 1
do j=1 for length(g
p= p + 1 /*increase the power of the base. */
else if g\=='' then g="."g /*reinsert the period if needed. */
if t\==10 then do /*convert base10
if w\=='' then do /*convert the whole number part. */
do j=1; _= t**j; if _>w then leave
end /*J*/
n=
do k=j-1 to 1 by -1; _= t**k; d= w % _
if c then n= n left_ || d || right_
else n= n || substr(!, 1 + d, 1)
w= w // _
end /*k*/
if c then w= n left_ || w || right_
else w= n || substr(!, 1 + w, 1)
end
if g\=='' then do; n= /*convert the fractional part. */
do digits()+1; if g==0 then leave
p= g * t; g = p // 1; d= trunc(p)
if c then n= n left_ || d || right_
else n= n || substr(!, d + 1, 1)
end /*digits···*/
if n==0 then n=
if n\=='' then n= '.'n /*is it only a fraction?*/
g= n
end
return oS || p( strip( space(w), 'L', 0)strip( strip(g, , 0), "T",.) 0)
/*──────────────────────────────────────────────────────────────────────────────────────*/
$basex: return @# || @abcu || @abc || space( translate(,
xrange('1'x, "fe"x), , @#'.+-'@abc || @abcu"0708090a0b0c0d"x), 0)
/*──────────────────────────────────────────────────────────────────────────────────────*/
num: procedure; parse arg x .,f,q; if x=='' then return x
if isnum(x) then return x/1; x= space( translate(x, , ','), 0)
if isnum(x) then return x/1; return numnot()
/*──────────────────────────────────────────────────────────────────────────────────────*/
putit: parse arg px,which; if \isint(px) then px= numx(px)
items.which= items.which + 1; _= items.which; item.which._= px; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
er: say '***error!***'; say; say arg(1); say; exit 13
er48: call er arg(1) 'contains invalid characters:' arg(2)
er53: call er arg(1) 'not numeric'
er81: call er arg(1) 'must be in the range:' arg(2)
isint: return datatype( arg(1), 'W')
isnum: return datatype( arg(1), 'N')
numnot: if q==1 then return x; call er53 x
numx: return num( arg(1), arg(2), 1)
p: return subword( arg(1), 1, max(1, words( arg(1) ) - 1) )</lang>
{{out|output|text= when using the (base) input of: <tt> 62 </tt>}}
<pre>
# addend uncompressed (zoned) BCD number
── ────── ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
-7 1e63 12345679^63
-6 1e54 +12345679012345679^54
-5 1e45 12345679012345679012345679^45
-4 1e36 12345679012345679012345679012345679^36
-3 1e27 +12345679012345679012345679012345679012345679^27
-2 1e18 12345679012345679012345679012345679012345679012345679^18
-1 1e9 12345679012345679012345679012345679012345679012345679012345679^9
0 1e0 +12345679012345679012345679012345679012345679012345679012345679012345679^0
1 1e-9 12345679012345679012345679012345679012345679012345679012345679012345679012345679^-9
2 1e-18 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-18
3 1e-27 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-27
4 1e-36 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-36
5 1e-45 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-45
6 1e-54 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-54
7 1e-63 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-63
8 1e-72 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-72
9 1e-81 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-81
10 1e-90 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-90
11 1e-99 12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-99
12 1e-108 +12345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679012345679^-108
13 1e-117 [193 digits]
14 1e-126 [202 digits]
15 1e-135 [212 digits]
16 1e-144 [220 digits]
17 1e-153 [229 digits]
18 1e-162 [239 digits]
19 1e-171 [247 digits]
20 1e-180 [256 digits]
21 1e-189 [266 digits]
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
-7 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-6 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-5 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-4 sum= 20wgMRBdL7rq2Kntz9B7xkSrr7S8ALuO5sk25dQY4
-3 sum= 20wgMRBdL7rq2Kntz9B7xkS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