RosettaCodeData/Task/Negative-base-numbers/EasyLang/negative-base-numbers.easy

29 lines
481 B
Text
Raw Permalink Normal View History

2024-07-13 15:19:22 -07:00
func$ encode n b .
2025-06-11 20:16:52 -04:00
if n = 0 : return "0"
2024-07-13 15:19:22 -07:00
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 .
2025-06-11 20:16:52 -04:00
if n$ = "0" : return 0
2024-07-13 15:19:22 -07:00
for c$ in strchars n$
c = strcode c$ - 48
tot = tot * b + c
.
return tot
.
2025-06-11 20:16:52 -04:00
proc test n b .
2024-07-13 15:19:22 -07:00
h$ = encode n b
print n & " -> " & h$ & "_" & b & " -> " & decode h$ b
.
test 10 -2
test 146 -3
test 15 -10