Negative base numbers: Difference between revisions

m
→‎{{header|REXX}}: simplified some code.
(Added Go)
m (→‎{{header|REXX}}: simplified some code.)
Line 822:
$=z || $ /*prepend new digit (numeral) to result*/
end /*while*/
return $
return word($ 0,1) /*when $ is NULL, then return a zero.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: ?=; if pBase(nb, b)\=n then ?=' ◄──error in negative base calculation'; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r; $p=0; p$=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=L by -1 for L /*process each of the numerals in X. */
Line 833:
end /*j*/ /* [↓] process the number "bottom-up".*/
return $</lang>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
10 converted to base -2 ────► 11110
Line 856:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
_Base: !='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
nBase: procedure; parse arg x,r,,$ /*get args; $ will be integer result. */
parse arg $; m=length(!); L=length(x); p=0
!='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
m=length(!)
if r<-m | r>-2 then do; say 'base' r "must be in range: -2 ───► -"m; exit 13; end
return
do while x\==0 /*keep processing while X isn't zero.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
z=x//r; x=x%r /*calculate remainder; calculate int ÷.*/
nBase: procedure; parse arg x,r,,$ ; call _Base /*get args; $ will be integer result. */
if z<0 then do; z=z-r /*subtract a negative R from Z ◄──┐ */
do while x\==0 /*keep processing x=x+1 /*bumpwhile X byisn't onezero.*/
z=x//r; x= end x%r /*calculate remainder; calculate int Funny "add" ►───┘ ÷.*/
if z<0 then do; $ z=substr(!, z+1,1)$ -r /*prependsubtract a negative R the newfrom numeral toZ the◄──┐ result*/
end x= x+1 /*whilebump X by one. │ */
if $=='' then $=0 end /*when $ is NULL, then return a zero. Funny "add" ►───┘ */
v$=pos(substr(x,j!, z+1),!) -1)$ /*use base R for /*prepend the new numeral's value.to the result*/
end /*while*/
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
ok: ?=; if pBase(nb, b)\=n then ?=return ' ◄──error in negative base calculation'; return ?''
/*──────────────────────────────────────────────────────────────────────────────────────*/
pBase: procedure; parse arg x,r; $=0;call _Base p=0 /*get args; $ will be integer result. */
do j=L by -1 do for while x\==0L /*keepprocess processingeach whileof the Xnumerals in isn't zeroX. */
!='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /*+-!éáµ' /*sym*/
v=pos( substr(x,j,1), !) - 1 /*use base R for the numeral's value.*/
m=length(!)
$=$ + v * r**p; p= p+1 /*add it to $ (result); bump power by 1*/
if r<-m | r>-2 then do; say 'base' r "must be in range: -2 ───► -"m; exit 13; end
L=length(x); do j=L by -1 for L end /*processj*/ each of the numerals in X. /* [↑] process the number "bottom-up".*/
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>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
10 converted to base -2 ────► 11110