mIRC Homepage
# $calc(-2^2) == INCORRECT

Posted By: Raccoon
## $calc(-2^2) == INCORRECT - 26/02/05 12:53 PM

Fundimental flaw in the order of operations that $calc uses.

When a negative interger is raised to a power without being enclosed in paranthasies, order of operations state that the exponent is calculated first and THEN the negative sign is applied.

$calc(-(2^2)) == -4$calc((-2)^2) == 4$calc(-2^2) == 4The order of operations in -2^2 should be equivilent to -(2^2) and NOT (-2)^2.

Enjoy!

Posted By: Erik_The_Red
## Re: $calc(-2^2) == INCORRECT - 26/02/05 05:55 PM

I see correct the three answers. -2² = 4

Posted By: Kelder
## Re: $calc(-2^2) == INCORRECT - 26/02/05 07:54 PM

$calc(0-2^2) == -4

Unary - is higher than exponent in a lot of programming languages, doesn't seem that strange that mIRC chooses to follow that...

Posted By: Erik_The_Red
## Re: $calc(-2^2) == INCORRECT - 26/02/05 08:30 PM

$calc(-2^2) = -2² = 4

$calc(-2*-2) = -2² = 4

Where it is the error?

Posted By: Danthemandoo
## Re: $calc(-2^2) == INCORRECT - 26/02/05 08:53 PM

according to my casio calculator -2² equals -4. So casio is wrong?

[Edit] above is not meant to sound sarcastic btw, its a serious question

Posted By: qwerty
## Re: $calc(-2^2) == INCORRECT - 26/02/05 09:05 PM

The order of operations does indeed state that exponent precedes subtraction. The negative sign there isn't an operation though (or at least that's how I understand it); it's part of the number, a symbol to denote that this number is negative.

What's more convincing than my speculations though is Microsoft's PowerToy Calc and Google. If you enter the expression "-2^2" in either of them, you get 4.

I searched Google for info on this but I found mixed answers, unfortunately not from reliable scientific sources.

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 26/02/05 09:27 PM

Your calculator has flat batteries maybe

Im getting 4 on both my casios (fx602p, fx82super)

What did you type?

[2] [+/-] [X²] result 4

[2] [X²] [+/-] result -4

Any negitive number squared makes a positive number and -2 is a negitive number last time i checked (which was a while ago so maybe they changed the rules of math lately?)

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 26/02/05 10:14 PM

To DaveC:

Your calculator has flat batteries maybe

Im getting 4 on both my casios (fx602p, fx82super)

If your Casio calculator is capable of multi-termed equasions, then you should have gotten

-4.

To Everyone Else:

According to my

TI-85:

According to Javascript:

- <a href="javascript:alert(-2^2);" target="_blank">javascript:alert(-2^2);</a> <- click

According to

MATLAB 6.5:

According to

Python 2.3.4:

According to "Beginning & Intermediate Algebra (3rd Ed)" by K. Martin-Gay (ISBN

0-13-144442-5)

- 1.4 Calculator Explorations, Order of Operations (p.30):

Some calculators follow the order of operations, and others do not. To see whether or not your calculator has the order of operations built in, use your calculator to find 2 + 3 · 4. To do this, press the following sequence of keys: [2] [+] [3] [x] [4] [=] (or [enter]). The correct answer will be 14 because the order of operations is to multiply before we add.

I'm guessing your Casio does not support order of operations.

5.1 Exponents (p.288):

As we reviewed in Section 1.4, an exponent is a shorthand notation for repeated factors. For example, 2 · 2 · 2 · 2 · 2 can be written as 2^5. The expression 2^5 is called an exponential expression. It is also called the fifth power of 2, or we say that 2 is raised to the fifth power. The base of an exponential expression is the repeated factor. The exponent is the number of times that the base is used as a factor.

Example:

c. (-4)² = (-4) (-4) = 16

d. -4² = -(4 · 4) = -16

Notice how similar -4² is to (-4)² in the example above. The difference between the two is the parentheses. In (-4)², the parentheses tell us that the base, or repeated factor, is -4. In -4², only 4 is the base.

Helpful Hint:

Be careful when identifying the base of an exponential expression.

Pay close attention to the use of parentheses.

In (-3)² the base is -3. (-3)² = (-3) (-3) = 9

In -3² the base is 3. -3² = -(3 · 3) = -9

In 2 · 3² the base is 3. 2 · 3² = 2 · 3 · 3 = 18

I hope this helps clear any confusion.

- Raccoon

Posted By: Danthemandoo
## Re: $calc(-2^2) == INCORRECT - 26/02/05 10:46 PM

