Skip to content

Commit

Permalink
rework log function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Mar 18, 2024
1 parent 207d90c commit 708cadc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,15 @@ return b.end_cell().begin_parse();",
},
{
"code": {
"code": "return __tact_log2(num) / __tact_log2(base);",
"code": "if (num < base) {
return 0;
}
int result = 0;
while (num >= base) {
num = num / base;
result += 1;
}
return result;",
"kind": "generic",
},
"comment": null,
Expand Down Expand Up @@ -6833,7 +6841,15 @@ return b.end_cell().begin_parse();",
},
{
"code": {
"code": "return __tact_log2(num) / __tact_log2(base);",
"code": "if (num < base) {
return 0;
}
int result = 0;
while (num >= base) {
num = num / base;
result += 1;
}
return result;",
"kind": "generic",
},
"comment": null,
Expand Down Expand Up @@ -10680,7 +10696,15 @@ return b.end_cell().begin_parse();",
},
{
"code": {
"code": "return __tact_log2(num) / __tact_log2(base);",
"code": "if (num < base) {
return 0;
}
int result = 0;
while (num >= base) {
num = num / base;
result += 1;
}
return result;",
"kind": "generic",
},
"comment": null,
Expand Down
10 changes: 9 additions & 1 deletion src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,15 @@ export function writeStdlib(ctx: WriterContext) {
ctx.context('stdlib');
ctx.body(() => {
ctx.write(`
return __tact_log2(num) / __tact_log2(base);
if (num < base) {
return 0;
}
int result = 0;
while (num >= base) {
num = num / base;
result += 1;
}
return result;
`);
});
});
Expand Down
49 changes: 45 additions & 4 deletions src/test/feature-math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,51 @@ describe('feature-math', () => {

for (let num = 1n; num <= 10n; num++) {
for (let base = 2n; base <= 10; base++) {
const a = Math.floor(Math.log2(Number(num)));
const b = Math.floor(Math.log2(Number(base)));
const c = BigInt(Math.floor(a / b));
expect(await contract.getLog(num, base)).toBe(c);
const logarithm = BigInt(
Math.floor(Math.log2(Number(num)) / Math.log2(Number(base)))
);
expect(await contract.getLog(num, base)).toBe(logarithm);
}
}

expect(await contract.getLog2(0n)).toBe(-1n);
expect(await contract.getLog(0n, 2n)).toBe(0n);

const maxint = 2n ** 256n - 1n;

function bigIntLogBase(num: bigint, base: bigint) {
let result = 0n;
while (num >= base) {
num = num / base;
result += 1n;
}
return result;
}

for (let num = maxint - 100n; num <= maxint; num++) {
expect(await contract.getLog2(num)).toBe(255n);
}

for (let num = maxint - 10n; num <= maxint; num++) {
for (let base = 2n; base <= 10; base++) {
expect(await contract.getLog(num, base)).toBe(
bigIntLogBase(num, base)
);
}
}

for (let num = maxint / 2n - 50n; num <= maxint / 2n; num++) {
expect(await contract.getLog2(num)).toBe(254n);
}
for (let num = maxint / 2n + 1n; num <= maxint / 2n + 50n; num++) {
expect(await contract.getLog2(num)).toBe(255n);
}

for (let num = maxint / 2n - 5n; num <= maxint / 2n + 5n; num++) {
for (let base = 2n; base <= 10; base++) {
expect(await contract.getLog(num, base)).toBe(
bigIntLogBase(num, base)
);
}
}
});
Expand Down

0 comments on commit 708cadc

Please sign in to comment.