Negative base numbers: Difference between revisions
Content added Content deleted
(added RPL) |
(Added Easylang) |
||
Line 771: | Line 771: | ||
13 encoded in base -62 = D |
13 encoded in base -62 = D |
||
D decoded in base -62 = 13</pre> |
D decoded in base -62 = 13</pre> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight> |
|||
func$ encode n b . |
|||
if n = 0 |
|||
return "0" |
|||
. |
|||
while n <> 0 |
|||
r = n mod b |
|||
n = n div b |
|||
if r < 0 |
|||
n += 1 |
|||
r -= b |
|||
. |
|||
out$ = strchar (r + 48) & out$ |
|||
. |
|||
return out$ |
|||
. |
|||
func decode n$ b . |
|||
if n$ = "0" |
|||
return 0 |
|||
. |
|||
for c$ in strchars n$ |
|||
c = strcode c$ - 48 |
|||
tot = tot * b + c |
|||
. |
|||
return tot |
|||
. |
|||
proc test n b . . |
|||
h$ = encode n b |
|||
print n & " -> " & h$ & "_" & b & " -> " & decode h$ b |
|||
. |
|||
test 10 -2 |
|||
test 146 -3 |
|||
test 15 -10 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 -> 11110_-2 -> 10 |
|||
146 -> 21102_-3 -> 146 |
|||
15 -> 195_-10 -> 15 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |