From 9c788d31d40dcd7101b8867562ba31d57d597954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 21 Nov 2018 11:16:42 +0100 Subject: [PATCH] [poincare] Do not add parenthesis to often in childNeedsParenthesis --- poincare/src/addition.cpp | 6 +++++- poincare/src/factorial.cpp | 4 ++-- poincare/src/subtraction.cpp | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index 28844a674..929e41745 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -31,7 +31,11 @@ int AdditionNode::getPolynomialCoefficients(Context & context, const char * symb // Layout bool AdditionNode::childNeedsParenthesis(const TreeNode * child) const { - if ((static_cast(child)->isNumber() && static_cast(child)->sign() == Sign::Negative) || static_cast(child)->type() == Type::Opposite) { + if (((static_cast(child)->isNumber() + && static_cast(child)->sign() == Sign::Negative) + || static_cast(child)->type() == Type::Opposite) + && child != childAtIndex(0)) + { return true; } return false; diff --git a/poincare/src/factorial.cpp b/poincare/src/factorial.cpp index 8751ae85a..9a785f974 100644 --- a/poincare/src/factorial.cpp +++ b/poincare/src/factorial.cpp @@ -21,8 +21,8 @@ bool FactorialNode::childNeedsParenthesis(const TreeNode * child) const { if (static_cast(child)->type() == Type::Rational && !static_cast(child)->denominator().isOne()) { return true; } - Type types[] = {Type::Subtraction, Type::Opposite, Type::Multiplication, Type::Division, Type::Addition, Type::Power, Type::Factorial}; - return static_cast(child)->isOfType(types, 7); + Type types[] = {Type::Subtraction, Type::Opposite, Type::Multiplication, Type::Division, Type::Addition, Type::Power}; + return static_cast(child)->isOfType(types, 6); } // Simplification diff --git a/poincare/src/subtraction.cpp b/poincare/src/subtraction.cpp index 0078caf44..f370964e7 100644 --- a/poincare/src/subtraction.cpp +++ b/poincare/src/subtraction.cpp @@ -24,6 +24,9 @@ int SubtractionNode::polynomialDegree(Context & context, const char * symbolName // Private bool SubtractionNode::childNeedsParenthesis(const TreeNode * child) const { + if (child == childAtIndex(0)) { + return false; + } if (static_cast(child)->isNumber() && static_cast(child)->sign() == Sign::Negative) { return true; }