its a fx-83WA according to the top, and i typed:

[(-)] [2] [x²] and it spat out -4

But technically -2² is -2 * -2 which equals 4, but (-2)² is 4..eerr. oh sod it i give up

i was never any good at maths anyway

Posted By: Darwin_Koala
## Re: $calc(-2^2) == INCORRECT - 26/02/05 11:15 PM

Having read this thread and seeing the various arguments for and against - here is an answer that should finalise this.

Regardless of everyone's ability or inability to operate their $5 calculator properly (that's an argument for another day!)

-2^2 , as written, is interpreted as (-2) ^ 2. The correct answer is 4.

That is not my opinion, that is fact. Clear as day, no argument. in this case (-2) is a number, just as 2 is.

Any debate on the correctness of it is not a debate of the maths, but of proponents ability to enter equations into their preferred mathematical software, or the ability of the software to interpret that equation.

Of course, if you enter (2^2) , then +/- you are going to get the answer (negative four).

And yes, 0-2^2 will be interpreted as "0 minus two squared", the correct answer will be "negative four". In this case, the "-" indicates "subtract", and the numbers in the equation are 0, 2 and 2.

However, the previous two equations are not equivalent to "-2^2".

The bottom line: there is no bug here.

Cheers, and yours in mathematica,

DK

Posted By: Darwin_Koala
## Re: $calc(-2^2) == INCORRECT - 26/02/05 11:27 PM

according to my casio calculator -2² equals -4. So casio is wrong?

[Edit] above is not meant to sound sarcastic btw, its a serious question

Try typing "2+3*4" into your calculator. The correct answer is "14", but the elCheapo calculators (the "$5 ones" in my previous response) will give an answer of "20".

To answer your question, yes, your casio can be wrong.

Having said that the Calculator can be "wrong", it is up to us as users of the tool to be aware of the limitations. In this case, we need to put ()'s in the right places to get the right answers. Of course, the stronger solution is a consumer boycott of cruddy products that don't give right answers!

BTW, some of the software solutions (I can think of a "scientific calculator" in Linux that fits in this category) are just as bad. Usually the simple "2+3*4" test will find them out.

Cheers,

DK

p.s. given that order of operations is usch an important part of maths, why is it so hard for calculator programmers to get such an easy thing right?

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 26/02/05 11:33 PM

Edited my post above.

Included picture of my TI-85.

Included output from 3 programming languages (Javascript, Python and MATLAB).

Included verbatim source from college algebra book.

Does anyone here have MAPLE they can try this on?

Would someone try debug.print -2^2 in Visual Basic aswell?

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 26/02/05 11:39 PM

Regardless of everyone's ability or inability to operate their $5 calculator properly (that's an argument for another day!)

-2^2 , as written, is interpreted as (-2) ^ 2. The correct answer is 4.

That is not my opinion, that is fact. Clear as day, no argument. in this case (-2) is a number, just as 2 is.

You are incorrect sir.

Please see me after class.

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:10 AM

-2^2 , as written, is interpreted as (-2) ^ 2. The correct answer is 4.

Since when is -2^2 interpreted as (-2)^2 ?

You realize you just stuck in a bracket into the equation..thereby explicitly changing the equation. So if -2^2 were to be interpreted as (-2)^2 then it would have been expressed as (-2)^2. Therefore it's logically impossible for -2^2 to be interpreted as (-2)^2 because then -2^2 would not exist.

You also realize that you must be explicit about your expressions in both computing and mathematics; and if you haven't realized this yet then i (a) don't know how you managed to pass any math tests past gr.6 in your life and (b) don't know how you're able to use a computer properly. You don't tell a computer to perform -2^2 and then say 'Oh but i meant (-2)^2'. According to order of operation rules that EVERY mathematician follows, exponents are second in line after brackets. -2 is 0-2. This is all stuff you learn in basic math courses in the early high school years.

That being said, -2^2 MEANS 0 - 2^2. If you don't agree, then YOU're wrong.

Posted By: qFox
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:11 AM

Prelude> -2^2

(-4)

This be Haskell

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:14 AM

(19:11:12) <Sais> perl -wle 'print -2**2;'
(19:11:13) <Sais> -4

I wonder how much more proof is needed. I was sold on the theory alone, personally *shrugs*

Posted By: qFox
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:18 AM

prolog:

test(X) :- X is -2^2.

1 ?- test(X).

X = 4

but i would not put my faith in prolog's parser, or math capabilities for that matter.

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:20 AM

Sub Main()
Console.Out.WriteLine(-2 ^ 2)
End Sub

-4.

"if MS follows a standard..then it has to be worthy of following"

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:24 AM

its a fx-83WA :

[(-)] [2] [x²] and it spat out -4

I dont have a (-) button, and if i used the +/i button when its zero it just stays zero, want negate the number.

what do you get if you go [(-)] [2] [x] [x] [=] ?

Sounds like a accountants calculator, no matter what you put in the result well be what he wants out.

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:29 AM

Unary - is higher than exponent in a lot of programming languages,

So far i haven't seen one language that follows this rule. Maybe someone can try FORTRAN/ Pascal ? ...Java?

unless by 'lots of programming languages' you mean one or two obscure languages, then i have to disagree

Posted By: Darwin_Koala
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:30 AM

Well, doesn't this just go to show that ()'s should always be used to resolve any ambiguities! Just when you thought you had all the facts lined up - something comes along to knock you for a six!

I have had the chance to look at your edited post - nice picture! Unfortunately, my texts (yes, you made me hit the text books) did not have any examples or reference to such confusion!

I would always have picked (-2) as being as much a number as (2) , hence the interpretation of the equation. In this case the "-" is negation, not minus, and I can't find my reference to BEDMAS at the moment which includes such things.

Cheers,

DK

Posted By: Darwin_Koala
## Re: $calc(-2^2) == INCORRECT - 27/02/05 12:41 AM

Since when is -2^2 interpreted as (-2)^2 ?

You realize you just stuck in a bracket into the equation..thereby explicitly changing the equation. So if -2^2 were to be interpreted as (-2)^2 then it would have been expressed as (-2)^2. Therefore it's logically impossible for -2^2 to be interpreted as (-2)^2 because then -2^2 would not exist.

i (a) don't know how you managed to pass any math tests past gr.6 in your life and (b) don't know how you're able to use a computer properly.

-2 is 0-2. This is all stuff you learn in basic math courses in the early high school years.

That being said, -2^2 MEANS 0 - 2^2. If you don't agree, then YOU're wrong.

*ouchies* Obviously I am a numbskull who is highly illiterate, I have never touched a computer in my life and I know nothing about mathematics. I bow to your greatly superior knowledge.

More seriously - OK - my memories of Maths from a long time ago have now been tested.

sure? Perhaps it means (-1) * (2^2) ? Is which case, I "disagree" with you. Is there a difference? Once you start transformations there is. Once is a translation, the other a scaling. Am I wrong?

An interesting argument, but without any further references (at hand) to back my argument up, I will have to side with Raccoons explanation. Thus, (until someone can find me a reference that unary negation has a higher precedence than exponential) I will concede, agree, concord, that this is a bug.

Cheers,

DK

Posted By: qwerty
## Re: $calc(-2^2) == INCORRECT - 27/02/05 01:12 AM

Provided MS follows a standard at all. Out of 4 MS products I tested so far, VB and JavaScript (in WSH) output -4, while VBScript and PowerToy Calc output 4.

It seems the majority leans to -4 and they are possibly right, but the fact that quite a few "big" software products give 4 tells me that this rule is far from being rigid or obvious.

To prevent misunderstandings, I'm not referring to the rule that exponent precedes subtraction. I'm talking about the role of the minus sign here, ie whether -2 should be treated as 0-2 (subtraction operation) or the negative number -2 as a single entity.

Posted By: tidy_trax
## Re: $calc(-2^2) == INCORRECT - 27/02/05 01:26 AM

Would someone try debug.print -2^2 in Visual Basic aswell?

Sub main()
Debug.Print -2 ^ 2
End Sub

-4

Posted By: Darwin_Koala
## Re: $calc(-2^2) == INCORRECT - 27/02/05 01:36 AM

As a reply in general (sorry tidy, just clicked on yours), the fact that various computing languages provide a particular answer, does not prove that it is correct. All it proves is that is how the creator/compile interpreted it. And that interpretation may or may not be correct - something that not many of here appear to be sure of.

Cheers,

DK

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 27/02/05 01:52 AM

I was going to say its not any type of operator, its just a visual representing of a negitive value, but infact i think mirc consideres - as a negation op higher than exponent.

Operator order of precedence should be (simplisticly)

^ : Exponentiation

- : Unary negation (unary minus)

*/ : times division

+- : add sub

I tried this...

//var %x = 2 | echo -a $calc(- %x ^ 2) | echo -a $calc(- %x * %x)

4

<incorrect>-4

<correct>In both i beleive the first %x is being negated then applied to the ^ or * operators

The first should have been %x^2 and then -, the second just comes out correctly by its nature.

While doing this i noticed something odd using the negating -

//echo -a 0-'s $calc( 1 ) | echo -a 1-'s $calc( -1 ) | echo -a 2-'s $calc( --1 ) | echo 3-'s $calc( ---1 ) | echo 4-'s $calc( ----1 ) | echo 5-'s $calc( -----1 )

0-'s 1

<correct>1-'s -1

<correct>2-'s -1

<incorrect>3-'s 1

<correct based on results on negation of 2-'s result onward>4-'s -1

<correct based on results on negation of 2-'s result onward>5-'s 1

<correct based on results on negation of 2-'s result onward>2 negates act as 1 and then extras work as there ment to negating the previous.

Oh PS: My 10 year old daughter just resolved all this by telling me that I and everyone else in this thread has no life.

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 27/02/05 01:56 AM

As a reply in general (sorry tidy, just clicked on yours), the fact that various computing languages provide a particular answer, does not prove that it is correct. All it proves is that is how the creator/compile interpreted it. And that interpretation may or may not be correct - something that not many of here appear to be sure of.

Just to let you know, Ill interpret that above how I like and I may take it as correct or incorrect or a possable result of NAN.

Posted By: tidy_trax
## Re: $calc(-2^2) == INCORRECT - 27/02/05 02:28 AM

I tried a few more as well:

VB:

Sub main()
Debug.Print -2 ^ 2
End Sub

-4

-----

Python:

-4

-----

C++ (MingW compiler):

-4

-----

C++ (Visual C++ compiler)

-4

-----

Javascript (Tried in internet explorer and firefox - although I don't think the browser matters)

-4

-----

Jscript

-4

-----

VBScript

4

-----

Google

4

-----

Calc.exe

-4

-----

PHP

4

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 02:28 AM

Posted By: Danthemandoo
## Re: $calc(-2^2) == INCORRECT - 27/02/05 02:38 AM

Try typing "2+3*4" into your calculator. The correct answer is "14", but the elCheapo calculators (the "$5 ones" in my previous response) will give an answer of "20".

Mine gave the answer 14

this is my calculator btw:

or click

here for a really blury, badly shot picture of the actual thing, edited for clarity, merged together and shot with a webcam i got free when i ordered my PSU

what do you get if you go [(-)] [2] [x] [x] [=] ?

If you mean [x] as in the multiply button, i get a syn error

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 27/02/05 03:23 AM

what do you get if you go [(-)] [2] [x] [x] [=] ?

If you mean [x] as in the multiply button, i get a syn error

wow really?!?!?!

normally if you do something like [10] [x] [x] (at this time a small 'k' should appear on screen) [=] 100 results, then do [1][2][4][=] 1240,

esentially it lets you type in a number and by just pressing [=] have the previously set operator and value take effect

So like you might have boxes that fit 13 along x 14 across x 7 high so you have 13x14x7= 1274 then press [x] [x] now type in how many boxes bob has [3][=] 3822, now Jim has [7][=] 8918, etc, works with x/+-

I thought all calculators had it.

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 27/02/05 04:29 AM

Python correction.

-2**2 is the proper syntex in Python for exponents.

-2^2 is coincidental. Both equal -4 in any event.

These are all very interesting statistics. I'm calling my friend at Google about it now to tell them Google is broken.

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 27/02/05 10:49 AM

Well, I've decided to let the masters figure this one out. I've posted the question to Google Answers. Twice, infact.

http://answers.google.com/answers/threadview?id=481638http://answers.google.com/answers/threadview?id=481636We shall see what they think of this, and then I'll play devil's advocate for a while to see what their thoughts are.

I'd like to thank everyone who posted their opinions and findings in this thread. I really don't think there's a "right or wrong" here. They're both appearing to be standard conventions which were never firmly decided upon. Nobody wrote out an RFC on Mathematical Expressions, so both adaptations can be found in use.

I look forward to seeing how Googleians respond and argue over this one.

Posted By: liltmasp
## Re: $calc(-2^2) == INCORRECT - 27/02/05 11:47 AM

Ah, dunno about that one. What version is your PHP interpreter?

PHP 5.0.3:

[kdawg@blizzard(~)]$ php -v

PHP 5.0.3 (cli) (built: Feb 24 2005 12:07:42)

Copyright (c) 1997-2004 The PHP Group

Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies

[kdawg@blizzard(~)]$ echo '<? print -2^2; print "\n"; ?>' | php

-4

Might as well throw Tcl into the mix as well:

[kdawg@blizzard(~)]$ echo "echo [expr -2^2]" | tcl

-4

[kdawg@blizzard(~)]$

Posted By: NeoN
## Re: $calc(-2^2) == INCORRECT - 27/02/05 06:29 PM

-2^2 should indeed be interpreted as -(2^2) = -4

For example, everyone agrees that: 2^2 = 4

If you multiply both sides of the equation by -1, you get: -2^2 = -4

You could argue that it should be written as: - (2^2) = -4

But if you look at polynomials:

5x is a shorthand for 5*x

and -5x for (-5)*x,

so -x is a shorthand for (-1)*x

5x^2 is a shorthand for 5*(x^2),

-5x^2 for (-5)*(x^2)

Then why would -x^2 be anything else than (-1)*(x^2)?

If you don't want to accept the same rules as the ones for polynomials, consider this:

* the order of terms doesn't matter for an addition:

for example:

2 + 3 = 3 + 2 = 5

2 + 2^2 = 2^2 + 2 = 6

* subtraction is just addition of the opposite:

a - b = a + (-b)

for example: 5 - 3 = 5 + (-3) = 2

So, a - b = a + (-b) = -b + a

for example: 5 - 3 = -3 + 5

So 2 - 2^2 = -2^2 + 2

And 2 - 2^2 = 2 - 4 = -2, which means -2^2 + 2 = -2 and -2^2 = -4

Another example,

to solve equations like ax^2 + bx + c = 0, there is this method:

D = b^2 - 4ac

solutions: (-b + sqrt(D)) / (2a) and (-b - sqrt(D)) / (2a)

-x^2 + x + 2 = 0

D = 1^2 - 4*(-1)*2 = 1 + 8 = 9

solutions: (-1 + 3) / (-2) = -1 and (-1 - 3) / (-2) = 2

To check your solutions, you can substitute them in the equation:

-(-1)^2 + (-1) + 2 = 0 ? ... only if -(-1)^2 = -1

-2^2 + 2 + 2 = 0 ? ... only if -2^2 = -4

I think this shows the convention of the unary minus versus powers.

Posted By: tidy_trax
## Re: $calc(-2^2) == INCORRECT - 27/02/05 06:34 PM

Since I don't have a PHP enabled host I asked someone to test it for me, he accidently evaluated (-2)^2 which gives 4, -2^2 gives -4.

Posted By: Hrung
## Re: $calc(-2^2) == INCORRECT - 27/02/05 08:23 PM

Look at it this way:

set %temp $calc(0 - 2)

set %temp2 $calc(%temp ^ 2)

would you be happy having that come up as -4? I would not.

Posted By: qwerty
## Re: $calc(-2^2) == INCORRECT - 27/02/05 09:08 PM

It's good you brought this up. Regardless of what is correct or not in math expressions in general, one should take into account the way mirc's parser works. For example, mirc allows you to do stuff like

//var %a = ^2 | echo -a $calc(5 %a)

which prints "25"

This probably means that first variables/identifiers are evaluated and then the resulting expression is passed to the math function that performs the calculations. So, if this 'bug' is fixed, we'd be forced to write ugly and boring extra code to avoid mishaps like this:

//var %a = $r(1,9) - 5 | echo -a $calc(%a ^ 2)

You would expect this to always print a positive number: whatever value %a has (negative or positive), it's squared, so the result is always positive. However, if the bug is fixed and %a happens to be -4, $calc() will output a negative number because "%a ^ 2" becomes "-4 ^ 2", which would now give -16. I'm guessing that few scripters would not be surprised by a negative result from the above. Of course, you can get around this by wrapping () around every single variable, but that's the point where it gets ugly and boring.

Considering this, I'm rather against fixing $calc(), even if it's technically incorrect.

Posted By: argv0
## Re: $calc(-2^2) == INCORRECT - 27/02/05 09:56 PM

Look at it this way:

set %temp $calc(0 - 2)

set %temp2 $calc(%temp ^ 2)

would you be happy having that come up as -4? I would not.

Owned, racc, owned!

outfoxed, rather.

Posted By: Kelder
## Re: $calc(-2^2) == INCORRECT - 27/02/05 10:05 PM

std::cout << -2*2;

document.write(-2^2);

WScript.Echo(-2^2);

The first one is kinda cheating, -2*2 wasn't being discussed

And for all 3 (C,C++,Jscript, Javascript, Java) -2^2 is something entirely different, so it's not useful either. Apparently there's no power operator in those languages, and a Math.pow(x) function makes you choose anyway...

For the interested, ^ is the bitwise XOR function in those languages, has lower precedence than lots of stuff including * (multiplication) / (division) + (binary addition) - (binary subtraction). Incidentally, it gives the correct (or wrong?) result due to the nature of the 2-complement integer representation in computers.

Perl is kinda strange imo, it says unary - has higher precedence than **, but it still gives -4 as answer, while *, % and other operators (below unary - and below **) do function as expected by the precedence.

Let's keep it at: use brackets

As a reference:

C operator precedenceJava operator precedence(google has the others)

Two's complementAs for my first reply, apparently I was wrong, I did learn some new stuff though

Posted By: tidy_trax
## Re: $calc(-2^2) == INCORRECT - 27/02/05 10:08 PM

Yeah, I cheated in C++ because pow() doesn't accept negative numbers :tongue:

Posted By: Hrung
## Re: $calc(-2^2) == INCORRECT - 28/02/05 10:04 AM

I've given this a lot of thought, and I came up with what I hope is an informed opinion. The question here is this: in the equation -2^2, is the - associated with just the 2 that it is next to, or is it associated with the entire 2^2? That is, would -2^2 be (-2)^2, or -(2^2). The answer, according to the correct order of operations, is -(2^2), or -4.

However, in:

var %negativetwo = -2

echo -a $calc(%negativetwo ^2)

the - is

clearly and unambiguously associated with the 2 only, and so the echo should give 4.

Now it comes down to whether or not mIRC's parser will allow $calc to know that the -2 in there came from a variable, and treat it as such (perhaps by, at least internally, enclosing it in parenthesis/brackets.)

Here is the part of the show where I list pros and cons for "fixing" this:

Pros:

a) It would be technically correct (the only pro I can think of)

Cons:

a) It will likely break at least some scripts out there.

b) fixing it

must not come at the expense of breaking legitimate code, such as the example with %variables. I don't have an intimate knowlege of mIRC's script parser, so I don't know how easy that would be, or even if it could be done without extensive modification.

In conclusion, if I were to be the one to make this decision, I would decide based upon how much work would have to be put into it. I see little sense in working long hours to fix something that nobody noticed until shortly before the product's 10th anniversary (happy b-day, mIRC!!) if someone else is going to complain that now their script doesn't work. Fortunately, the burden isn't on me, but it is Khaled that must choose.

In other news, I hear that Microsoft Excel

agrees with mirc that -2^2 = 4.

Posted By: praetorian_
## Re: $calc(-2^2) == INCORRECT - 28/02/05 08:14 PM

Just to add to the discussion:

C#:

System.Diagnostics.Debug.WriteLine(-2^2);

-4

Java:

System.out.println(-2^2);

-4

VB .NET:

Debug.WriteLine(-2 ^ 2)

-4

PHP (5.0.3):

echo -2^2;

-4

Excel indeed returns "4" when -2^2 is entered as a function.

Posted By: Armada
## Re: $calc(-2^2) == INCORRECT - 01/03/05 04:30 AM

To add a bit more, my fiance is a college student going to be a teacher where her focus/specialty is math so I went and asked her since this is all fresh in her mind. She looked in her fancy teacher books and it is postive 4 just mine and her 2 cents

Edit:

(-2)^2 is how it is supposed to look when you go to eval it

Posted By: bamaboy1217
## Re: $calc(-2^2) == INCORRECT - 01/03/05 11:54 PM

i have a ti89 calculate and if you do

-2^2 it will say -4 so it always takes it as -(2)^2 so it sees the negative as out in front, same happens on 83's HOWEVER as always if you put it as (-2)^2 it will be 4

As far as i know that is how it should behave w/o () around the -2

or the -2 being like var %two = -2 i would say that it should auto see -2 as (-2) thats what the ti's do at least

-2->a

a^2 = 4

My 2 cents as well.

Posted By: Zmodem
## Re: $calc(-2^2) == INCORRECT - 07/03/05 08:46 PM

No matter how you look at it, there's going to be HUGE opinion regarding this. You're always taught that multiplication supercedes addition/subtraction. But, in this case, it looks as though you have a problem. The order of operations states that it's left to right unless a supercede interrupts the process, thus eliminating rules until the operation takes place, putting order back to left to right. In this case, we have -2^2, but with it as is, and everyone not wanting to interpret it in any fancy order: (-2)^2, (0 - 2)^2, 0 - 2 ^ 2, or whatever else you can think of, it's simply a matter of everyone's opinion, including mine. If you were to ask me, I would have to explain as follows:

-2^2 - A number that is already labeled negative must stay there, because if we go on to changing things the way we want (0 - 2), in my opinion, we screw up the whole mathematical system. The equations must be interpretted as they are written. So, in this case, have negative 2 squared 2 times, -2^2. So, following the order of operations' alloted cases where multiplication supercedes any other form of calculation, you must identify that this equation is multiplying a negative number twice, which allows you to end up with a positive. I'm stating this as OPINION because that's what it is. I could be wrong, or I could be right, it's all opinion. You can't say, well this is (-2)^2, because that is NOT how this operation is written. If there is a rule that will allow you to write just that, then I think that is the missing link that we are searching for. If you slice it, you see (-2)^2, -(2)^2. Either way, your negative attachment is superceding the multiplication because it is a multiplication in itself. You are negotiating the point of whether the number is positive or negative, which is noted as an operation of importance to that of multiplication/division, so this MUST be concluded before any multiplication is done. Why? Order of operations tells us that it is to be performed left to right, unless an operation of higher law/imporance is apparent before that calculation can take place, in which case those operations are performed beforehand, left to right of course. Once those operations have been done, you can continue with normal left->right operation. In this case, however, we have 2 cases where left->right has the same rule of operation as each other.

Anyhow, I'm open to other suggestions that heed incorrect on this. Sorry for the long post ;-)

Posted By: starbucks_mafia
## Re: $calc(-2^2) == INCORRECT - 07/03/05 10:22 PM

Do you even understand what you wrote there?

The case of whether it should be -4 or 4 is solely down to whether you take the minus symbol to be the unary negative operator or a component of the number 2. Seeing that plain string in any programming language or on paper I would take the answer to be -4, and if it were down to that then I'd say anyone disagreeing with it is wrong outright. The only place where it's not so clear is the fact that mIRC has no numeric type and so, as several people have pointed out, a variable with the value -2 put into $calc() will behave functionally equivalent as the plain string whereas it would in 99% of cases be meant as a representation of a negative number.

I think mIRC's $calc() should be a little more advanced in it's parsing and treat input from variables as if surrounded by parentheses. Scripters who wanted to override this behaviour could always use evaluation brackets.

Posted By: Zmodem
## Re: $calc(-2^2) == INCORRECT - 08/03/05 08:11 AM

Agreed completely, as for understanding, of course I do. The only problem here seems to be that I couldn't stop typing, for whatever reason. Go figure...

Posted By: vague
## Re: $calc(-2^2) == INCORRECT - 08/03/05 02:42 PM

You my friend are so encouraging:-) Mentioning Haskell and Prolog in the same thread, kudos!=)

Posted By: alhammer
## Re: $calc(-2^2) == INCORRECT - 11/03/05 10:38 PM

I am confused, considering I AM in grade school :tongue:, however, i'll still throw in my own two cents: -2^2 = (-2)(-2) When multiplying two numbers with the same sign, the product will be positive. So, (-2)(-2) = 4. For all I know, i'm completely wrong. :tongue:

Posted By: cysleeck
## Re: $calc(-2^2) == INCORRECT - 11/03/05 11:51 PM

I am confused, considering I AM in grade school :tongue:, however, i'll still throw in my own two cents: -2^2 = (-2)(-2) When multiplying two numbers with the same sign, the product will be positive. So, (-2)(-2) = 4. For all I know, i'm completely wrong. :tongue:

it was said before:

(-2)^2 = (-2)*(-2)= 4

but

-2^2 = -1*(2^2) = -1*4 = -4

how simple

Posted By: zack
## Re: $calc(-2^2) == INCORRECT - 12/03/05 11:00 PM

My LG mobile phone says -2^2 is 4. Life's Good with LG.

Posted By: DaveC
## Re: $calc(-2^2) == INCORRECT - 13/03/05 01:09 AM

OK OK OK I HAVE HAD ENOUGH

I have checked and the official answer is -2^2 is exactly 0 (+/-4)

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 16/05/05 10:28 AM

HAHAHA. I love that answer Dave. Glad I revisited this thread.

It seems this equation is really a loaded gun in academia, bringing about a fervent debate in any field of science. You'll get completely different answers from a math professor than you would a physics or computer science professor. Put them in the same room and watch them go. It's hours of fun!

The key to remember is that physics and computer science professors haven't touched algebra since they were 13, and probably don't remember this rule anyway. While they may be correct in how their mind wraps around the mathimatical problem, they are incorrect in how the written expression is officially translated from ink into problem. Unlike the English language which relies heavily on context, mathimatical syntax has to be black and white, even if it means creating obscure rules that make -2^2 = -4.

Posted By: Riamus2
## Re: $calc(-2^2) == INCORRECT - 16/05/05 01:07 PM

Really, it's just a matter of how a program looks at the expression. If you have a negative sign (not a minus sign), then it automatically treats it as (-2). If you use a minus sign, then it treats it as -2. When you square it, you have -(2^2) with a minus sign and (-2)^2 for the negative sign. Both are correct like DaveC said. This is why you should use ()'s with anything ambiguous. For mIRC, use ()'s and it will work just fine.

