Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement log2 and log functions in math.tact #166

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Gusarich marked this conversation as resolved.
Show resolved Hide resolved
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++) {
Gusarich marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Gusarich marked this conversation as resolved.
Show resolved Hide resolved

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
Loading