{"id":2195,"date":"2016-11-12T00:01:19","date_gmt":"2016-11-12T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2195"},"modified":"2016-11-05T09:26:54","modified_gmt":"2016-11-05T16:26:54","slug":"the-abs-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2195","title":{"rendered":"The <em>abs()<\/em> Function"},"content":{"rendered":"<p>Seriously, why do programmers need an <em>abs()<\/em> function? It&#8217;s available in just about every programming language; if you view an alphabetic list of functions, <em>abs()<\/em> is probably the first one. Yet, what&#8217;s the point of a function that simply converts negative values to positive values?<br \/>\n<!--more--><br \/>\nThe point is that you find yourself needing positive values from time to time and the <em>abs()<\/em> function guarantees that result.<\/p>\n<p>For example, you subtract variable <code>b<\/code> from <code>a<\/code>, which returns the difference. The result could be positive or negative. To ensure that its positive, you use the <em>abs()<\/em> function:<\/p>\n<p><code>difference = abs( a - b );<\/code><\/p>\n<p>The <em>abs()<\/em> function is defined in the <code>stdlib.h<\/code> header file. It accepts integer values as input and returns a positive integer value.<\/p>\n<p>For floating-point values, use the <em>fabs()<\/em> function, defined in the <code>math.h<\/code> header file. It accepts <em>double<\/em> values as input and returns a positive <em>double<\/em> value. Other variations are available on these absolute value functions; refer to the man page entries for <em>abs()<\/em> and <em>fabs()<\/em>.<\/p>\n<p>The following code uses the <em>abs()<\/em> function to return the positive (absolute) difference between values in a loop that runs from -10 and steps up to +10:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    for(a=-10;a&lt;=10;a++)\r\n    {\r\n        printf(\"0 - %3d = %3d abs(%3d)\\n\",\r\n                a,\r\n                0-a,\r\n                abs(0-a));\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>abs()<\/em> function is nestled in the <em>printf()<\/em> statement at Line 10. Here&#8217;s the output:<\/p>\n<pre><code>0 - -10 =  10 abs( 10)\r\n0 -  -9 =   9 abs(  9)\r\n0 -  -8 =   8 abs(  8)\r\n0 -  -7 =   7 abs(  7)\r\n0 -  -6 =   6 abs(  6)\r\n0 -  -5 =   5 abs(  5)\r\n0 -  -4 =   4 abs(  4)\r\n0 -  -3 =   3 abs(  3)\r\n0 -  -2 =   2 abs(  2)\r\n0 -  -1 =   1 abs(  1)\r\n0 -   0 =   0 abs(  0)\r\n0 -   1 =  -1 abs(  1)\r\n0 -   2 =  -2 abs(  2)\r\n0 -   3 =  -3 abs(  3)\r\n0 -   4 =  -4 abs(  4)\r\n0 -   5 =  -5 abs(  5)\r\n0 -   6 =  -6 abs(  6)\r\n0 -   7 =  -7 abs(  7)\r\n0 -   8 =  -8 abs(  8)\r\n0 -   9 =  -9 abs(  9)\r\n0 -  10 = -10 abs( 10)<\/code><\/pre>\n<p>The <em>abs()<\/em> function always returns a positive value.<\/p>\n<p>Be on the lookout for functions that require positive values as input. That&#8217;s one of those times you must use an absolute value function when you might not think you need one. So when you read a function&#8217;s description and it requires positive value as input, use the <em>abs()<\/em> function to ensure that any variable passed is positive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The absolute value function returns the positive difference between two values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2195\">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-2195","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\/2195","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=2195"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions"}],"predecessor-version":[{"id":2207,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2195\/revisions\/2207"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}