<?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>ToddHa's WebLog : Math</title><link>http://blogs.msdn.com/toddha/archive/tags/Math/default.aspx</link><description>Tags: Math</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Assumptions are the mother of all.</title><link>http://blogs.msdn.com/toddha/archive/2007/02/07/assumptions-are-the-mother-of-all.aspx</link><pubDate>Wed, 07 Feb 2007 23:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1621572</guid><dc:creator>toddha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/toddha/comments/1621572.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=1621572</wfw:commentRss><description>&lt;P&gt;While working on one of&amp;nbsp;my side projects at work,&amp;nbsp;I encounted something interesting with the % sign. The code that I'm going to talk about has nothing to do with any upcoming products -- it's simply a side project I'm working on in my free time that uses WCF and WPF (which are 2 major features of the .NET Framework 3.0). That being said, I've (hopefully) abstracted away everything project specific.&lt;/P&gt;
&lt;P&gt;The problem I encountered came from doing some basic math in C# code. Simply, I construct an equation and evaluate what the answer should be by going from left to right (ignoring operator precedence). So when I came up with some final result, I wanted to&amp;nbsp;double check that I was correct.&amp;nbsp;But since I don't keep track of every equation or answer, I had to write a quick tool to trace through it. I'm a big fan of Python for prototyping&amp;nbsp;because I've spent a lot of time in it and understand it well. So I copied over my C# code, made some slight modifications to save the intermediate data&amp;nbsp;and then parse and print the data at the end, and ran it. 
&lt;P&gt;My final answer in Python didn't match that from the one calculated in C# (note that C++ returns the same as C#). The algorithm looked correct, but the final results didn't match. 
&lt;P&gt;Eventually, I&amp;nbsp;found my problem. Take the following simple equation as an example: 
&lt;P&gt;&lt;EM&gt;-13&amp;nbsp;% 6 = ?&lt;/EM&gt; 
&lt;P&gt;C# evaluates this to -1. Python, on the other hand, evaluates to 5. &lt;EM&gt;Um...what?&lt;/EM&gt; Which one is correct? 
&lt;P&gt;Well, it depends on your point of view. Technically they're both correct.&amp;nbsp; Just one method takes it&amp;nbsp;one step further than the other. 
&lt;P&gt;&lt;EM&gt;-13 + 6 = -7&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;-7 + 6 = -1&lt;/EM&gt; (C# answer) 
&lt;P&gt;&lt;EM&gt;-1 + 6 = 5&lt;/EM&gt; (Python answer) 
&lt;P&gt;Hm. At first I thought that the in Python the answer will always be in the set &lt;EM&gt;{0, 1, 2, .... n-1 }&lt;/EM&gt;, and the C# answer will always be in the set &lt;EM&gt;{0, ±1, ±2, .... ±(n-1) }&lt;/EM&gt;. To check this, I ran another simple equation through Python and C#. 
&lt;P&gt;&lt;EM&gt;8 % -3 = ?&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;8&amp;nbsp;+ -3 = 5&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;5&amp;nbsp;+ -3 = 2 &lt;/EM&gt;(C# answer)&amp;nbsp; 
&lt;P&gt;&lt;EM&gt;2 + -3 = -1&lt;/EM&gt; (Python answer) 
&lt;P&gt;Theory crushed. Python and C# follow the same basic principle rule of all the answer being in the set &lt;EM&gt;{0, ±1, ±2, ... ±(n-1) }&lt;/EM&gt;. But why does Python always take it further than the C# ? 
&lt;P&gt;So I looked up the community &lt;A title="defition of modulo" href="http://en.wikipedia.org/wiki/Modulo_operation" mce_href="http://en.wikipedia.org/wiki/Modulo_operation"&gt;definition of modulo&lt;/A&gt;. 
&lt;P&gt;It turns out that the % sign has no strict definition. Python keeps the same sign as the divisor. C# keeps the same sign as the dividend. 
&lt;P&gt;What you should take away : don't always assume that operators will work the same in different languages. 
&lt;P&gt;Extra credit : 
&lt;P&gt;Windows Live Search and Google both provide basic calculator functions in their search. For example, searching for the term "2 + 2" in WLS and Google both give 4. But what about "-13 % 6"?&amp;nbsp;Why&amp;nbsp;do / don't they give the same answer?&amp;nbsp; 
&lt;P&gt;&lt;A title="(Hint : look at who uses Python)" href="http://en.wikipedia.org/wiki/Python_software" mce_href="http://en.wikipedia.org/wiki/Python_software"&gt;Answer&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1621572" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>