<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx</link><description>Raymond has written about this , I have written about Raymond writing about it , but I still frequently get questions from people who are unclear on the difference between precedence, associativity and evaluation order. I suspect that this confusion arises</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8539694</link><pubDate>Fri, 23 May 2008 18:07:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8539694</guid><dc:creator>Steve</dc:creator><description>&lt;p&gt;a = &amp;quot;Test&amp;quot;;&lt;/p&gt;
&lt;p&gt;b = 3;&lt;/p&gt;
&lt;p&gt;c = 6;&lt;/p&gt;
&lt;p&gt;(a + b) + c == &amp;quot;Test36&amp;quot;&lt;/p&gt;
&lt;p&gt;a + (b + c) == &amp;quot;Test9&amp;quot;&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8539764</link><pubDate>Fri, 23 May 2008 18:21:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8539764</guid><dc:creator>Trevel</dc:creator><description>&lt;p&gt;Not sure the exact value of x necessary for this in c#, but I'ma guess... &lt;/p&gt;
&lt;p&gt;a = 10 ^ x&lt;/p&gt;
&lt;p&gt;b = -a&lt;/p&gt;
&lt;p&gt;c = 5&lt;/p&gt;
&lt;p&gt;(a+b) + c == 5&lt;/p&gt;
&lt;p&gt;a + (b+c) == 0&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8539778</link><pubDate>Fri, 23 May 2008 18:24:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8539778</guid><dc:creator>Rising</dc:creator><description>&lt;p&gt;Double a = 1;&lt;/p&gt;
&lt;p&gt;Double b = -1;&lt;/p&gt;
&lt;p&gt;Double c = Double.Epsilon;&lt;/p&gt;
&lt;p&gt;(a + b) + c == Double.Epsilon&lt;/p&gt;
&lt;p&gt;a + (b + c) == 0&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8540254</link><pubDate>Fri, 23 May 2008 20:03:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8540254</guid><dc:creator>oldnewthing</dc:creator><description>&lt;p&gt;Associativity is significant even when computing by hand: (3-2)-1 is different from 3-(2-1).&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8541015</link><pubDate>Fri, 23 May 2008 22:21:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8541015</guid><dc:creator>theman</dc:creator><description>&lt;p&gt;substraction is a non-associative operation as is division that's why the above example does not evaluate to the same result&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8541154</link><pubDate>Fri, 23 May 2008 22:45:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8541154</guid><dc:creator>Sean Reilly</dc:creator><description>&lt;p&gt;a = -1, b= 1, c=Int32.MaxValue&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8550599</link><pubDate>Sun, 25 May 2008 08:14:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8550599</guid><dc:creator>mike</dc:creator><description>&lt;p&gt;&amp;gt;This topic usually comes up is when someone has an expression chock full of side effects -- assignments, &lt;/p&gt;
&lt;p&gt;&amp;gt;increments, decrements, pointer stores and so on,&lt;/p&gt;
&lt;p&gt;As a VB-ish guy with a more plodding mind than all you crack C guys :-) I personally find that creating expressions that are complex enough that you actually have to play at being the compiler to figure out what they're doing is a programming practice that doesn't get me much. Except unexpected behavior, haha. &lt;/p&gt;
&lt;p&gt;Eric, perhaps you have addressed this in the past? &lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8552209</link><pubDate>Mon, 26 May 2008 13:53:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8552209</guid><dc:creator>Michel Desangles</dc:creator><description>&lt;p&gt;Sean, you had me rolling on the floor with this one. Best example of thinking out of the box ever.&lt;/p&gt;
&lt;p&gt;Excellent article, Eric, thanks.&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8553413</link><pubDate>Tue, 27 May 2008 00:10:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8553413</guid><dc:creator>Grant Husbands</dc:creator><description>&lt;p&gt;I've seen it stated before, but does anyone really believe in PEDMAS as stated? &amp;nbsp;3-1+1 is 3, not 1. Similarly, 4*3/2 is 6, not 4, even under integer math. Essentially, addition and subtraction have equal precedence, as do division and mutliplication.&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8572262</link><pubDate>Wed, 04 Jun 2008 01:37:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8572262</guid><dc:creator>blake (not a math guy either)</dc:creator><description>&lt;p&gt;I know it's been a few days, but I still felt the need to reply. fwiw i'm not a math wiz either, but i know some bits.&lt;/p&gt;
&lt;p&gt;@Grant, PEDMAS is just a mnemonic but not the whole story, D &amp;amp; M have equal priority (left to right) as do Addition and Subtraction (left to right) - the grade 7 math blog agrees with me :)&lt;/p&gt;
&lt;p&gt;P&lt;/p&gt;
&lt;p&gt;E&lt;/p&gt;
&lt;p&gt;DM&lt;/p&gt;
&lt;p&gt;AS&lt;/p&gt;
&lt;p&gt;Addition is associative and commutative. Treat the -1 as being + (-1) and then apply the rule. Some examples of commenters above made the mistake of putting the brackets in the wrong place and ignoring the negative quantities. When done incorrectly, the answer is incorrect. &lt;/p&gt;
&lt;p&gt;Instead, I think you'll agree that the following are equivalent:&lt;/p&gt;
&lt;p&gt;3 + (-1) + 1 = &lt;/p&gt;
&lt;p&gt;1 + (-1) + 3 = &lt;/p&gt;
&lt;p&gt;1 + 3 + (-1) &lt;/p&gt;
&lt;p&gt;similarly @ oldnewthing, you're correct that the statements you made are incompatible, because fundamentally hey are two different expressions. You mistakenly wrote 3 + -2 + -1 to an non-equivalent form: 3 + -1* (2 + -1)&lt;/p&gt;
&lt;p&gt;whereas correct bracketing could be 3 + -1 * (2 + 1) --&amp;gt; (distributive property) ax + bx = x(a+b) where x = -1&lt;/p&gt;
&lt;p&gt;also equivalent expressions:&lt;/p&gt;
&lt;p&gt;3-2-1&lt;/p&gt;
&lt;p&gt;3 + -2 + -1 =&lt;/p&gt;
&lt;p&gt;3 + -1 + -2 =&lt;/p&gt;
&lt;p&gt;-2 + 3 + -1 = &lt;/p&gt;
&lt;p&gt;3 + (-2 + -1) =&lt;/p&gt;
&lt;p&gt;(3 + -2) + -1&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;Additionally, incorrect answers by computing devices with limited precision shouldn't be inferred as a mathematical proof of how numbers work. Computers can lie, and often do if you don't watch what you're doing.&lt;/p&gt;
&lt;p&gt;In a computer: 4000000000.0F + 1.0F = 4E+09&lt;/p&gt;
&lt;p&gt;In real life: 4,000,000,000,000 + 1 = 4,000,000,000,001&lt;/p&gt;
&lt;p&gt;When in doubt, use brackets. It lets both the compiler and programmers know what you mean.&lt;/p&gt;
&lt;p&gt;@steve - nice one. very funny.&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8578168</link><pubDate>Fri, 06 Jun 2008 18:21:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8578168</guid><dc:creator>nikov</dc:creator><description>&lt;p&gt;Hi, Eric.&lt;/p&gt;
&lt;p&gt;Look at this example:&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;class A&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void Main()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var x = F(1) + F(2) + F(3);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static A F(int x)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(x);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static A operator +(A x, A y)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;+&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;It prints:&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;+&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;+&lt;/p&gt;
&lt;p&gt;So, the first + operator evaluates before the F(3) operand. Is it required by the specification, or it is not specified? In other words, can a conforming implementation give the following result?&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;+&lt;/p&gt;
&lt;p&gt;+&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;nikov&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8587747</link><pubDate>Tue, 10 Jun 2008 01:53:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8587747</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;The observed order is required. Look at it this way. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;F(1) + F(2) + F(3)&lt;/p&gt;
&lt;p&gt;is equivalent to&lt;/p&gt;
&lt;p&gt;((F(1) + F(2)) + F(3)&lt;/p&gt;
&lt;p&gt;which is equivalent to&lt;/p&gt;
&lt;p&gt;(A.op+(F(1), F(2)) + F(3)&lt;/p&gt;
&lt;p&gt;which is equivalent to&lt;/p&gt;
&lt;p&gt;A.op+(A.op+(F(1), F(2)), F(3))&lt;/p&gt;
&lt;p&gt;And now the order of operations is clear. The values of A, op+ and A.op+ are resolved by the compiler and have no side effects so we'll ignore those. &amp;nbsp;The other operations are resolved in order from left to right. So first we compute F(1), then we compute F(2), then we compute the result of the inner op+, because that expression is to the left of F(3). &amp;nbsp;F(3) is computed, and now we have enough information to pass arguments to the outer op+.&lt;/p&gt;
</description></item><item><title>re: Precedence vs Associativity vs Order</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx#8594535</link><pubDate>Fri, 13 Jun 2008 17:25:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8594535</guid><dc:creator>Matt Jones</dc:creator><description>&lt;p&gt;&amp;quot;(As an exercise can you find values for a, b, c such that (a + b) + c is unequal to a + (b + c) in C#?)&amp;quot;&lt;/p&gt;
&lt;p&gt;Well, this isn't exactly what you was after, but I thought it was a humorous misinterpretation of your request :) &amp;nbsp;Here A + B + C isn't even the same as A + B + C.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Matt&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Launcher&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static int a = 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static int A&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return a *= a;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static int B&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return a++;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static int C&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return a += 5;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int ans1 = (A + B) + C;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int ans2 = (A + B) + C;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int ans3 = A + (B + C);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item></channel></rss>