Generalised floating point addition: Difference between revisions

m
→‎base ten only: separated the boxed comments to the REXX section header, added/changed whitespace and comments.
(Added Wren)
m (→‎base ten only: separated the boxed comments to the REXX section header, added/changed whitespace and comments.)
Line 976:
=={{header|REXX}}==
===base ten 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 -) │
┌─┘ This REXX program uses an uncompressed (or zoned) BCD which └─┐
│ is optional. An exponent is also allowed which is preceded by a ^. │
│ consumes one byte for each represented digit. A leading sign (+ or -) │
│ The value of the exponent may have 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 Each (+numeral or -(digit). is stored as its own character (glyph), as well
│ as the signs and exponent indicator. There is essentially no limit on │
│ Each numeral (digit) is stored as its own character (glyph), as well │
│ the number of digits in the mantissa or the exponent, but the value of │
│ as the signs and exponent indicator. There is essentially no limit on │
the number of digits in the mantissa or the exponent, butis thelimited to around 16 million. The mantissa may also value of
have a decimal point (.).
│ 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
Method: a table of twenty-eightadding that BCD numbersnumber is81 built,times and a(essentially multiplying by 81), test case
│ and then a number is added to that sum, and the resultant sum should │
│ of adding that BCD number 81 times (essentially multiplying by 81), │
and then a number is added to that sum, andresult in the resultantfinal sum shouldof 1e72 (for all cases).
result in the final sum of 1e72 (for all cases).
└─┐ The number of digits for the precision is automatically adjusted. ┌─┘
│ │
└────────────────────────────────────────────────────────────────────┘*/
└─┐ The number of digits for the precision is automatically adjusted. ┌─┘
<lang rexx>/*REXX pgmprogram toperforms perform generalized floating point addition using BCD nums numbers. */
└────────────────────────────────────────────────────────────────────┘*/
maxW= linesize() - 1 /*maxmaximum 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, '─') /*hdr " 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 #. */
parse var BCD.j '^' pow /*get the exponent part of the #number. */
addend.j= '1e'pow /*build theexponent addend the hard way. */
_=right(j, 2) right(addend.j, 6) /*construct the prefix for a line. */
aLine= _ BCD.j /*construct a line offor the output. */
if length(aLine)<maxW then say aLine /*FitDoes it fit on a line? Display it. */
else say _ ' ['length(BCD.j) ' "digits]'" /*otherotherwise...*/
mult= mult - 9 /*decrease the multiplier's exponent. */
maxDigs= length(BCD.j) + abs(pow) + 5 /*compute maxthe maximum precision needed. */
if maxDigs>digits() then numeric digits maxDigs /*inflateincrease 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 '^' exponent /*decompose the zoned BCD num number. */
x= mantissa'e'exponent /*reconstitute the original numnumber. */
sum= 0 /*prepare for the 81 additions. */
do times
sum= sum + x /*multiplying the hard way, yupyuppers! */
end
 
sum= (sum + addend.k) / 1 /*aone waymethod to elide trailing zeroes. */
_= format(sum, , , , 0) /*force sum ──►exponentional──► fmtexponential format. */
say right(k,3) 'sum=' translate(_, "e", 'E') /*letsuse a lowercase the "E" for exponents. */
end /*k*/
exit /*stick a fork in it, we're all done. */</lang>
 
exit /*stick a fork in it, we're done.*/</lang>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here ──►&nbsp; ───► &nbsp; [[LINESIZE.REX]].<br>
 
'''{{out|output''|text=:}}'
<pre>
# addend uncompressed (zoned) BCD number