Posted By: Hrung
## Re: $calc(-2^2) == INCORRECT - 17/05/05 12:55 AM

Just nit-picking, but ()'s cannot remove ambiguity from such an equation, because there is no ambiguity to begin with. The order of operations clearly states how "-2^2" (or any other equation for that matter) should be interpretted. Math is a form of logic, and logic can't say anything for certain if there is any ambiguity in one of the steps. ()'s are a part of the language of math and, properly used, can only change the meaning of the equation, not clarify it. "Properly used" here means not just thrown in for fun. In fact, extra ()'s can make an equation more difficult, or at least more time consuming, for a person to sort out. Simple example: (((1) + 1) + 1) = 1 + 1 + 1. None of the ()'s make any difference to the outcome, they just make it take slightly longer to read. Again, I'm only having fun splitting hairs here. My opinion on the problem itself was already summed up in an earlier post.

Posted By: Riamus2
## Re: $calc(-2^2) == INCORRECT - 17/05/05 01:56 AM

Parentheses are used to change the order of operation. In this case, it is necessary to let the program know you want the order of operation to be different from that which it uses as a default. Yes, I know you already know that. I'm just clarifying as you seem to have done with previous statement. That is why I said that in cases where a mathematical operation is "ambiguous" (by this if you read the context, I'm talking about where order of operations is in question), it can and will prevent issues. It is true that you don't have to add them all over the place, but they are needed at certain times.

In this case, the "correct" order of operation without parentheses would be 2^2 * -1 = -4. A minus operation whether by negative sign or by an actual minus is done after powers. In fact, multiplication and division (both of which are done before any minus/negative operation) is done after exponents.

2*2^3 = 16 if you don't use parentheses

If you wanted 4^3, you have to use parentheses just has you would with -2^2 (aka -1*2^2).

Posted By: Raccoon
## Re: $calc(-2^2) == INCORRECT - 17/05/05 05:31 PM

Dude, you guys still don't get it.

You are forbidden from adding paranthasis to the expression, because doing so would change the expression.

-2^2 is perfectly valid and un-ambiguous on its own. Nothing else but -2^2 is the same written expression as -2^2. Modifying the expression changes the expression and invalidates your arguement, because you'd be talking about a whole new expression entirely.

Now presuming that modifying the expression with paranthesis is forbidden, and acknowledging that -2^2 is a valid expression in its own... Know that -2^2 is equal to -4 and nothing else.

No IFs, ANDs or BUTs. Final. It is mathmatical LAW.

Posted By: Riamus2
## Re: $calc(-2^2) == INCORRECT - 17/05/05 06:09 PM

Exactly.

My comments regarding parentheses was to show what must be done to get -2^2 to be equal to +4. Like in my last post, exponents are done before multiplication/division/addition/subtraction if you have no parentheses to change the order of operation.

Posted By: Kelder
## Re: $calc(-2^2) == INCORRECT - 17/05/05 08:10 PM

It's not a mathematics forum here, it's a mIRC scripting forum. This means that the parsing of an expression is done by mIRC and that parsing is unambiguous since it always returns 4 for -2^2.

(ps: oh no, not again)

Posted By: Riamus2
## Re: $calc(-2^2) == INCORRECT - 17/05/05 08:14 PM

Lol. Though, mIRC should probably be correct in how it handles orders of operation. Still, it isn't hard to correct if you really need it.

Posted By: Hrung
## Re: $calc(-2^2) == INCORRECT - 17/05/05 08:23 PM

The point is just that it is unambiguously wrong.

Posted By: C45P3R
## Re: $calc(-2^2) == INCORRECT - 22/05/05 04:53 AM

If you will notice you have -2-2 not -2^2 = -4 on your calculator Raccoon.

the second 0-2^2 becomes 0-(2^2) because the minus sign is an operator.

And in -(2^2) well the - is applied after the exponent is worked out.

but in the case of just -2^2 it is not a minus operator but infact signifying it as a negitive number. the +/- button makes it -2^2 become (-2)^2 invisibly because minus was not used but a negitive to the base number.

But in the end yes in mIRC and programming that don't have +/- to signifiy the number/base as negitive it is in fact a minus so -2^2 = -4, But calcaulators -2(negative two) is the same as (-2) when programming.

Calculator:

[+/-] [2] [^] [2] [=] 4

[-] [ 2] [^] [2] [=] -4

Programs:

-2^2=-4

Simple no?