Arithmetic/Integer: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: changed the program (for validating the input), add/changed comments and whitespace.)
Line 2,621: Line 2,621:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm gets 2 integers from the C.L. or via prompt, shows some opers*/
<lang rexx>/*REXX pgm gets 2 integers from the C,L. or via prompt; shows some operations.*/
numeric digits 20 /*all numbers are rounded at ··· */
numeric digits 20 /*#s are round at 20th significant dig.*/
/*··· the 20th significant digit.*/
parse arg x y . /*maybe the integers are on the C.L. */
parse arg x y . /*maybe the integers are on C.L.?*/
if y=='' then do /*nope, then prompt user for 'em.*/
say "─────Enter two integer values (separated by blanks):"
parse pull x y .
end
do 2 /*show A with B, then B with A.*/
say /*show blank line for eyeballing.*/


do while \datatype(x,'W') | \datatype(y,'W') /*both X and Y must be ints.*/
call show 'addition' , "+", x+y
say "─────Enter two integer values (separated by blanks):"
call show 'subtraction' , "-", x-y
parse pull x y . /*accept two items from command line. */
call show 'multiplication', "*", x*y
end /*while ··· */
call show 'int division' , "%", x%y, ' [rounds down]'
/* [↓] perform this DO loop twice. */
call show 'real division' , "/", x/y
do j=1 for 2 /*show A oper B, then B oper A.*/
call show 'div remainder' , "//", x//y, ' [sign from 1st operand]'
call show 'power' , "**", x**y
call show 'addition' , "+", x+y
call show 'subtraction' , "-", x-y
call show 'multiplication' , "*", x*y
call show 'int division' , "%", x%y, ' [rounds down]'
call show 'real division' , "/", x/y
call show 'division remainder', "//", x//y, ' [sign from 1st operand]'
call show 'power' , "**", x**y


parse value x y with y x /*swap the two values & do again.*/
parse value x y with y x /*swap the two values and perform again*/
if j==1 then say copies('═', 79) /*display a fence after the 1st round. */
end /*2*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────SHOW subroutine─────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
show: parse arg what,oper,value,comment
say right(what,25)' ' x center(oper,4) y ' ───► ' value comment
show: parse arg c,o,#,?; say right(c,25)' ' x center(o,4) y ' ───► ' # ?; return</lang>
'''output''' when using the input of: &nbsp; <tt> 17 &nbsp; -4 </tt>
return</lang>
'''output''' when using the input of: <tt> 17 -4 </tt>
<pre>
<pre>
addition 17 + -4 ───► 13
addition 4 + -17 ───► -13
subtraction 17 - -4 ───► 21
subtraction 4 - -17 ───► 21
multiplication 17 * -4 ───► -68
multiplication 4 * -17 ───► -68
int division 17 % -4 ───► -4 [rounds down]
int division 4 % -17 ───► 0 [rounds down]
real division 17 / -4 ───► -4.25
real division 4 / -17 ───► -0.23529411764705882353
div remainder 17 // -4 ───► 1 [sign from 1st operand]
division remainder 4 // -17 ───► 4 [sign from 1st operand]
power 17 ** -4 ───► 0.000011973036721303624238
power 4 ** -17 ───► 5.8207660913467407227E-11
═══════════════════════════════════════════════════════════════════════════════

addition -4 + 17 ───► 13
addition -17 + 4 ───► -13
subtraction -4 - 17 ───► -21
subtraction -17 - 4 ───► -21
multiplication -4 * 17 ───► -68
multiplication -17 * 4 ───► -68
int division -4 % 17 ───► 0 [rounds down]
int division -17 % 4 ───► -4 [rounds down]
real division -4 / 17 ───► -0.23529411764705882353
real division -17 / 4 ───► -4.25
div remainder -4 // 17 ───► -4 [sign from 1st operand]
division remainder -17 // 4 ───► -1 [sign from 1st operand]
power -4 ** 17 ───► -17179869184
power -17 ** 4 ───► 83521
</pre>
</pre>