Skip to content

Commit

Permalink
gcd and lcm don't return negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp committed Jan 14, 2025
1 parent 821aaae commit 355ab52
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/doenetml-worker/src/components/MathOperators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ function gcd(x, y, ...z) {
return gcd(x, ...z);
}
if (!y) {
return x;
return Math.abs(x);
}
return gcd(y, x % y, ...z);
}
Expand All @@ -1382,5 +1382,5 @@ function lcm(...z) {

let g = gcd(...gcdArgs);

return prod / g;
return Math.abs(prod / g);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7509,6 +7509,32 @@ describe("Math operator tests", async () => {
]);
});

it("gcd not negative", async () => {
let core = await createTestCore({
doenetML: `
<gcd name="gcd1a"><number>-135</number><number>81</number></gcd>
<gcd name="gcd1b"><number>135</number><number>-81</number></gcd>
<gcd name="gcd1c"><number>-135</number><number>-81</number></gcd>
<gcd name="gcd2a">-135 81 63</gcd>
<gcd name="gcd2b">135 -81 63</gcd>
<gcd name="gcd2c">135 81 -63</gcd>
<gcd name="gcd2d">-135 81 -63</gcd>
<gcd name="gcd2e">-135 -81 -63</gcd>
`,
});

let stateVariables = await returnAllStateVariables(core);

expect(stateVariables["/gcd1a"].stateValues.value.tree).eq(27);
expect(stateVariables["/gcd1b"].stateValues.value.tree).eq(27);
expect(stateVariables["/gcd1c"].stateValues.value.tree).eq(27);
expect(stateVariables["/gcd2a"].stateValues.value.tree).eq(9);
expect(stateVariables["/gcd2b"].stateValues.value.tree).eq(9);
expect(stateVariables["/gcd2c"].stateValues.value.tree).eq(9);
expect(stateVariables["/gcd2d"].stateValues.value.tree).eq(9);
expect(stateVariables["/gcd2e"].stateValues.value.tree).eq(9);
});

it("lcm", async () => {
let core = await createTestCore({
doenetML: `
Expand All @@ -7529,6 +7555,32 @@ describe("Math operator tests", async () => {
]);
});

it("lcm not negative", async () => {
let core = await createTestCore({
doenetML: `
<lcm name="lcm1a"><number>-135</number><number>81</number></lcm>
<lcm name="lcm1b"><number>135</number><number>-81</number></lcm>
<lcm name="lcm1c"><number>-135</number><number>-81</number></lcm>
<lcm name="lcm2a">-135 81 63</lcm>
<lcm name="lcm2b">135 -81 63</lcm>
<lcm name="lcm2c">135 81 -63</lcm>
<lcm name="lcm2d">-135 81 -63</lcm>
<lcm name="lcm2e">-135 -81 -63</lcm>
`,
});

let stateVariables = await returnAllStateVariables(core);

expect(stateVariables["/lcm1a"].stateValues.value.tree).eq(405);
expect(stateVariables["/lcm1b"].stateValues.value.tree).eq(405);
expect(stateVariables["/lcm1c"].stateValues.value.tree).eq(405);
expect(stateVariables["/lcm2a"].stateValues.value.tree).eq(2835);
expect(stateVariables["/lcm2b"].stateValues.value.tree).eq(2835);
expect(stateVariables["/lcm2c"].stateValues.value.tree).eq(2835);
expect(stateVariables["/lcm2d"].stateValues.value.tree).eq(2835);
expect(stateVariables["/lcm2e"].stateValues.value.tree).eq(2835);
});

it("extract parts of math expression", async () => {
let core = await createTestCore({
doenetML: `
Expand Down

0 comments on commit 355ab52

Please sign in to comment.