Generalised floating point addition: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
m (→‎{{header|ALGOL 68}}: handle fully normalised floating point.)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 524:
</pre>
[[Category:Arbitrary precision]]
 
=={{header|REXX}}==
===base 10 only===
<lang rexx>/*REXX pgm to perform generalized floating point addition using BCD nums*/
 
/*┌────────────────────────────────────────────────────────────────────┐
┌─┘ This REXX program uses an uncompressed (or zoned) BCD which └─┐
│ consumes one byte for each represented digit. A leading sign (+ or -) │
│ is optional. An exponent is also allowed which is preceded by a ^. │
│ The value of the exponent may have a leading sign (+ or -). │
│ Each numeral (digit) is stored as it's own character (gylph), as well │
│ as the signs and exponent indicator. There is essentially no limit on │
│ the number of digits in the mantissa or the exponent, but the value of │
│ the exponent is limited to around 16 million. The mantissa may also │
│ have a decimal point (.). │
│ │
│ Method: a table of twenty-eight BCD numbers is built, and a test case │
│ of adding that BCD number 81 tinmes (essentially multiplying by 81), │
│ and then a number is added to that sum, and the resulant sum should │
│ result in the final sum of 1e72 (for all cases). │
│ │
└─┐ The number of digits for the precision is automatically adjusted. ┌─┘
└────────────────────────────────────────────────────────────────────┘*/
 
maxW=200-1 /*max width allowed for displays.*/
_123=012345679; reps=0; mult=63 /*used to construct test cases. */
say ' # addend uncompressed (zoned) BCD number' /*header.*/
say left('── ────── ─',maxW,'─') /*hdr sep*/
 
do j=-7 to 21 /*traipse through the test cases.*/
reps=reps+1 /*increase number of repititions.*/
BCD.j=strip(copies(_123,reps)'^'mult,'L',0) /*construct zoned BCD.*/
if j//3==0 then BCD.J='+'BCD.j /*add a leading + sign every 3rd#*/
parse var BCD.j '^' pow /*get the exponent part of the #.*/
addend.j='1e'pow /*build the addend the hard way. */
aLine=right(j,2) right(addend.j,6) BCD.j /*construct a line of output*/
if length(aLine)<maxW then say aLine /*Fit on a line? Display it*/
mult=mult-9 /*decrease multiplier's exponent.*/
maxDigs=length(BCD.j)+abs(pow)+5 /*compute max precision needed. */
if maxDigs>digits() then numeric digits maxDigs /*inflate if needed.*/
end /*j*/
 
say copies('═',maxW) /*display a fence for seperation.*/
times=81 /*the number of times to add it. */
 
do k=-7 to 21 /*traipse through the test cases.*/
parse var BCD.k mantissa '^' exponent /*decompose the zoned BCD num.*/
x=mantissa'e'exponent /*reconstitute the original num. */
sum=0 /*prepare for the 81 additions. */
do times
sum=sum+x /*multiplying the hard way, yup! */
end
 
sum=(sum+addend.k)/1 /*a way to elide trailing zeroes.*/
say right(k,3) 'sum='format(sum,,,,0) /*this forces exponentional fmt*/
end /*k*/</lang>
'''output'''
<pre style="overflow:scroll">
# 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
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
-7 sum=1E+72
-6 sum=1E+72
-5 sum=1E+72
-4 sum=1E+72
-3 sum=1E+72
-2 sum=1E+72
-1 sum=1E+72
0 sum=1E+72
1 sum=1E+72
2 sum=1E+72
3 sum=1E+72
4 sum=1E+72
5 sum=1E+72
6 sum=1E+72
7 sum=1E+72
8 sum=1E+72
9 sum=1E+72
10 sum=1E+72
11 sum=1E+72
12 sum=1E+72
13 sum=1E+72
14 sum=1E+72
15 sum=1E+72
16 sum=1E+72
17 sum=1E+72
18 sum=1E+72
19 sum=1E+72
20 sum=1E+72
21 sum=1E+72
</pre>
 
=={{header|J}}==