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

[LEMS-2777] Bugfix: KAS handling functions around plain numbers #2266

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

handeyeco
Copy link
Contributor

@handeyeco handeyeco commented Feb 24, 2025

Summary:

See the ticket for the full break-down of the issue.

What I think was happening:

  • KAS saw f(2)
  • KAS looked for a variable, which it doesn't have (f is a function)
  • KAS said "cool, I'll take a shortcut and just treat this as a number then"
  • It tried to do math on f(2) like it was a Rational

I just removed the part of the code that was trying to take a shortcut.

Issue: LEMS-2777

Test plan:

  • Added unit tests
  • Repro steps for the UI are in the ticket

@handeyeco handeyeco self-assigned this Feb 24, 2025
@handeyeco handeyeco changed the title [LEMS-2777/rev-expression] docs(changeset): Bugfix: KAS handling functions around plain numbers [LEMS-2777] Bugfix: KAS handling functions around plain numbers Feb 24, 2025
Copy link
Contributor

github-actions bot commented Feb 24, 2025

Size Change: +149 B (+0.02%)

Total Size: 871 kB

Filename Size Change
packages/kas/dist/es/index.js 39.7 kB +149 B (+0.38%)
ℹ️ View Unchanged
Filename Size
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 11.1 kB
packages/math-input/dist/es/index.js 78.2 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 29.7 kB
packages/perseus-editor/dist/es/index.js 276 kB
packages/perseus-linter/dist/es/index.js 22.8 kB
packages/perseus-score/dist/es/index.js 20.6 kB
packages/perseus/dist/es/index.js 366 kB
packages/perseus/dist/es/strings.js 6.57 kB
packages/pure-markdown/dist/es/index.js 4.14 kB
packages/simple-markdown/dist/es/index.js 13.1 kB

compressed-size-action

Copy link
Contributor

github-actions bot commented Feb 24, 2025

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (8dcc700) and published it to npm. You
can install it using the tag PR2266.

Example:

pnpm add @khanacademy/perseus@PR2266

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR2266

Comment on lines 428 to 431
// if no variables, only need to evaluate once
if (!varList.length && !this.has(Unit) && !other.has(Unit)) {
return equalNumbers(this.eval(), other.eval());
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have very low confidence that this change won't introduce a bug. KAS has always been very tricky to me. However all new and old tests are passing, I'm going to encourage a thorough QE run, and I would love if I could get a thoughts/feels from other devs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, this also passes tests:

        const varAndFuncList = _.union(
            this.getVars(/* excludeFunc */ false),
            other.getVars(/* excludeFunc */ false),
        );
        // if no variables, only need to evaluate once
        if (!varAndFuncList.length && !this.has(Unit) && !other.has(Unit)) {
            return equalNumbers(this.eval(), other.eval());
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the comment it makes me think that this was an optimization to avoid unnecessary computation. Out of curiosity, did this return statement ever get hit when running the tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the alternative you've proposed makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, did this return statement ever get hit when running the tests?

I tested it out and it seems to hit that return statement in the "evaluate only" tests. I think it was an optimization for cases like expect("2+2").toEqualExpr("4");

Copy link
Contributor

@kevinb-khan kevinb-khan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I think you should go with the alternative you suggested, but both approaches work.

Comment on lines 428 to 431
// if no variables, only need to evaluate once
if (!varList.length && !this.has(Unit) && !other.has(Unit)) {
return equalNumbers(this.eval(), other.eval());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the comment it makes me think that this was an optimization to avoid unnecessary computation. Out of curiosity, did this return statement ever get hit when running the tests?

Comment on lines 428 to 431
// if no variables, only need to evaluate once
if (!varList.length && !this.has(Unit) && !other.has(Unit)) {
return equalNumbers(this.eval(), other.eval());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the alternative you've proposed makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants