{"id":5572,"date":"2022-10-15T00:01:23","date_gmt":"2022-10-15T07:01:23","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5572"},"modified":"2022-10-08T08:49:09","modified_gmt":"2022-10-08T15:49:09","slug":"gmp-integers-and-math","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5572","title":{"rendered":"GMP Integers and Math"},"content":{"rendered":"<p>Continuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5559\">last week&#8217;s Lesson<\/a>, just as you can&#8217;t use the equal sign to assign an <em>mpz_t<\/em> huge integer value, you can&#8217;t use the standard arithmetic operators to do math &mdash; huge math. Nope, you must use special GMP library functions to do the math.<br \/>\n<!--more--><br \/>\nAddition is where math teaching begins, probably because a 5-year-old quickly understands the concept of &#8220;more.&#8221; The function is <em>mpz_add()<\/em>, which has three arguments:<\/p>\n<ul>\n<li>The result<\/li>\n<li>Value 1<\/li>\n<li>Value 2<\/li>\n<\/ul>\n<p>All arguments are initialized <em>mpz_t<\/em> values, which makes this function the equivalent of:<\/p>\n<p><code>The result = Value 1 + Value 2;<\/code><\/p>\n<p>Here&#8217;s some code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_15-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_10_15-Lesson-a.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 a,b,x;\r\n\r\n    <span class=\"comments\">\/* initialize *\/<\/span>\r\n    mpz_init(a);\r\n    mpz_init(b);\r\n    mpz_init(x);\r\n\r\n    <span class=\"comments\">\/* set values *\/<\/span>\r\n    mpz_set_ui(a,3000000000);    <span class=\"comments\">\/* three billion *\/<\/span>\r\n    mpz_set_ui(b,5000000000);    <span class=\"comments\">\/* five billion *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* add the values *\/<\/span>\r\n    mpz_add(x,a,b);\r\n    <span class=\"comments\">\/* output *\/<\/span>\r\n    gmp_printf(\"%Zd + %Zd = %Zd\\n\",a,b,x);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variables <code>a<\/code>, <code>b<\/code>, and <code>x<\/code> are declared at Line 5. These values are initialized at Lines 8 through 10, which sets their values to zero and allows them to be used in further functions. If you attempt to use an uninitialized value, the program may barf as the pointers in the <em>mpz_t<\/em> structure are uninitialized.<\/p>\n<p>Values for variables <code>a<\/code> and <code>b<\/code> are assigned at Lines 13 and 14.<\/p>\n<p>The math happens at Line 17 with the <em>mpz_add()<\/em> function. Values <code>a<\/code> and <code>b<\/code> are added, with the result stored in variable <code>x<\/code>. Line 19 uses the <em>gmp_printf()<\/em> function to output the results:<\/p>\n<p><code>3000000000 + 5000000000 = 8000000000<\/code><\/p>\n<p>(Remember to add the <code>-lgmp<\/code> switch when compiling at the command line to link in the GMP library.)<\/p>\n<p>Now you know that three billion plus five billion equals eight billion. Of course, with the GMP library you can use much larger values: Modify the code on your own and wear out the zero key on the keyboard to see how humongous the values get.<\/p>\n<p>Multiplication is handled by the <em>mzp_addmul()<\/em> function. Its arguments are the same as for <em>mpz_add()<\/em>, though the second and third arguments are multiplied and stored in the first argument.<\/p>\n<p>To update the existing code, I added the following lines before the <em>return<\/em> statement:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* multiply *\/<\/span>\r\n    mpz_addmul(x,a,b);\r\n    <span class=\"comments\">\/* output *\/<\/span>\r\n    gmp_printf(\"%Zd * %Zd = %Zd\\n\",a,b,x);\r\n<\/pre>\n<p>The <em>mpz_addmul()<\/em> function performs multiplication, storing the result in re-used variable <code>x<\/code>. The <em>gmp_printf()<\/em> statement outputs the results.<\/p>\n<p>Here&#8217;s a sample run, which also includes output from the original code:<\/p>\n<p><code>3000000000 + 5000000000 = 8000000000<br \/>\n3000000000 * 5000000000 = 15000000008000000000<\/code><\/p>\n<p>While three billion times five billion is equal to 15 quintillion, I hope you spied the digit 8 in the result. This value is left over from the first calculation, stored in variable <code>x<\/code>. Unlike standard integer datatypes, you must always initialize a <em>mpz_t<\/em> variable before it&#8217;s used. In this code, variable <code>x<\/code> &mdash; already assigned the value eight billion &mdash; is re-used. Therefore the bits in the value aren&#8217;t reset for the new calculation.<\/p>\n<p>To fix the problem, you must add an <em>mpz_init()<\/em> function before the <em>mpz_addmul()<\/em> function:<\/p>\n<p><code>mpz_init(x);<\/code><\/p>\n<p>Once re-initialized (to zero), the output is correct:<\/p>\n<p><code>3000000000 + 5000000000 = 8000000000<br \/>\n3000000000 * 5000000000 = 15000000000000000000<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10_15-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to see the full, correct code on GitHub.<\/p>\n<p>You can explore the rest of the math functions on your own, as they&#8217;re referenced in the <a href=\"https:\/\/gmplib.org\/manual\/index\" rel=\"noopener\" target=\"_blank\">online manual (API)<\/a>. Have fun with the ha-yuge integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nope, you can&#8217;t use standard C operators to manipulate the ha-yuge values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5572\">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-5572","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\/5572","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=5572"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5572\/revisions"}],"predecessor-version":[{"id":5577,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5572\/revisions\/5577"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}