phetsims / function-builder

"Function Builder" is an educational simulation in HTML5, by PhET Interactive Simulations at the University of Colorado Boulder.
GNU General Public License v3.0
4 stars 4 forks source link

CT: adjacent divide should have been collapsed #135

Closed pixelzoom closed 2 years ago

pixelzoom commented 3 years ago

This happened for the first time on 1/18/2021, 8:45:48 PM. It's either a new issue introduced by common-code changes, or (more likely) something that is unlikely to be exercised by CT. Note that a specific equation ([[x]/3]/1) is shown in the assertion message, so I would start by trying to manually reproduce that equation.

function-builder : fuzz : unbuilt
https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/function-builder_en.html?continuousTest=%7B%22test%22%3A%5B%22function-builder%22%2C%22fuzz%22%2C%22unbuilt%22%5D%2C%22snapshotName%22%3A%22snapshot-1611027948396%22%2C%22timestamp%22%3A1611032425249%7D&brand=phet&ea&fuzz&memoryLimit=1000
Query: brand=phet&ea&fuzz&memoryLimit=1000
Uncaught Error: Assertion failed: adjacent divide should have been collapsed: [[x]/3]/1
Error: Assertion failed: adjacent divide should have been collapsed: [[x]/3]/1
at window.assertions.assertFunction (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/assert/js/assert.js:25:13)
at new HelpfulEquationNode (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/js/common/view/equations/HelpfulEquationNode.js:227:21)
at EquationPanel.updateEquations (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/js/common/view/equations/EquationPanel.js:141:32)
at https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/js/common/view/equations/EquationPanel.js:110:14
at TinyEmitter.emit (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/axon/js/TinyEmitter.js:71:18)
at https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/axon/js/Emitter.js:35:29
at Emitter.execute (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/axon/js/Action.js:225:18)
at Emitter.emit (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/axon/js/Emitter.js:60:19)
at MathBuilder.addFunctionInstance (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/js/common/model/builder/Builder.js:149:33)
at MathBuilder.addFunctionInstance (https://bayes.colorado.edu/continuous-testing/ct-snapshots/1611027948396/function-builder/js/common/model/builder/MathBuilder.js:35:11)
id: Bayes Chrome
Snapshot from 1/18/2021, 8:45:48 PM
pixelzoom commented 3 years ago

This is still occurring very occassionally as of 8/2/2021.

pixelzoom commented 2 years ago

I haven't seen this in a long while. It's not blocking, and will not cause problems in practice.

pixelzoom commented 2 years ago

Based on the error message: adjacent divide should have been collapsed: [[x]/3]/1 ...

The failing assertion is in HelpfulEquationNode.ts, the Node that is shown in the Equation screen, with the bottom (equation) drawer open, and "simplify" checkbox unchecked.

assert && assert( !previousOperator || previousOperator !== FBSymbols.DIVIDE,
  `adjacent divide should have been collapsed: ${equation.toString()}` );

To reproduce:

screenshot_1688

In practice (assertions disabled) the screenshot below is what is shown.

screenshot_1689

So what should be shown? The ground truth for the "helpful" format is document in equation-formats.md.

Based on this rule:

(1) Adjacent addition and subtraction operations are collapsed, e.g.: + 3 - 2 → x + 1. If they collapse to zero, then the operations are dropped entirely, e.g.: * 2 + 3 - 3 → 2x

The + 0 in / 3 + 0 / 1 should be dropped, resulting in / 3 / 1. Then based on this rule:

(6) Adjacent division operations are collapsed, e.g.: / 3 / 2 → x/6

... / 3 / 1 should be collapsed to / 3.

So what is displayed should be y = x / 3.

pixelzoom commented 2 years ago

The fix for this needs to be applied in the constructor of HelpfulEquation.js, where we're currently doing nothing for the if ( currentOperand === 0 ) case.

      if ( currentOperator === FBSymbols.PLUS || currentOperator === FBSymbols.MINUS ) {

        if ( currentOperand === 0 ) {
          // ignore plus or minus zero
        }
pixelzoom commented 2 years ago

In HelpfulEquation.js, lne 113 is likely the buggy line:

      if ( stack.length > 0 ) {
        previousFunction = stack[ stack.length - 1 ];
113     previousOperator = currentOperator;
        previousOperand = previousFunction.operandProperty.get();
      }

It should have been:

113     previousOperator = previousFunction.operator;
pixelzoom commented 2 years ago

Fixed in the above commit. Screenshot:

screenshot_1690
pixelzoom commented 2 years ago

To verify in 1.2.0-rc.1, you'll need to compare its behavior to 1.0.25 (the published version).

If you have questions about whether the format is correct, see equation-formats.md or ask me.

Nancy-Salpepi commented 2 years ago

In my testing, the unsimplified equations look the same in 1.2.0-rc.1 and 1.2.25 except in the instances mentioned in the above https://github.com/phetsims/function-builder/issues/135#issuecomment-1139067805. The unsimplified equations are following the format outlined in https://github.com/phetsims/function-builder/blob/master/doc/equation-formats.md.

Closing but will reopen if something comes up.