mIRC Home    About    Download    Register    News    Help

Print Thread
Page 2 of 4 1 2 3 4
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

Page 2 of 4 1 2 3 4

Link Copied to Clipboard