mIRC Home    About    Download    Register    News    Help

Print Thread
Page 1 of 4 1 2 3 4
#112756 26/02/05 12:53 PM
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
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) == 4

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

Enjoy! smile


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Sep 2004
Posts: 18
E
Pikka bird
Offline
Pikka bird
E
Joined: Sep 2004
Posts: 18
I see correct the three answers. -2² = 4

Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
$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...

Joined: Sep 2004
Posts: 18
E
Pikka bird
Offline
Pikka bird
E
Joined: Sep 2004
Posts: 18
$calc(-2^2) = -2² = 4
$calc(-2*-2) = -2² = 4

Where it is the error?

Joined: Mar 2004
Posts: 457
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Mar 2004
Posts: 457
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

Last edited by Danthemandoo; 26/02/05 09:00 PM.
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Your calculator has flat batteries maybe smile
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?)

Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
To DaveC:
Quote:
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:
  • >> -2^2

    ans =

    -4
According to Python 2.3.4:
  • >>> -2^2
    -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. smile

- Raccoon

Last edited by Raccoon; 27/02/05 12:02 AM.
Joined: Mar 2004
Posts: 457
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Mar 2004
Posts: 457
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 confused i was never any good at maths anyway crazy

Last edited by Danthemandoo; 26/02/05 10:48 PM.
Joined: Feb 2004
Posts: 206
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Feb 2004
Posts: 206
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


Darwin_Koala

Junior Brat, In-no-cent(r)(tm) and original source of DK-itis!
Joined: Feb 2004
Posts: 206
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Feb 2004
Posts: 206
Quote:
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?


Darwin_Koala

Junior Brat, In-no-cent(r)(tm) and original source of DK-itis!
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
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?


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
Quote:
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.


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Quote:

-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.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Oct 2003
Posts: 14
Q
Pikka bird
Offline
Pikka bird
Q
Joined: Oct 2003
Posts: 14
Prelude> -2^2
(-4)


This be Haskell smile

Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Code:
(19:11:12) &lt;Sais&gt; perl -wle 'print -2**2;'
(19:11:13) &lt;Sais&gt; -4


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


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Oct 2003
Posts: 14
Q
Pikka bird
Offline
Pikka bird
Q
Joined: Oct 2003
Posts: 14
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.

Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Code:
Sub Main()
   Console.Out.WriteLine(-2 ^ 2)
End Sub


-4.

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


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
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.

Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Quote:
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


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Feb 2004
Posts: 206
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Feb 2004
Posts: 206
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


Darwin_Koala

Junior Brat, In-no-cent(r)(tm) and original source of DK-itis!
Joined: Feb 2004
Posts: 206
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Feb 2004
Posts: 206
Quote:

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.

Quote:
-2^2 MEANS 0 - 2^2


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


Darwin_Koala

Junior Brat, In-no-cent(r)(tm) and original source of DK-itis!
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.

Last edited by qwerty; 27/02/05 01:24 AM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
Quote:
Would someone try debug.print -2^2 in Visual Basic aswell?


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


-4


New username: hixxy
Joined: Feb 2004
Posts: 206
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Feb 2004
Posts: 206
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


Darwin_Koala

Junior Brat, In-no-cent(r)(tm) and original source of DK-itis!
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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. frown

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
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.

Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
I tried a few more as well:

VB:

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


-4

-----

Python:

Code:
-2^2


-4

-----

C++ (MingW compiler):

Code:
std::cout &lt;&lt; -2*2;


-4

-----

C++ (Visual C++ compiler)

Code:
std::cout &lt;&lt; -2*2;


-4

-----

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

Code:
document.write(-2^2);


-4

-----

Jscript

Code:
WScript.Echo(-2^2);


-4

-----

VBScript

Code:
MsgBox -2^2


4

-----

Google

Code:
-2^2


4

-----

Calc.exe

Code:
-2^2


-4

-----

PHP

Code:
echo -2^2;


4


New username: hixxy
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Mar 2004
Posts: 457
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Mar 2004
Posts: 457
Quote:
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 shocked

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 crazy
Quote:
what do you get if you go [(-)] [2] [x] [x] [=] ?

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

Last edited by Danthemandoo; 27/02/05 03:00 AM.
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
Quote:
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.

Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
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. wink


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
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=481638
http://answers.google.com/answers/threadview?id=481636

