Skip to content

Commit

Permalink
[poincare] Fix root of negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilieNumworks authored and Ecco committed Sep 5, 2017
1 parent 503786e commit 5551378
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion poincare/src/nth_root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Evaluation<T> * NthRoot::templatedEvaluate(Context& context, AngleUnit angleUnit

template<typename T>
Complex<T> NthRoot::compute(const Complex<T> c, const Complex<T> d) const {
if (c.b() == 0 && d.b() == 0) {
if (c.a() >= 0 && c.b() == 0 && d.b() == 0) {
return Complex<T>::Float(std::pow(c.a(), 1/d.a()));
}
Complex<T> invIndex = Fraction::compute(Complex<T>::Float(1), d);
Expand Down
2 changes: 1 addition & 1 deletion poincare/src/square_root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ExpressionLayout * SquareRoot::privateCreateLayout(FloatDisplayMode floatDisplay

template<typename T>
Complex<T> SquareRoot::templatedComputeComplex(const Complex<T> c) const {
if (c.b() == 0) {
if (c.b() == 0 && c.a() >= 0) {
return Complex<T>::Float(std::sqrt(c.a()));
}
return Power::compute(c, Complex<T>::Float(0.5));
Expand Down
6 changes: 6 additions & 0 deletions poincare/test/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@ QUIZ_CASE(poincare_function_evaluate) {

Complex<double> ai[1] = {Complex<double>::Float(720.0f)};
assert_parsed_expression_evaluates_to("6!", ai);

Complex<float> aj[1] = {Complex<float>::Cartesian(0.0f, 1.0f)};
assert_parsed_expression_evaluates_to("R(-1)", aj);

Complex<double> ak[1] = {Complex<double>::Cartesian(0.5, 0.86602540378443864676)};
assert_parsed_expression_evaluates_to("root(-1,3)", ak);
}

0 comments on commit 5551378

Please sign in to comment.