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>
 
===versionhandles 2 (up to base -71)===
This REXX version supports up to negative base &nbsp; '''─71''', &nbsp; 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. &nbsp; 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; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
n= 146; b= -3; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
n= 15; b= -10; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
n= -15; b= -10; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
n= 0; b= -5; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
n=-6284695; b= -62; nb q= nBase(n, b); say right(n, 20) @ right(b,3) '────►' nb q ok()
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
_Base: !='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
parse arg $; m= length(!); L= length(x); p= 0
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; x= x %r r /*calculate remainder; calculate int ÷.*/
if z<0 then do; z= z -r r /*subtract a negative R from Z ◄──┐ */
x= x +1 1 /*bump X by one. │ */
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. */
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: if pBase(nbq, b)\=n then return ' ◄──error in negative base calculation'; return ''
/*──────────────────────────────────────────────────────────────────────────────────────*/
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; p= p + 1 /*add it to $ (result); bump power by 1*/
end /*j*/ /* [↑] process the number "bottom-up".*/
return $</lang>