{"id":5532,"date":"2022-10-01T00:03:19","date_gmt":"2022-10-01T07:03:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5532"},"modified":"2022-10-08T08:39:39","modified_gmt":"2022-10-08T15:39:39","slug":"really-ha-yuge-integers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5532","title":{"rendered":"Really Ha-yuge Integers"},"content":{"rendered":"<p>Unlike real numbers, integers are stored in binary as-is: The bits are read and values set. The only limit on an integer&#8217;s value is on the number of bits in the binary chunk &mdash; the bit width &mdash; which sets the value&#8217;s range, positive and negative or just positive.<br \/>\n<!--more--><br \/>\nFor most computer systems, the largest integer value is based on a word size of 64 bits. In C, this size is data type <em>long<\/em> or <em>long long<\/em>. The values range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The <em>unsigned<\/em> value is large enough that E notation is required: 2<sup>64<\/sup>-1<\/p>\n<p>Until the 128-bit era dawns for PCs and Linux boxes, the <em>unsigned long long<\/em> is as huge as you&#8217;re going to see for straight integer values in C &mdash; or on any computer system. Here&#8217;s a demo:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_01-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_10_01-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    unsigned long long i = 0;\r\n\r\n    while(i++)\r\n        printf(\"%llu\\n\",i);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program outputs values from 1 through 18,446,744,073,709,551,614. As you can imagine, it takes quite a while to run, mostly because screen output is slow. I gave up running this code after three days. My guess is it would take over a week for the program to finish. When output is suppressed, however, the program runs in 0.003 seconds! That&#8217;s how long it takes the computer to count from one through 2<sup>64<\/sup>-1.<\/p>\n<p>Just imagine how much longer it would take such a program to run if the upper limit of an integer were the full 128 bytes! Or maybe 256 bytes? Or an infinite number of bytes?<\/p>\n<p>Well, you don&#8217;t need to imagine. That&#8217;s because the GMP library exists. It extends the C language&#8217;s capabilities to deal with integers of nearly infinite size.<\/p>\n<p>In this blog, I normally don&#8217;t cover C programming beyond the standard library. Yes, I&#8217;ve covered the math library, which must be linked for Linux systems. But this is the first time I&#8217;m exploring adding a new library to your compiler: the GMP Library.<\/p>\n<p>The full name is the GNU Multiple Precision Arithmetic Library. It may be pre-installed on your computer, though probably not. You must use the Linux package manager to obtain and install the library. This step adds the required header files and libraries so that you can play with <em>ha-yuge<\/em> integer values.<\/p>\n<p>In Linux, install the <code>libgmp-dev<\/code> package. The command I use in Ubuntu is:<\/p>\n<p><code>sudo apt-get install libgmp-dev<\/code><\/p>\n<p>On the Mac, I use the <a href=\"https:\/\/brew.sh\/\" rel=\"noopener\" target=\"_blank\">Homebrew program<\/a> to obtain the package. The command is <code>brew install gmp<\/code><\/p>\n<p>To confirm that the package is installed, run the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_01-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_10_01-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;gmp.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"GMP version: %d\\n\",__GNU_MP_RELEASE);\r\n    return(0);\r\n}<\/pre>\n<p>The program&#8217;s output reflects the GMP library version number, which can vary depending on the package. For the Mac, it shows:<\/p>\n<p><code>GMP version: 60201<\/code><\/p>\n<p>In Ubuntu, I see:<\/p>\n<p><code>GMP version: 60102<\/code><\/p>\n<p>Upon success (meaning the compiler sees the <code>gmp.h<\/code> header file), you can confirm that the GMP library is installed on your system. Compiling code that uses the library&#8217;s functions requires that you use the <code>-lgmp<\/code> switch when compiling. This option is added last at the command prompt. In an IDE, you must configure the compiler to add the <code>-lgmp<\/code> switch or otherwise link the GMP library as part of the build process.<\/p>\n<p>I continue exploration of the GMP library and its monstrous integer values with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5559\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can kiss goodbye the traditional range of integer values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5532\">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-5532","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\/5532","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=5532"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5532\/revisions"}],"predecessor-version":[{"id":5578,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5532\/revisions\/5578"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}