{"id":37017,"date":"2022-11-19T11:01:25","date_gmt":"2022-11-19T11:01:25","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37017"},"modified":"2022-11-19T11:01:26","modified_gmt":"2022-11-19T11:01:26","slug":"numpy-trunc","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-trunc","title":{"rendered":"Numpy trunc() &#8211; Return the truncated value of the input, element-wise"},"content":{"rendered":"\n<p>Today, we&#8217;ll learn how to get the truncated value of inputs on our computer using the trunc() function. This function is similar to the ceiling and floor functions, but with a slightly different domain and range. To use this function, we need to import the NumPy module. Once we have imported NumPy, we can use the trunc() function to get the truncated value of any input.<\/p>\n\n\n\n<p><strong>Let us understand our <code>trunc<\/code> concept theoretically before getting implemented on our computers today.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Numpy.trunc() method?<\/h2>\n\n\n\n<p>The truncated value of the<strong> scalar&nbsp;x&nbsp;<\/strong>is the <strong>nearest integer&nbsp;y&nbsp;which is closer to zero than&nbsp;x&nbsp;is<\/strong>. Please have a look at the two examples below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The truncated value of -2.35 is  -2.0<\/strong>. The nearest integers of -2.35 are -3 and -2. -2 is closer to 0 than -3. So the truncated value for -2.35 is -2.0.<\/li>\n\n\n\n<li><strong>The truncated value of 3.35 is  3<\/strong>. The nearest integers of 3.35 are 3 and 4. 3 is closer to 0 than 4. So the truncated value for 3.35 is 3.<\/li>\n\n\n\n<li><strong>The truncated value for 0 is 0, and for -3 is -3, and for 5 is 5.<\/strong> (Truncated value for integers is same as the number)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using Numpy.trun()<\/h2>\n\n\n\n<p>Now we will see how to implement this function in our code snippet today. Let us see the syntax below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.trunc(x, out=None, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, subok : &#x5B;bool, datatype])\n<\/pre><\/div>\n\n\n<p>The parameters used in the above snippet function as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x:&nbsp;<\/strong>It can be a variable containing a value in Radian or it may be an array containing some value<\/li>\n\n\n\n<li><strong>out:<\/strong>&nbsp;A location in which the result is to be stored. If provided, it must have a shape similar to the input X. If not provided or None, a fresh value or result is returned. It\u2019s optional.<\/li>\n\n\n\n<li><strong>where = true:&nbsp;<\/strong>&nbsp;Where the condition is True, We must get our result or output for \u201cwhere = False\u201d We will not get any result. It\u2019s optional. y default its value is True.<\/li>\n\n\n\n<li><strong>casting=\u2019same_kind\u2019:<\/strong>&nbsp;It means only float64 to float32 value or results are allowed. The function shout cast values in this range or datatype.<\/li>\n\n\n\n<li><strong>order = \u2018K\u2019:<\/strong>&nbsp;It always results in an output array in the form of k ordered. (Note: There are 4 types of orders as&nbsp;<strong>{\u2018K\u2019, \u2018C\u2019, \u2018F\u2019, \u2018A\u2019}<\/strong>). It\u2019s optional.<\/li>\n\n\n\n<li><strong>subok<\/strong>: [bool, datatype]&nbsp;<strong>to make a subclass of the result or not<\/strong>. if True then provide a name for the subclass. It returns&nbsp;an array with the same shape and type as a given array. It\u2019s also optional.<\/li>\n<\/ul>\n\n\n\n<p>Now we will implement this function in our <strong>examples below<\/strong>. We should try two examples as well. And last, <strong>we will see the graphical representation for the same.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1 &#8211; Working with two values<\/h2>\n\n\n\n<p>In this example, we passed two values and received our output. In the second implementation, we passed a parameter for the out parameter. By default, the out parameter formats the output array. So, the truncated value for 3.25 was loaded into the c array and printed the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nb = np.trunc(-2.35)\nprint(b)\n\nnp.trunc(3.25, c)\nprint(c)\n\n#output\n    -2.0\n    &#x5B;3. 3. 3. 3. 3. 3. 3.]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Example 2 &#8211; Numpy.trunc() on an array of values<\/h2>\n\n\n\n<p>In this example, we are passing an array of values in our function. It will return a resultant array comprising of all corresponding truncated values for all values of the input array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ninput = np.array(&#x5B;-2.35, -1,  -0.36, 0, 0.36, 1, 5.69])\nd=np.trunc(input)\nprint(d)\n\n#output\n     &#x5B;-2. -1. -0.  0.  0.  1.  5.]\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Plotting numpy.trunc() on a graph<\/h2>\n\n\n\n<p>The numpy.trunc() function is used to return the truncated value of the input element, i.e. the integer part of the input element. If we have an input array, this function returns the truncated value for each element in the array. We can also represent the truncated value of a single element in a graphical manner using the x-y graph.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport numpy\nx=&#x5B;]\ny=&#x5B;]\ni=-6.99\nwhile (i&lt;7.00):\n    x.append(i)\n    y.append(numpy.trunc(i))\n    i=i+0.01\n\nplt.xlabel(&quot;x axis&quot;)\nplt.ylabel(&quot;y axis&quot;)\nplt.grid(linestyle=&#039;-&#039;, linewidth=0.5,color=&#039;red&#039;)\nplt.plot(x,y, linewidth=3, color = &#039;black&#039;)\n<\/pre><\/div>\n\n\n<p><strong>The above code snippet will give the output as below.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"379\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Capture-8.png\" alt=\"\" class=\"wp-image-37028\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Capture-8.png 639w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Capture-8-300x178.png 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So today, We covered the Numpy truncated function implementation in our code using python. We saw the graphical representation of this function for some limits. from this graph, You can somewhat analyze the function and how it works. Through this, You can much better understand our topics. We must visit again with some more exciting topics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we&#8217;ll learn how to get the truncated value of inputs on our computer using the trunc() function. This function is similar to the ceiling and floor functions, but with a slightly different domain and range. To use this function, we need to import the NumPy module. Once we have imported NumPy, we can use [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":37030,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37017"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37017\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37030"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}