Calculator icon indicating copy to clipboard operation
Calculator copied to clipboard

Wrong calc on power?

Open nagisml opened this issue 9 years ago • 4 comments

Hi

Looks like the calculator has issues with power and brackets. 50 - (2 ^ 5) - 5 13 is correct 50 - 2 ^ 5 - 5 254803963 is wrong should be also 13

Regards

nagisml avatar Dec 14 '15 21:12 nagisml

I'm not sure this is wrong, if i paste 50 - 2 ^ 5 - 5 into c# interactive, i get 48. I paste it into google, it converts it to 50 - (2 ^ 5) - 5. I'll do some more research.

pwelter34 avatar Dec 15 '15 15:12 pwelter34

Hi

Accoring to https://en.wikipedia.org/wiki/Order_of_operations the following order has to be applied: exponents and roots multiplication and division addition and subtraction

It means that 50 - 2 ^ 5 - 5 has to be executed as 50 - (2 ^ 5) - 5. Simular as 50 - 2 * 5 - 5 is a the same as 50 - (2 * 5) - 5

Regards

nagisml avatar Dec 15 '15 17:12 nagisml

@pwelter34 This is because in C# 50 - 2 ^5 - 5 means (50 - 2) XOR (5 - 5), which is 48. Also, this should be irrelevant, as a calculator should follow rules of mathematics, not programming languages anyway. Please fix this!

hfellner avatar Nov 14 '19 11:11 hfellner

private static int Precedence(string c)
{
    if (c.Length == 1 && (c[0] == '^'))
        return 3;
 
    if (c.Length == 1 && (c[0] == '*' || c[0] == '/' || c[0] == '%'))
        return 2;
 
    return 1;
}

from https://lifeinhex.com/deobfuscating-autoit-scripts-part-2/

sredna avatar Apr 10 '22 20:04 sredna