<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dat Ngo on Medium]]></title>
        <description><![CDATA[Stories by Dat Ngo on Medium]]></description>
        <link>https://medium.com/@datngo?source=rss-243f57d03c27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O7B36K-n7A6hDVRjgDBE0w.png</url>
            <title>Stories by Dat Ngo on Medium</title>
            <link>https://medium.com/@datngo?source=rss-243f57d03c27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:20:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@datngo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Arithmetic operators in C#]]></title>
            <link>https://medium.com/@datngo/arithmetic-operators-in-net-part-1-14aea3eb0220?source=rss-243f57d03c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/14aea3eb0220</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Dat Ngo]]></dc:creator>
            <pubDate>Fri, 03 Sep 2021 04:00:59 GMT</pubDate>
            <atom:updated>2021-09-14T09:29:32.883Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*kD9BaGy6_Oa9uufGpDgH9A.jpeg" /></figure><p>The topic will be back to the basics of arithmetic operators. I also create some unit-tests with XUnit framework to demonstrate these implementations. Hope to understand better.</p><h3>Operator precedence of Math</h3><p>The following list orders arithmetic operators starting from the highest precedence to the lowest:</p><ul><li>Postfix increment x++ and decrement x-- operators</li><li>Prefix increment ++x and decrement --x and unary + and - operators</li><li>Multiplicative *, /, and % operators</li><li>Additive + and - operators</li><li>Binary arithmetic operators are left-associative. That is, operators with the same precedence level are evaluated from left to right.</li><li>Use parentheses, (), to change the order of evaluation imposed by operator precedence and associativity.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d8cb3a4fddc6a0cb68caefe6c2a1608/href">https://medium.com/media/7d8cb3a4fddc6a0cb68caefe6c2a1608/href</a></iframe><h3><strong>Unary</strong></h3><p>There are operators:</p><ul><li>++(increment)</li><li>— (decrement)</li><li>+ (plus)</li><li>- (minus)</li></ul><h4>Increment operator ++</h4><ul><li><strong>i++</strong> means “Give me the value before the increment”</li><li><strong>++i</strong> means “Give me the value after the increment”</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8d68a5ae2b65aee4149e3e70f7da081/href">https://medium.com/media/b8d68a5ae2b65aee4149e3e70f7da081/href</a></iframe><h4>Decrement operator —</h4><ul><li><strong>i —</strong> means “Give me the value before the decrement”</li><li><strong>— i</strong> means “Give me the value after the decrement”</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c3942bf9011e0bb200170706efc8daa/href">https://medium.com/media/9c3942bf9011e0bb200170706efc8daa/href</a></iframe><h4>Unary plus and minus operators</h4><p><strong>Note*</strong>: The <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types">ulong</a> type doesn’t support the unary - operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/079e5be375603772545e26e0c9c0f2d7/href">https://medium.com/media/079e5be375603772545e26e0c9c0f2d7/href</a></iframe><h3>Binary</h3><p>There are operators:</p><ul><li>* (multiplication)</li><li>/ (division)</li><li>% (remainder)</li><li>+(addition)</li><li>- (subtraction) operators</li></ul><h4>Integer division</h4><p>For the operands of integer types, the result of the / operator is of an integer type and equals the quotient of the two operands rounded towards zero:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06fc12b83efdda7c64d8c12a23bfbdbe/href">https://medium.com/media/06fc12b83efdda7c64d8c12a23bfbdbe/href</a></iframe><h4>Integer remainder</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9448c03cd024c7b73ecb4c593c2b4bb/href">https://medium.com/media/a9448c03cd024c7b73ecb4c593c2b4bb/href</a></iframe><h4>Addition operator +</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a63dbbff118f8d887e685f8d9b9b64db/href">https://medium.com/media/a63dbbff118f8d887e685f8d9b9b64db/href</a></iframe><h4>Subtraction operator -</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12944ce768c4639de86995dd76274790/href">https://medium.com/media/12944ce768c4639de86995dd76274790/href</a></iframe><h4>Multiplication operator *</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d55d38137d36448e3743cb96574d584/href">https://medium.com/media/0d55d38137d36448e3743cb96574d584/href</a></iframe><h3>Ternary</h3><p>A conditional expression evaluates only one of the two expressions: either <strong>consequent</strong> or <strong>alternative</strong></p><blockquote>is this condition true ? yes : no</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcfeab907fab8f43a451f2920de5a583/href">https://medium.com/media/bcfeab907fab8f43a451f2920de5a583/href</a></iframe><h4>Conditional ref expression</h4><p>In the case of a conditional ref expression, the type of consequent and alternative must be the same. Conditional ref expressions are <strong>not target-typed.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6ec5d25c32bea1313088a21d60e2df9/href">https://medium.com/media/b6ec5d25c32bea1313088a21d60e2df9/href</a></iframe><h3>Expressions</h3><p>They are sequences of operators and operands. Evaluating an expression results in a single value for the entire expression.</p><h4>Logical exclusive OR operator ^</h4><p>The <strong>^</strong> operator also known as the logical <strong>XOR</strong></p><p>That is, for the bool operands, the ^ operator computes the same result as the inequality operator <strong>!=</strong>.</p><p>The result of x ^ y is true if</p><ul><li>x evaluates to true and y evaluates to false</li><li>x evaluates to false and y evaluates to true</li></ul><p>Otherwise, the result is false</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e935049011c038416e14cb43dbb59c1/href">https://medium.com/media/2e935049011c038416e14cb43dbb59c1/href</a></iframe><h4>Logical AND operator &amp;</h4><p>The &amp; operator computes the logical AND of its operands. The result of x &amp; y is true if both x and y evaluate to true. Otherwise, the result is false.</p><p>The &amp; operator evaluates both operands even if the left-hand operand evaluates to false, so that the operation result is false regardless of the value of the right-hand operand.</p><blockquote>This operator always <em>evaluate the right-hand operand regardless the left-hand operand evaluates to true or false</em>.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7344f9851355f699ee207aad9ce0ecc/href">https://medium.com/media/c7344f9851355f699ee207aad9ce0ecc/href</a></iframe><h4>Logical OR operator |</h4><p>The | operator computes the logical OR of its operands. The result of x | y is true if either x or y evaluates to true. Otherwise, the result is false.</p><p>The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand.</p><blockquote>This operator always evaluate the right-hand operand regardless the left-hand operand evaluates to true or false.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54dec749e14a4cda925bc41fd3dd7229/href">https://medium.com/media/54dec749e14a4cda925bc41fd3dd7229/href</a></iframe><h4>Conditional logical AND operator &amp;&amp;</h4><ul><li>The conditional logical AND operator &amp;&amp;, also known as the “<strong>short-circuiting”</strong> logical AND operator, computes the logical AND of its operands. The result of x &amp;&amp; y is true if both x and y evaluate to true. Otherwise, the result is false. If x evaluates to false, y is not evaluated.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/784d20d351911c5c21eda520662b9fc4/href">https://medium.com/media/784d20d351911c5c21eda520662b9fc4/href</a></iframe><h4>Conditional logical OR operator ||</h4><ul><li>The conditional logical OR operator ||, also known as the “<strong>short-circuiting</strong>” logical OR operator, computes the logical OR of its operands. The result of x || y is true if either x or y evaluates to true. Otherwise, the result is false. If x evaluates to true, y is not evaluated.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b297c3681d6183f24efb61fbf8352c59/href">https://medium.com/media/b297c3681d6183f24efb61fbf8352c59/href</a></iframe><h3>Nullable Boolean logical operators</h3><p>For<strong> bool?</strong> operands, the &amp; (logical AND) and | (logical OR) operators support the three-valued logic as follows:</p><ul><li>The &amp; operator produces true only if both its operands evaluate to true. If either x or y evaluates to false, x &amp; y produces false (even if another operand evaluates to null). Otherwise, the result of x &amp; y is null.</li><li>The | operator produces false only if both its operands evaluate to false. If either x or y evaluates to true, x | y produces true (even if another operand evaluates to null). Otherwise, the result of x | y is null.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uZ9ONqsrKoJOWoZ2Eb0Alw.png" /><figcaption>Truth table for Nullable&lt;bool&gt; operators</figcaption></figure><h3>Arithmetic overflow and division by zero</h3><h4>Integer arithmetic overflow</h4><p>Integer division by zero always throws a DivideByZeroException.</p><p>In case of integer arithmetic overflow, an overflow checking context, which can be checked or unchecked, controls the resulting behavior:</p><ul><li>In a checked context, if overflow happens in a constant expression, a compile-time error occurs. Otherwise, when the operation is performed at run time, an OverflowException is thrown.</li><li>In an unchecked context, the result is truncated by discarding any high-order bits that don&#39;t fit in the destination type.</li><li>By default, arithmetic operations occur in an unchecked context.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c66b5d0436670e9f31211b148a67dab/href">https://medium.com/media/0c66b5d0436670e9f31211b148a67dab/href</a></iframe><h4>Floating-point arithmetic overflow</h4><ul><li>Arithmetic operations with the float and double types never throw an exception.</li><li>The result of arithmetic operations with those types can be one of the special values that represent infinity and not-a-number:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00ae90d31a0a71dafbc42b9ea4a96318/href">https://medium.com/media/00ae90d31a0a71dafbc42b9ea4a96318/href</a></iframe><ul><li>For the operands of the decimal type, arithmetic overflow always throws an OverflowException and division by zero always throws a DivideByZeroException.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ad27d584a1d7f3dbfd667432553a30a/href">https://medium.com/media/1ad27d584a1d7f3dbfd667432553a30a/href</a></iframe><h3>Reference:</h3><ul><li><a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators">https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators</a></li><li><a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators">https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14aea3eb0220" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Round-off errors in C#]]></title>
            <link>https://medium.com/@datngo/round-off-errors-in-net-17769d3deb21?source=rss-243f57d03c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/17769d3deb21</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Dat Ngo]]></dc:creator>
            <pubDate>Fri, 03 Sep 2021 02:25:23 GMT</pubDate>
            <atom:updated>2021-09-03T09:22:23.565Z</atom:updated>
            <content:encoded><![CDATA[<h3>Round-off errors in C#</h3><p>By default, double is the default floating-point data type in C#</p><p>There are some round-off errors if we using money calculation by float or double, please make sure to use decimal value instead.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8e7aa4abb06cf1e4272c91096fdad4d/href">https://medium.com/media/e8e7aa4abb06cf1e4272c91096fdad4d/href</a></iframe><p>Reference: <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#round-off-errors">Round-off errors</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17769d3deb21" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>