{"id":7895,"date":"2021-01-21T19:27:01","date_gmt":"2021-01-21T13:57:01","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=7895"},"modified":"2026-07-13T12:31:24","modified_gmt":"2026-07-13T07:01:24","slug":"numpy-allclose","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-allclose\/","title":{"rendered":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> np.allclose compares numeric values within relative and absolute tolerances. Choose atol for the scale near zero, understand broadcasting, and use assert_allclose when a test should fail loudly with stricter shape semantics.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-allclose-tolerances.png\" alt=\"NumPy allclose infographic showing relative tolerance, absolute tolerance, broadcasting, and equal_nan behavior\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>allclose uses relative and absolute tolerances; choose atol carefully for values near zero and check shapes when testing.<\/figcaption><\/figure>\n\n\n<p class=\"wp-block-paragraph\"><strong>NumPy allclose()<\/strong> checks whether two arrays are close enough to be treated as equal within a tolerance. It is useful when floating-point numbers differ by tiny rounding errors, where exact equality with <code>==<\/code> would be too strict.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NumPy is a third-party package, not a built-in Python module. The official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.allclose.html\" target=\"_blank\" rel=\"noreferrer noopener\">numpy.allclose documentation<\/a> defines the function as <code>numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)<\/code>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#What_Does_NumPy_allclose_Do\" >What Does NumPy allclose() Do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Parameters\" >Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Return_Value\" >Return Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Examples_of_NumPy_allclose\" >Examples of NumPy allclose()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#NumPy_allclose_vs_isclose\" >NumPy allclose() vs isclose()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Related_NumPy_Guides\" >Related NumPy Guides<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Understand_The_Tolerance\" >Understand The Tolerance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Set_atol_For_Small_Values\" >Set atol For Small Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Check_Shape_And_NaNs_Separately\" >Check Shape And NaNs Separately<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-numpy-allclose\"><span class=\"ez-toc-section\" id=\"What_Does_NumPy_allclose_Do\"><\/span>What Does NumPy allclose() Do?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>np.allclose()<\/code> compares two input arrays element by element. It returns one Boolean value: <code>True<\/code> only when every compared element is close within the selected tolerance, and <code>False<\/code> when at least one element is outside that tolerance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The comparison uses this rule:<\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-code\"><code>absolute(a - b) &lt;= (atol + rtol * absolute(b))<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">That means both tolerances matter. <code>rtol<\/code> scales with the size of the reference value <code>b<\/code>, while <code>atol<\/code> is a fixed absolute tolerance. For very small values near zero, choose <code>atol<\/code> carefully instead of blindly trusting the default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-syntax\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-code\"><code>numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)<\/code><\/pre><\/div>\n\n\n\n\n<!-- Python Pool visual layout repair 2026-07-13 -->\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/allclose-arrays-b158.png\" alt=\"Python Pool infographic showing two floating-point arrays, differences, relative scale, and comparison\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Array values: Two floating-point arrays, differences, relative scale, and comparison.<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-parameters\"><span class=\"ez-toc-section\" id=\"Parameters\"><\/span>Parameters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>a, b<\/strong>: array-like inputs to compare.<\/li>\n\n\n\n<li><strong>rtol<\/strong>: relative tolerance. It is multiplied by the absolute value of <code>b<\/code>.<\/li>\n\n\n\n<li><strong>atol<\/strong>: absolute tolerance. It sets a fixed allowed difference.<\/li>\n\n\n\n<li><strong>equal_nan<\/strong>: when set to <code>True<\/code>, NaN values in the same positions compare as equal.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-return-value\"><span class=\"ez-toc-section\" id=\"Return_Value\"><\/span>Return Value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>np.allclose()<\/code> returns <code>True<\/code> if all compared values are within tolerance. Otherwise, it returns <code>False<\/code>.<\/p>\n\n\n\n\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/allclose-tolerance-b158.png\" alt=\"Python Pool infographic mapping absolute and relative tolerance through rtol, atol, and allclose\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Tolerances: Absolute and relative tolerance through rtol, atol, and allclose.<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-examples-of-the-allclose-function\"><span class=\"ez-toc-section\" id=\"Examples_of_NumPy_allclose\"><\/span>Examples of NumPy allclose()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-basic-numpy-allclose-example\">1. Basic NumPy allclose() Example<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;div class=&quot;pythonpool-code-scroll&quot; style=&quot;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;&quot;&gt;import numpy as np\n\na = &#x5B;1.67, 3.56]\nb = &#x5B;1.68, 3.56]\n\nprint(np.allclose(a, b))&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-preformatted\">False<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The first values differ by <code>0.01<\/code>, which is larger than the default tolerance for numbers around this size. Because not every element is close, the function returns <code>False<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-numpy-allclose-with-custom-tolerance\">2. NumPy allclose() With Custom Tolerance<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;div class=&quot;pythonpool-code-scroll&quot; style=&quot;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;&quot;&gt;import numpy as np\n\narr1 = &#x5B;1e10, 1e-8]\narr2 = &#x5B;1.00001e10, 1e-9]\n\nprint(np.allclose(arr1, arr2, rtol=0.1, atol=0.1))&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-preformatted\">True<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here the tolerances are intentionally loose. The absolute differences fit inside the allowed tolerance, so every element is considered close.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-choosing-atol-for-small-values\">3. Choose atol Carefully for Small Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The default <code>atol<\/code> can be misleading when numbers are much smaller than one. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;div class=&quot;pythonpool-code-scroll&quot; style=&quot;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;&quot;&gt;import numpy as np\n\nprint(np.allclose(1e-9, 2e-9))\nprint(np.allclose(1e-9, 2e-9, atol=0.0))&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-preformatted\">True\nFalse<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">With the default absolute tolerance, both tiny values are considered close. With <code>atol=0.0<\/code>, the relative difference matters more and the comparison becomes stricter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-array-having-nan-value\">4. Array Having NaN Value<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;div class=&quot;pythonpool-code-scroll&quot; style=&quot;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;&quot;&gt;import numpy as np\n\nprint(np.allclose(&#x5B;2.0, np.nan], &#x5B;2.0, np.nan]))&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-preformatted\">False<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">By default, matching NaN values are not treated as equal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-array-having-nan-value-with-equal-nan-true\">5. Array Having NaN Value With equal_nan=True<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;div class=&quot;pythonpool-code-scroll&quot; style=&quot;max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;&quot;&gt;import numpy as np\n\nprint(np.allclose(&#x5B;2.0, np.nan], &#x5B;2.0, np.nan], equal_nan=True))&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre class=\"wp-block-preformatted\">True<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>equal_nan=True<\/code>, NaN values in the same positions are considered equal. Positive and negative infinity values are considered equal only when they are in the same positions and have the same sign.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-numpy-allclose-vs-isclose\"><span class=\"ez-toc-section\" id=\"NumPy_allclose_vs_isclose\"><\/span>NumPy allclose() vs isclose()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>np.allclose()<\/code> and <a href=\"https:\/\/www.pythonpool.com\/numpy-isclose\/\">np.isclose()<\/a> use the same closeness idea, but they return different shapes of results. Use <code>allclose()<\/code> when you need one final <code>True<\/code> or <code>False<\/code>. Use <code>isclose()<\/code> when you want to see which individual elements are close.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><div class=\"pythonpool-table-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><table><thead><tr><th>Function<\/th><th>Returns<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>np.allclose(a, b)<\/code><\/td><td>One Boolean value<\/td><td><code>False<\/code><\/td><\/tr><tr><td><code>np.isclose(a, b)<\/code><\/td><td>A Boolean array<\/td><td><code>[ True False]<\/code><\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">One more important detail: <code>allclose(a, b)<\/code> is not perfectly symmetric because <code>b<\/code> is used in the relative tolerance part of the formula. In rare cases, <code>allclose(a, b)<\/code> and <code>allclose(b, a)<\/code> can differ.<\/p>\n\n\n\n\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/allclose-special-b158.png\" alt=\"Python Pool infographic comparing NaN, infinity, equal_nan, dtype, and floating-point comparison\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Special values: NaN, infinity, equal_nan, dtype, and floating-point comparison.<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-related-numpy-guides\"><span class=\"ez-toc-section\" id=\"Related_NumPy_Guides\"><\/span>Related NumPy Guides<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>NumPy isclose Explained With Examples in Python<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythonpool.com\/np-sign\/\">NumPy sign()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythonpool.com\/numpy-ndarray-object-is-not-callable-error-and-resolution\/\">NumPy ndarray object is not callable: error and resolution<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\">Fixed: type numpy.ndarray doesn&#8217;t define __round__ method<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythonpool.com\/numpy-array-to-pandas-dataframe\/\">How to Convert NumPy Array to Pandas DataFrame<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>numpy.allclose()<\/code> is best when you need a single answer to the question, &#8220;Are these arrays close enough?&#8221; For reliable results, choose <code>rtol<\/code> and <code>atol<\/code> for the scale of your data, remember that NaN handling requires <code>equal_nan=True<\/code>, and use <code>np.isclose()<\/code> when you need element-by-element detail.<\/p>\n\n\n<h2><span class=\"ez-toc-section\" id=\"Understand_The_Tolerance\"><\/span>Understand The Tolerance<span class=\"ez-toc-section-end\"><\/span><\/h2><p>allclose treats two values as close when the absolute difference is within atol + rtol * abs(reference). Relative tolerance scales with the reference value, while absolute tolerance is the floor that matters near zero. The comparison is directional because the second input supplies the reference scale.<\/p><div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre><code class=\"language-python\">import numpy as np\n\nexpected = np.array([100.0, 0.001])\nactual = np.array([100.0005, 0.0010005])\nprint(np.allclose(actual, expected, rtol=1e-5, atol=1e-8))<\/code><\/pre><\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/allclose-check-b158.png\" alt=\"Python Pool infographic testing shape, broadcasting, scale, signed zero, and assertion output\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Comparison checks: Shape, broadcasting, scale, signed zero, and assertion output.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Set_atol_For_Small_Values\"><\/span>Set atol For Small Values<span class=\"ez-toc-section-end\"><\/span><\/h2><p>The default absolute tolerance can be too permissive or too strict for a domain with values much smaller than one. Select tolerances from measurement noise, numerical error, or a test contract. Do not copy defaults into scientific validation without checking the scale.<\/p><div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre><code class=\"language-python\">import numpy as np\n\nprint(np.allclose(1e-9, 2e-9))\nprint(np.allclose(1e-9, 2e-9, atol=1e-10, rtol=0))<\/code><\/pre><\/div><h2><span class=\"ez-toc-section\" id=\"Check_Shape_And_NaNs_Separately\"><\/span>Check Shape And NaNs Separately<span class=\"ez-toc-section-end\"><\/span><\/h2><p>Broadcasting means compatible different shapes can compare as close, so add an explicit shape assertion when shape is part of correctness. NaNs are unequal by default; equal_nan=True treats NaNs in matching positions as equal. Use numpy.testing.assert_allclose for test failures and diagnostics.<\/p><div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\"><pre><code class=\"language-python\">import numpy as np\n\nleft = np.array([1.0, np.nan])\nright = np.array([1.0, np.nan])\nprint(np.allclose(left, right))\nprint(np.allclose(left, right, equal_nan=True))<\/code><\/pre><\/div><p>See NumPy&#8217;s <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.allclose\">allclose reference<\/a> for the comparison equation and edge cases.<\/p>\n\n<p>For related numerical checks, compare <a href=\"https:\/\/www.pythonpool.com\/numpy-any\/\">NumPy any()<\/a>, <a href=\"https:\/\/www.pythonpool.com\/numpy-standard-deviation\/\">standard deviation<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/numpy-mean\/\">mean calculations<\/a> with the tolerance policy here.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3>What does np.allclose do?<\/h3>\n<p>It returns True when numeric inputs are element-wise equal within relative and absolute tolerances.<\/p>\n<h3>What is the difference between rtol and atol?<\/h3>\n<p>rtol scales the allowed difference relative to the reference, while atol supplies a fixed floor for values near zero.<\/p>\n<h3>Does allclose require equal shapes?<\/h3>\n<p>No. NumPy broadcasting can allow compatible different shapes, so check shape equality separately when shape is part of correctness.<\/p>\n<h3>How does allclose handle NaN values?<\/h3>\n<p>NaNs compare as unequal by default; pass equal_nan=True when matching NaN positions should count as close.<\/p>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What does np.allclose do?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It returns True when numeric inputs are element-wise equal within relative and absolute tolerances.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between rtol and atol?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"rtol scales the allowed difference relative to the reference, while atol supplies a fixed floor for values near zero.\"}},{\"@type\":\"Question\",\"name\":\"Does allclose require equal shapes?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"No. NumPy broadcasting can allow compatible different shapes, so check shape equality separately when shape is part of correctness.\"}},{\"@type\":\"Question\",\"name\":\"How does allclose handle NaN values?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"NaNs compare as unequal by default; pass equal_nan=True when matching NaN positions should count as close.\"}}]}<\/script>","protected":false},"excerpt":{"rendered":"<p>Choose rtol and atol from the measurement scale, understand broadcasting, and use assert_allclose for strict tests.<\/p>\n","protected":false},"author":12,"featured_media":8077,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[3367,3364,3366,3368,3365,3363,3207],"class_list":["post-7895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-allclose-numpy-2","tag-allclose-numpy","tag-numpy-allclose-failing","tag-numpy-allclose-python","tag-numpy-allclose-vs-is-close","tag-numpy-allclose-zero","tag-numpy-isclose-vs-allclose","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>NumPy allclose(): Compare Floating-Point Arrays with Tolerances<\/title>\n<meta name=\"description\" content=\"Use NumPy allclose to compare numeric arrays with relative and absolute tolerances, broadcasting, and explicit NaN behavior.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/numpy-allclose\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy allclose(): Compare Floating-Point Arrays with Tolerances\" \/>\n<meta property=\"og:description\" content=\"Choose rtol and atol from the measurement scale, understand broadcasting, and use assert_allclose for strict tests.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-allclose\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-21T13:57:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:01:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-allclose-tolerances.png\" \/>\n<meta name=\"author\" content=\"Prachee Sao\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-allclose-tolerances.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prachee Sao\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/\"},\"author\":{\"name\":\"Prachee Sao\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/b91d749826b21e606d55cda77d51ef47\"},\"headline\":\"NumPy allclose(): Compare Floating-Point Arrays with Tolerances\",\"datePublished\":\"2021-01-21T13:57:01+00:00\",\"dateModified\":\"2026-07-13T07:01:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/\"},\"wordCount\":866,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Theatre-Actor-Portfolio-Website-32.png\",\"keywords\":[\"allclose numpy\",\"allclose() numpy\",\"numpy allclose failing\",\"numpy allclose python\",\"numpy allclose vs is close\",\"numpy allclose zero\",\"numpy isclose vs allclose\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/\",\"name\":\"NumPy allclose(): Compare Floating-Point Arrays with Tolerances\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Theatre-Actor-Portfolio-Website-32.png\",\"datePublished\":\"2021-01-21T13:57:01+00:00\",\"dateModified\":\"2026-07-13T07:01:24+00:00\",\"description\":\"Use NumPy allclose to compare numeric arrays with relative and absolute tolerances, broadcasting, and explicit NaN behavior.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Theatre-Actor-Portfolio-Website-32.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/Theatre-Actor-Portfolio-Website-32.png\",\"width\":1350,\"height\":650,\"caption\":\"NumPy allclose\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-allclose\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NumPy allclose(): Compare Floating-Point Arrays with Tolerances\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/b91d749826b21e606d55cda77d51ef47\",\"name\":\"Prachee Sao\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g\",\"caption\":\"Prachee Sao\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances","description":"Use NumPy allclose to compare numeric arrays with relative and absolute tolerances, broadcasting, and explicit NaN behavior.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/numpy-allclose\/","og_locale":"en_US","og_type":"article","og_title":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances","og_description":"Choose rtol and atol from the measurement scale, understand broadcasting, and use assert_allclose for strict tests.","og_url":"https:\/\/www.pythonpool.com\/numpy-allclose\/","og_site_name":"Python Pool","article_published_time":"2021-01-21T13:57:01+00:00","article_modified_time":"2026-07-13T07:01:24+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-allclose-tolerances.png","type":"","width":"","height":""}],"author":"Prachee Sao","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-allclose-tolerances.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Prachee Sao","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/"},"author":{"name":"Prachee Sao","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47"},"headline":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances","datePublished":"2021-01-21T13:57:01+00:00","dateModified":"2026-07-13T07:01:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/"},"wordCount":866,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-32.png","keywords":["allclose numpy","allclose() numpy","numpy allclose failing","numpy allclose python","numpy allclose vs is close","numpy allclose zero","numpy isclose vs allclose"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-allclose\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/","url":"https:\/\/www.pythonpool.com\/numpy-allclose\/","name":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-32.png","datePublished":"2021-01-21T13:57:01+00:00","dateModified":"2026-07-13T07:01:24+00:00","description":"Use NumPy allclose to compare numeric arrays with relative and absolute tolerances, broadcasting, and explicit NaN behavior.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-allclose\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-32.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-32.png","width":1350,"height":650,"caption":"NumPy allclose"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-allclose\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"NumPy allclose(): Compare Floating-Point Arrays with Tolerances"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47","name":"Prachee Sao","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g","caption":"Prachee Sao"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=7895"}],"version-history":[{"count":17,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7895\/revisions"}],"predecessor-version":[{"id":41552,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7895\/revisions\/41552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/8077"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=7895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=7895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=7895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}