Re: gcalctool is incorrect in exponents of negative numbers



On Sat, 09 Jul 2016 14:55:21 +0300
tu tu <xxxsnowflake yandex ru> wrote:

gcalctool is incorrect in exponents of negative numbers
 
-4 ^ 2 = -16
 
 
wtf?
 

Apparently gnome-calculator follows the convention of written/printed
math, where −3^2 is interpreted to mean 0 − (3^2) = −9, see:
https://en.wikipedia.org/wiki/Order_of_operations#Exceptions

According to this convention you should write (-4) ^ 2.

This shows in the code where it explicitly says that the UNARY_MINUS
operator has the same precedence of the POWER operator:
https://git.gnome.org/browse/gnome-calculator/tree/lib/equation-parser.vala#n32

I agree this might be confusing but it's not wrong per se, file a bug
report on the gnome bugzilla and see what the developers think, I am
just a user. However feel free to CC me on the bug report.

This quick and dirty patch changes the behavior to what you expect, but
I don't know if there are unwanted side-effects:

diff --git a/lib/equation-parser.vala b/lib/equation-parser.vala
index b00224d..1e3e851 100644
--- a/lib/equation-parser.vala
+++ b/lib/equation-parser.vala
@@ -29,12 +29,12 @@ private enum Precedence
     FUNCTION        = 5,
     BOOLEAN         = 6,
     PERCENTAGE      = 7,
-    /* UNARY_MINUS, ROOT and POWER must have same precedence. */
-    UNARY_MINUS     = 8,
+    /* ROOT and POWER must have same precedence. */
     POWER           = 8,
     ROOT            = 8,
     FACTORIAL       = 9,
-    NUMBER_VARIABLE = 10,
+    UNARY_MINUS     = 10,
+    NUMBER_VARIABLE = 11,
     /* DEPTH should be always at the bottom. It stops node jumping off the current depth level. */
     DEPTH
 }


Ciao,
   Antonio

-- 
Antonio Ospite
http://ao2.it

A: Because it messes up the order in which people normally read text.
   See http://en.wikipedia.org/wiki/Posting_style
Q: Why is top-posting such a bad thing?



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