[gnome-calculator/gnome-3-18] Reuse generic root() method to calculate sqrt, prevents an infinite loop for imaginary numbers such



commit 771b7cdfa0e1507b090256e334695c53b3e893ba
Author: Alberto Ruiz <aruiz gnome org>
Date:   Mon Dec 28 16:06:52 2015 +0000

    Reuse generic root() method to calculate sqrt, prevents an infinite loop for imaginary numbers such as 
sqrt(-1)
    
    Fixes #757657

 lib/number.vala          |    5 +----
 tests/test-equation.vala |    1 +
 2 files changed, 2 insertions(+), 4 deletions(-)
---
diff --git a/lib/number.vala b/lib/number.vala
index d6b419c..8f94a40 100644
--- a/lib/number.vala
+++ b/lib/number.vala
@@ -623,10 +623,7 @@ public class Number : Object
     /* Sets z = √x */
     public Number sqrt ()
     {
-        var tmp = MPFloat.init2 ((Precision) precision);
-        tmp.sqrt (re_num, Round.NEAREST);
-        var z = new Number.mpfloat (tmp);
-        return z;
+        return root(2);
     }
 
     /* Sets z = ln x */
diff --git a/tests/test-equation.vala b/tests/test-equation.vala
index 10fabce..b5d46d4 100644
--- a/tests/test-equation.vala
+++ b/tests/test-equation.vala
@@ -420,6 +420,7 @@ private void test_equations ()
     test ("2×√4", "4", 0);
     test ("Sqrt (4)", "2", 0);
     test ("Sqrt (2)", "1.414213562", 0);
+    test ("Sqrt (-1)", "i", 0);
     test ("4^0.5", "2", 0);
     test ("2^0.5", "1.414213562", 0);
     test ("₃√−8", "−2", 0);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]