{"id":5559,"date":"2022-10-08T00:01:19","date_gmt":"2022-10-08T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5559"},"modified":"2022-10-01T10:36:24","modified_gmt":"2022-10-01T17:36:24","slug":"playing-with-ha-yuge-gmp-integers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5559","title":{"rendered":"Playing with Ha-Yuge GMP Integers"},"content":{"rendered":"<p>I&#8217;ve written a few books on using external libraries, which I shamelessly plug later in this post. After confirming that the library is installed (see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5532\">last week&#8217;s post<\/a>), the next step isn&#8217;t to code, but rather to peruse the online documentation, the API or Application Programming Interface.<br \/>\n<!--more--><br \/>\nFor the GMP library, which handles painfully large integers, the API or online manual is found here: <a href=\"https:\/\/gmplib.org\/manual\/index\" rel=\"noopener\" target=\"_blank\">https:\/\/gmplib.org\/manual\/index<\/a><\/p>\n<p>Don&#8217;t expect any handholding. Programmers write to other programmers. Often they&#8217;re so immersed in the topic they forget about people who are just starting out. That&#8217;s okay; it&#8217;s the reason you&#8217;re reading this blog and not the official GNU manual. But do check out the manual.<\/p>\n<p>The GMP integer values are stored as structures cast as the <em>mpz_t<\/em> datatype. (I assume the structure&#8217;s contents are revealed somewhere on the library&#8217;s website, though knowing the details isn&#8217;t necessary.) So to use the huge integer values, you must follow certain procedures and use the specific functions available in the GMP library.<\/p>\n<p>Due to its construction, the <em>mpz_t<\/em> datatype requires three steps to initialize; you cannot use the equal sign operator to assign a value:<\/p>\n<ol>\n<li>Declare the <em>mpz_t<\/em> variable.<\/li>\n<li>Initialize the variable.<\/li>\n<li>Assign the variable a value.<\/li>\n<\/ol>\n<p>The declaration is simple:<\/p>\n<p><code>mpz_t i;<\/code><\/p>\n<p>This statement creates an <em>mpz_t<\/em> variable <code>i<\/code>. It allocates the structure, but like any variable declared in C, the contents are garbage. A second step is required:<\/p>\n<p><code>mpz_init(i);<\/code><\/p>\n<p>The <em>mpz_init()<\/em> function initializes variable <code>i<\/code>, setting its <em>int<\/em> value to zero. The variable can now be used in the host of <em>mpz<\/em> and <em>gmp<\/em> functions.<\/p>\n<p>To assign a value to the <em>mpz_t<\/em> variable, you use one of an <em>mpz_set<\/em> function:<\/p>\n<p><code>mpz_set_ui(i,1000000000);<\/code><\/p>\n<p>The <em>mpz_set_ui()<\/em> function assigns an unsigned integer (<em>ui<\/em>) value of one billion to <em>mpz_t<\/em> variable <code>i<\/code>. The variable can now be used in your code, but only with special functions. For example, the <em>gmp_printf()<\/em> function, shown in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_08-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_10_08-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;gmp.h&gt;\r\n\r\nint main()\r\n{\r\n    mpz_t i;\r\n    mpz_init(i);\r\n\r\n    <span class=\"comments\">\/* i equals one billion *\/<\/span>\r\n    mpz_set_ui(i,1000000000);\r\n    gmp_printf(\"%Zd\\n\",i);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>gmp_printf()<\/em> function at Line 10 works like the standard <em>printf()<\/em> function, though it&#8217;s capable of outputting <em>mpz_t<\/em> values. The <code>%Zd<\/code> placeholder outputs the <em>mpz_t<\/em> value as a decimal number. Here&#8217;s a sample run:<\/p>\n<p><code>1000000000<\/code><\/p>\n<p>Impressive!<\/p>\n<p>But this code is only a start.<\/p>\n<p>As I wrote earlier, various <em>mpz_set<\/em> functions are available to assign specific values to <em>mpz_t<\/em> variables. For example, <em>mpz_set_si()<\/em> to assign a <em>signed int<\/em> value; <em>mpz_set_d()<\/em> to assign a <em>double<\/em>. The full list is available on <a href=\"https:\/\/gmplib.org\/manual\/Assigning-Integers\" rel=\"noopener\" target=\"_blank\">this page<\/a>. You can even convert a value from a string.<\/p>\n<p>The library also hosts functions that read standard input for assigning values, such as <em>gmp_scanf()<\/em>. The manual lists the lot.<\/p>\n<p>In next week&#8217;s Lesson, I cover some basic math and <em>mpz_t<\/em> value manipulation.<\/p>\n<p>Oh! Here are the books I&#8217;ve written that cover augmenting your C code by incorporating other libraries:<\/p>\n<table>\n<tr>\n<td>\n<a href=\"https:\/\/www.amazon.com\/dp\/B075FGJJCS?ie=UTF8&#038;qid=1504752200&#038;sr=8-1&#038;keywords=ncurses+programming&#038;linkCode=li3&#038;tag=dangookin&#038;linkId=203cf745ae6d203d210831061f3ea608&#038;language=en_US&#038;ref_=as_li_ss_il\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" border=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&#038;ASIN=B075FGJJCS&#038;Format=_SL250_&#038;ID=AsinImage&#038;MarketPlace=US&#038;ServiceVersion=20070822&#038;WS=1&#038;tag=dangookin&#038;language=en_US\" ><\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ir-na.amazon-adsystem.com\/e\/ir?t=dangookin&#038;language=en_US&#038;l=li3&#038;o=1&#038;a=B075FGJJCS\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/>\n<\/td>\n<td>\n<a href=\"https:\/\/www.amazon.com\/dp\/B07ZS294W6?keywords=curl+programming&#038;qid=1572480296&#038;sr=8-4&#038;linkCode=li3&#038;tag=dangookin&#038;linkId=44d3357074023a128030498aced6d598&#038;language=en_US&#038;ref_=as_li_ss_il\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" border=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&#038;ASIN=B07ZS294W6&#038;Format=_SL250_&#038;ID=AsinImage&#038;MarketPlace=US&#038;ServiceVersion=20070822&#038;WS=1&#038;tag=dangookin&#038;language=en_US\" ><\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ir-na.amazon-adsystem.com\/e\/ir?t=dangookin&#038;language=en_US&#038;l=li3&#038;o=1&#038;a=B07ZS294W6\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/>\n<\/td>\n<td>\n<a href=\"https:\/\/www.amazon.com\/dp\/B07W8HJGJV?keywords=XML+JSON&#038;qid=1565066237&#038;s=digital-text&#038;sr=1-1&#038;linkCode=li3&#038;tag=dangookin&#038;linkId=45ad5fcf2abee7ff7c294d284427e8a9&#038;language=en_US&#038;ref_=as_li_ss_il\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" border=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&#038;ASIN=B07W8HJGJV&#038;Format=_SL250_&#038;ID=AsinImage&#038;MarketPlace=US&#038;ServiceVersion=20070822&#038;WS=1&#038;tag=dangookin&#038;language=en_US\" ><\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ir-na.amazon-adsystem.com\/e\/ir?t=dangookin&#038;language=en_US&#038;l=li3&#038;o=1&#038;a=B07W8HJGJV\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><br \/>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>With the GMP library installed, your code can play with integers of impressively large values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5559\">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-5559","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\/5559","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=5559"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5559\/revisions"}],"predecessor-version":[{"id":5570,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5559\/revisions\/5570"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}