{"id":38968,"date":"2022-12-29T13:45:07","date_gmt":"2022-12-29T13:45:07","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38968"},"modified":"2023-01-28T15:08:38","modified_gmt":"2023-01-28T15:08:38","slug":"numpy-nextafter","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-nextafter","title":{"rendered":"Numpy Nextafter: How to Use Numpy Nextafter in Python?"},"content":{"rendered":"\n<p>Looking for a function to return the next floating point value of a given number in the direction of another number? Do you want the same function to carry out the same with a collection of numbers rather than a single number?<\/p>\n\n\n\n<p>Well, look no further for the <em>numpy <\/em>library has got just the thing you are looking for \u2013 the <em>nextafter( ) <\/em>function. In this article, we shall demonstrate the functionality of the above function along with understanding the basic constructs that are to be fed as inputs for its functioning. <\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-kron\" data-type=\"post\" data-id=\"38818\">Numpy.kron(): How to Calculate Kronecker Product Using Numpy?<\/a><\/em><\/strong><\/p>\n\n\n\n<p>We shall get things started by first importing the <em>numpy <\/em>library using the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>Thereafter, we shall explore further the&nbsp;<em>nextafter( )&nbsp;<\/em>function through each of the following sections.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of the&nbsp;<em>nextafter( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Using <em>nextafter( ) <\/em>on Scalars<\/strong><\/li>\n\n\n\n<li><strong>Using <em>nextafter( ) <\/em>on N-Dimensional Arrays<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax of\u00a0the <em>nextafter( )<\/em>\u00a0function<\/strong><\/h2>\n\n\n\n<p>The function relies on the primary inputs x1 &#038; x2 as given below of which, x1 is the entity for which the next floating point value is to be found along the direction of x2. The other optional inputs that can also be used within the <em>nextafter( ) <\/em>function are as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.nextafter(x1, x2, out=None, *, where=True, dtype=None)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>x1 \u2013&nbsp;<\/em><\/strong>N-dimensional array or a scalar for which the nearest floating point value is to be found<\/li>\n\n\n\n<li><strong><em>x2 \u2013<\/em><\/strong>&nbsp;N-dimensional array or a scalar to provide the direction of search<\/li>\n\n\n\n<li><strong><em>out \u2013<\/em><\/strong> an optional construct set to <em>none <\/em>by default, but could be used to store the results in the desired array which is of the same length as the output<\/li>\n\n\n\n<li><strong><em>* <\/em>\u2013 <\/strong>kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function<\/li>\n\n\n\n<li><strong><em>where \u2013 <\/em><\/strong>an optional construct which is used to calculate the universal function (ufunc) at the given position when set to <em>True <\/em>(default setting) or not calculate when set to <em>False<\/em><\/li>\n\n\n\n<li><strong><em>dtype \u2013 <\/em><\/strong>an optional construct used to specify the data type which is being used<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using <em>nextafter( )<\/em> on Scalars<\/strong><\/h2>\n\n\n\n<p>In this section let us use a couple of scalars for deploying into the <em>nextafter( ) <\/em>function as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 100\nb = -50\nnp.nextafter(a, b)\n<\/pre><\/div>\n\n\n<p>Once the above code is run, the following computation happens in the back end to return the next nearest number to \u2018a\u2019 when searched in the direction of \u2018b\u2019.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2018b\u2019 is given as \u2018-50\u2019 which means that the direction in which the next nearest number to \u2018a\u2019 is to be searched leftwards or before \u2018100\u2019, rather than after it.<\/li>\n\n\n\n<li>The nearest number to \u2018100\u2019 is found to be 99.99999999999999 (Python returns the floating point number to 14-digit precision after the decimal).<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"212\" height=\"101\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-nextafter-on-Scalars.jpg\" alt=\"Using Nextafter On Scalars\" class=\"wp-image-38978\"\/><figcaption class=\"wp-element-caption\">Using <em>nextafter( )<\/em> On Scalars<\/figcaption><\/figure>\n\n\n\n<p>From the above explanation, it could be deduced that the direction of search is synonymous with searching a number (first input, x1) in the number line rightwards when the second input \u2018x2\u2019 is greater than the first input \u2018x1\u2019 or leftwards if the second input \u2018x2\u2019 is lesser than the first input \u2018x1\u2019.<\/p>\n\n\n\n<p>The same logic holds good even if one uses positive or negative infinity as \u2018x2\u2019 rather than the other numbers and we shall use the following code to establish this conundrum.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nc = -10\nd = +np.inf\nnp.nextafter(c, d)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"236\" height=\"84\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-nextafter-on-Scalars-in-Direction-of-Infinity.jpg\" alt=\"Using Nextafter On Scalars In Direction Of Infinity\" class=\"wp-image-38979\"\/><figcaption class=\"wp-element-caption\">Using <em>nextafter<\/em>( ) on Scalars in the Direction of +Infinity<\/figcaption><\/figure>\n\n\n\n<p>The above result bears evidence that the nearest floating point number returned is towards the right of \u2018-10\u2019 in the number line since the direction of the search is positive infinity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using <em>nextafter( ) <\/em>on N-Dimensional Arrays<\/strong><\/h2>\n\n\n\n<p>When deployed on arrays the results would be rounded off to the same number of digits after the decimal given in the array, rather than the whole 14 digits as depicted below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;1.05, 3, 7],\n                &#x5B;2.9, 9, 4.3666667]])\nar2 = np.array(&#x5B;&#x5B;-1, 0.3, +np.inf],\n                &#x5B;0, -np.inf, 10]])\nnp.nextafter(ar1, ar2)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"134\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-nextafter-on-Arrays.jpg\" alt=\"Using Nextafter On Arrays\" class=\"wp-image-38982\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-nextafter-on-Arrays.jpg 390w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Using-nextafter-on-Arrays-300x103.jpg 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><figcaption class=\"wp-element-caption\">Using <em>nextafter<\/em>( ) on Arrays<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on how to use the\u00a0<em>nextafter( )\u00a0<\/em>function from the\u00a0<em>numpy\u00a0<\/em>library. Here\u2019s another article that details the calculation of the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-kron\" type=\"URL\" id=\"https:\/\/www.askpython.com\/python\/how-to-calculate-kronecker-product-using-numpy\" target=\"_blank\" rel=\"noreferrer noopener\">Kronecker product using numpy kron()<\/a>. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are looking to level up in Python.\u00a0<em>Mazel tov<\/em>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for a function to return the next floating point value of a given number in the direction of another number? Do you want the same function to carry out the same with a collection of numbers rather than a single number? Well, look no further for the numpy library has got just the thing [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":38969,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38968","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\/38968","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\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38968"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38968\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38969"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}