We 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. smile


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Feb 2005
Posts: 1
L
Mostly harmless
Offline
Mostly harmless
L
Joined: Feb 2005
Posts: 1
Quote:

PHP

Code:

echo -2^2;



4


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(~)]$

Joined: Nov 2003
Posts: 25
N
Ameglian cow
Offline
Ameglian cow
N
Joined: Nov 2003
Posts: 25
-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.

Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
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. smile


New username: hixxy
Joined: Dec 2002
Posts: 208
H
Fjord artisan
Offline
Fjord artisan
H
Joined: Dec 2002
Posts: 208
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.


If I knew now what I will know then... maybe things will have been different...
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Quote:
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.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Quote:
std::cout << -2*2;
document.write(-2^2);
WScript.Echo(-2^2);


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

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 crazy

As a reference:
C operator precedence
Java operator precedence
(google has the others)
Two's complement


As for my first reply, apparently I was wrong, I did learn some new stuff though smile

Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
Yeah, I cheated in C++ because pow() doesn't accept negative numbers :tongue:


New username: hixxy
Joined: Dec 2002
Posts: 208
H
Fjord artisan
Offline
Fjord artisan
H
Joined: Dec 2002
Posts: 208
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.

Last edited by Hrung; 28/02/05 10:08 AM.
Joined: Feb 2003
Posts: 32
P
Ameglian cow
Offline
Ameglian cow
P
Joined: Feb 2003
Posts: 32
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.

Joined: Mar 2004
Posts: 540
A
Fjord artisan
Offline
Fjord artisan
A
Joined: Mar 2004
Posts: 540
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

Last edited by Armada; 01/03/05 04:32 AM.
Joined: Jul 2003
Posts: 77
B
Babel fish
Offline
Babel fish
B
Joined: Jul 2003
Posts: 77
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 smirk 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.


hmmm signed by me
Joined: Dec 2002
Posts: 87
Babel fish
Offline
Babel fish
Joined: Dec 2002
Posts: 87
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 ;-)


-Zmodem
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
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.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Dec 2002
Posts: 87
Babel fish
Offline
Babel fish
Joined: Dec 2002
Posts: 87
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...


-Zmodem
Joined: Dec 2002
Posts: 39
V
Ameglian cow
Offline
Ameglian cow
V
Joined: Dec 2002
Posts: 39
You my friend are so encouraging:-) Mentioning Haskell and Prolog in the same thread, kudos!=)

Joined: Feb 2005
Posts: 194
A
Vogon poet
Offline
Vogon poet
A
Joined: Feb 2005
Posts: 194
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:


"God sometimes puts us in the dark for us to see the light"
Joined: Nov 2004
Posts: 16
C
Pikka bird
Offline
Pikka bird
C
Joined: Nov 2004
Posts: 16
Quote:
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

Joined: Dec 2002
Posts: 266
Z
Fjord artisan
Offline
Fjord artisan
Z
Joined: Dec 2002
Posts: 266
My LG mobile phone says -2^2 is 4. Life's Good with LG.


You won't like it when I get angry.
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
OK OK OK I HAVE HAD ENOUGH

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

Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
HAHAHA. I love that answer Dave. Glad I revisited this thread. smile

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.


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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.


Invision Support
#Invision on irc.irchighway.net
Joined: Dec 2002
Posts: 208
H
Fjord artisan
Offline
Fjord artisan
H
Joined: Dec 2002
Posts: 208
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.


If I knew now what I will know then... maybe things will have been different...
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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).


Invision Support
#Invision on irc.irchighway.net
Joined: Feb 2003
Posts: 2,812
Raccoon Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
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.


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Exactly. smile

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.


Invision Support
#Invision on irc.irchighway.net
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
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)

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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. smile


Invision Support
#Invision on irc.irchighway.net
Joined: Dec 2002
Posts: 208
H
Fjord artisan
Offline
Fjord artisan
H
Joined: Dec 2002
Posts: 208
The point is just that it is unambiguously wrong.


If I knew now what I will know then... maybe things will have been different...
Joined: Jan 2005
Posts: 20
C
Ameglian cow
Offline
Ameglian cow
C
Joined: Jan 2005
Posts: 20
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?

Page 1 of 4 1 2 3 4

Link Copied to Clipboard