Anonymous user
Negative base numbers: Difference between revisions
m
→version 2 (up to base -71): changed comments and whitepace.
m (→handles up to -10): changed some comments and whitespace.) |
m (→version 2 (up to base -71): changed comments and whitepace.) |
||
Line 1,591:
</pre>
===
This REXX version supports up to negative base '''─71''', but it may not be compatible to other programming examples
<br>because the symbols (glyphs or numerals) used herein may not be in the same exact order. The symbols represented
Line 1,597:
<lang rexx>/*REXX pgm converts & displays a base ten integer to a negative base number (up to -71).*/
@=' converted to base '; numeric digits 300 /*be able to handle ginormous numbers. */
n= 10; b= -2;
n= 146; b= -3;
n= 15; b= -10;
n= -15; b= -10;
n= 0; b= -5;
n=-6284695; b= -62;
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
_Base: !='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
parse arg $; m= length(!);
if r<-m | r>-2 then do; say 'base' r "must be in range: -2 ───► -"m; exit 13; end
return
Line 1,612:
nBase: procedure; parse arg x,r; call _Base /*get args; $ will be integer result. */
do while x\==0 /*keep processing while X isn't zero.*/
z=x // r;
if z<0 then do; z= z -
x= x +
end /* Funny "add" ►───┘ */
$=substr(!, z+1, 1)$ /*prepend the new numeral to the result*/
end /*while*/
return word($ 0, 1) /*possibly adjust for a zero value. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: if pBase(
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r; call _Base 0 /*get args; $ will be integer result. */
do j=L by -1 for L /*process each of the numerals in X. */
v=pos( substr(x,j,1), !) - 1 /*use base R for the numeral's value.*/
$= $ + v * r**p;
end /*j*/ /* [↑] process the number "bottom-up".*/
return $</lang>
|