{"id":4697,"date":"2021-04-10T00:01:02","date_gmt":"2021-04-10T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4697"},"modified":"2021-04-03T10:21:06","modified_gmt":"2021-04-03T17:21:06","slug":"positive-or-negative-zero","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4697","title":{"rendered":"Positive or Negative Zero"},"content":{"rendered":"<p>Mathematicians truly enjoy doing their math thing. As a mortal human, I don&#8217;t have a spot for math things on my &#8220;fun&#8221; spectrum. Yet, one of the more jolly things the math nerds do is discuss the value zero.<br \/>\n<!--more--><br \/>\nFor example, I&#8217;ve learned that zero is considered an even number. I also learned that zero isn&#8217;t the same thing as infinity. But when it comes to being positive or negative, mathematicians assert that zero is neither. This assertion isn&#8217;t held by some programming languages.<\/p>\n<p>Yes, Java, I&#8217;m looking at you.<\/p>\n<p>But this blog is about the C programming language, which doesn&#8217;t really have a hangup regarding negative zero or positive zero. Or does it?<\/p>\n<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4685\">last week&#8217;s Lesson<\/a>, I covered the sign bit. This bit determines which values are negative for signed integer variables. Zero is <code>00000000<\/code> in binary. But <code>10000000<\/code> is the value -128, not -0. This approach is how all integers work.<\/p>\n<p>Floating point values are also stored with a sign bit. Their bitwise arrangement is more complex than an integer, with intricate details I don&#8217;t want to get into. Still, for a real number (<em>float<\/em> or <em>double<\/em>) in C, it&#8217;s theoretically possible to have a positive or negative value of zero. The question is whether the C language recognizes the difference.<\/p>\n<p>In the following code, I use the sign operator to compare the values zero, positive zero, and negative zero.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_10-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_04_10-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float n,p,z;\r\n\r\n    n = -0.0;\r\n    p = +0.0;\r\n    z = 0.0;\r\n\r\n    if( z==z )\r\n        printf(\"%.3f is equal to zero\\n\",z);\r\n    if( z==n )\r\n        printf(\"%.3f is equal to negative zero\\n\",z);\r\n    if( z==p )\r\n        printf(\"%.3f is equal to positive zero\\n\",z);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Three <em>float<\/em> variables are declared, <code>n<\/code>, <code>p<\/code>, and <code>z<\/code>. These are assigned values in Lines 7 through 9, with <code>n<\/code> and <code>p<\/code> assigned negative zero and positive zero, respectively. These values are compared in a series of <em>if<\/em> statements. Here&#8217;s the output:<\/p>\n<p><code>0.000 is equal to zero<br \/>\n0.000 is equal to negative zero<br \/>\n0.000 is equal to positive zero<\/code><\/p>\n<p>As far as I can tell, the sign bit on the zero value is ignored &mdash; supposing that the sign bit is set in the first place.<\/p>\n<p>For experimental purposes, I attempted to manually flip the sign bit on a zero value. My efforts failed, though I think it&#8217;s always fun to mess with bits in a value to see the effect. Still, I believe for comparison purposes, any differences between zero, positive or negative, are irrelevant in the C language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is zero positive, negative, or just zero? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4697\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4697","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4697"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4697\/revisions"}],"predecessor-version":[{"id":4709,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4697\/revisions\/4709"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}