-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitsb.dnh
54 lines (53 loc) · 1.69 KB
/
itsb.dnh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
let charLookupTable = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
function IntToStringBase(num, base) {
let res = "";
while (num >= 1) {
let digit = truncate(num % base);
res = charLookupTable[digit] ~ res;
num = truncate(num / base);
}
if (res == "") {return "0";}
return res;
}
function IntToStringBaseI(num, base, interval, separator) {
return intersperseRight(IntToStringBase(num, base), interval, separator);
}
function IntToStringBaseLZ(num, base, len) {
let res = "";
ascent (i in 0 .. len) {
let digit = truncate(num % base);
res = charLookupTable[digit] ~ res;
num = truncate(num / base);
}
return res;
}
function RealToStringBase(num, base, prec, point) {
let bp = base ^ prec;
return IntToStringBase(truncate(num), base) ~
point ~
IntToStringBaseLZ(truncate((num * bp) % bp), base, prec);
}
function RealToStringBaseI(num, base, prec, i, s1, s2, point) {
let bp = base ^ prec;
return IntToStringBaseI(truncate(num), base, i, s1) ~
point ~
intersperseLeft(IntToStringBaseLZ(truncate((num * bp) % bp), base, prec), i, s2);
}
function intersperseLeft(str, interval, separator) {
let res = "";
let len = length(str);
let left = ceil(len / interval) - 1;
ascent (i in 0 .. left) {
res = res ~ str[interval * i .. interval * i + interval] ~ separator;
}
return res ~ str[interval * left .. len];
}
function intersperseRight(str, interval, separator) {
let res = "";
let len = length(str);
let left = ceil(len / interval) - 1;
ascent (i in 0 .. left) {
res = separator ~ str[len - interval * i - interval .. len - interval * i] ~ res;
}
return str[0 .. len - interval * left] ~ res;
}