Anonymous user
Negative base numbers: Difference between revisions
→{{header|REXX}}: fixed handling of zero.
m (→{{header|Sidef}}: replaced ".pairs" with ".kv") |
(→{{header|REXX}}: fixed handling of zero.) |
||
Line 230:
n= 15; b=-10; nb=nBase(n,b); say right(n, 20) @ right(b, 3) '────►' nb ok()
n=-15; b=-10; nb=nBase(n,b); say right(n, 20) @ right(b, 3) '────►' nb ok()
n= 0; b= -5; nb=nBase(n,b); say right(n, 20) @ right(b, 3) '────►' nb ok()
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nBase: procedure;
do while x\==0
z=x//r; x=x%r
if z<0 then do; z=z-r
x=x+1
$=z || $
end /*while*/
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;
L=length(x); do j=L by -1 for L /*process each of the numerals in X. */
_=substr(x, j, 1) /*obtain a numeral of the X number. */
Line 256 ⟶ 257:
15 converted to base -10 ────► 195
-15 converted to base -10 ────► 25
0 converted to base -5 ────► 0
</pre>
|