google / mathsteps

Step by step math solutions for everyone
https://socratic.org
Apache License 2.0
2.12k stars 275 forks source link

don't add parenthesis before subtracting/adding (equations) #115

Open evykassirer opened 7 years ago

evykassirer commented 7 years ago

as discussed in #90 (thanks @arve0 !)

I have this simple equation: x + 1 = 2

Here are the steps from solveEquation():

  1. SUBTRACT_FROM_BOTH_SIDES x + 1 = 2 => (x + 1) - 1 = 2 - 1
  2. SIMPLIFY_LEFT_SIDE (x + 1) - 1 = 2 - 1 => x = 2 - 1
    1. COLLECT_AND_COMBINE_LIKE_TERMS x + 1 - 1 = 2 - 1 => x + 0 = 2 - 1
      1. COLLECT_LIKE_TERMS x + 1 - 1 = 2 - 1 => x + (1 - 1) = 2 - 1
      2. SIMPLIFY_ARITHMETIC x + 1 - 1 = 2 - 1 => x + 0 = 2 - 1
    2. REMOVE_ADDING_ZERO x + 0 = 2 - 1 => x = 2 - 1
  3. SIMPLIFY_RIGHT_SIDE x = 2 - 1 => x = 1

I expected newEquation of step 1 to be without the parenthesis, like in substep a (2 > i > a). Might be a reason the parenthesis is added? For subtracting several terms, maybe?

Edit: To be a bit more specific, I expected (x + 1) - 1 = 2 - 1 to be x + 1 - 1 = 2 - 1.

evykassirer commented 7 years ago

so this happens in performTermOperationOnExpression in EquationOperations.js

we should add parenthesis if the operator is / or * or ^ e.g. 2y = 2x + 2 => y = (2x + 2) / 2

we should not though, if the operator is + or -, because we don't need parens to add/subtract to both sides

this should be a change that's only a few lines :)

rshconn commented 7 years ago

I'm working on this!

evykassirer commented 7 years ago

RACHEL! 😸 ❤️

excited to have you contributing

kevinbarabash commented 7 years ago

@evykassirer are the parens necessary for the /, *, and ^? Wouldn't the following expression tree:

{
   type: "OperationNode",
   op: "/",
   args: [
       {
           type: "OperationNode",
           op: "+",
           args: [
               { type: SymbolNode, name: "x" }, 
               { type: ConstantNode, value: "2" }
           ],
       },
       { type: ConstantNode, value: "2" }
   ]
}

render as (x + 2) / 2?

evykassirer commented 7 years ago

it does when you print it with .toString() but not with our handmade print() (I think?) (which was made before we were able to use toString()

I kind of like the idea of knowing exactly where parens are, especially because sometimes we explicitly add them for pedagogy reasons (e.g. (x + x) + (2 + 2)) but maybe we could consider having a smarter print function and not as many parens in the tree. What do you think?

(I'm okay with this change going in for now though)

evykassirer commented 7 years ago

(conversation about print continued in #126 )