Negative base numbers: Difference between revisions

m
→‎handles up to -10): changed some comments and whitespace.
m (→‎version 2 (up to base -71): changed wording in the REXX section header.)
m (→‎handles up to -10): changed some comments and whitespace.)
Line 1,552:
Both REXX versions use a type of   ''assert''   (a function call of '''OK''')   that converts the numbers in the
<br>negative base back to the original number in base ten &nbsp; (and issues an error message if not correct).
===versionhandles 1 (up to base -10)===
<lang rexx>/*REXX pgm converts & displays a base ten integer to a negative base number (up to -10).*/
@=' 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()
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nBase: procedure; parse arg x,r; $= $= /*obtain args; $ is the integer result.*/
if r<-10 | r>-2 then do; say 'base' r "must be in range: -2 ───► -10"; exit 13; end
do while x\==0 /*keep processing while X isn't zero.*/
z= x // r; x=x%r x= x % r /*calculate remainder; calculate int ÷.*/
if z<0 then do; z= z - r /*subtract a negative R from Z ◄──┐ */
x= x + 1 /*bump X by one. │ */
end end /* Funny "add" ►───┘ */
$= z || $ /*prepend new digit (numeral) to result*/
end /*while*/
return word($ 0, 1) /*possibly adjust for a zero value. */
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: ?=; if pBase(nbq, b)\=n then ?= ' ◄──error in negative base calculation'; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r; p= 0; $=0 0 /*obtain args; $ is the integer result.*/
if r<-10 | r>-2 then do; say 'base' r "must be in range: -2 ───► -10"; exit 13; end
L=length(x); do j=Llength(x) by -1 for L length(x) /*process each of the numerals in X. */
$= $ + substr(x,j,1) * r ** p /*add value of a numeral to $ (result).*/
p= p + 1 p=p+1 /*bump the power by 1. */
end /*j*/ end /*j*/ /* [↓] process the number "bottom-up".*/
return $</lang>
{{out|output|text=&nbsp; when using the default inputs:}}