{"id":120943,"date":"2020-11-20T16:23:54","date_gmt":"2020-11-20T16:23:54","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17086"},"modified":"2020-11-20T16:23:54","modified_gmt":"2020-11-20T16:23:54","slug":"python-math-domain-error-how-to-fix-this-stupid-bug","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/20\/python-math-domain-error-how-to-fix-this-stupid-bug\/","title":{"rendered":"Python Math Domain Error (How to Fix This Stupid Bug)"},"content":{"rendered":"<p>You may encounter a special <code>ValueError<\/code> when working with Python\u2019s <a href=\"https:\/\/blog.finxter.com\/python-math-module\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Math Module [Ultimate Guide]\"><code>math<\/code> module<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ValueError: math domain error<\/pre>\n<p>Python raises this error when you try to do something that is not mathematically possible or mathematically defined.<\/p>\n<p>To understand this error, have a look at the definition of the <strong>domain<\/strong>:<\/p>\n<p>&#8220;<em>The <strong>domain <\/strong>of a function is the complete set of possible values of the independent variable. Roughly speaking, the <strong>domain<\/strong> is the set of all possible (input) x-values which result in a valid (output) y-value.<\/em>&#8221; (<a href=\"https:\/\/www.intmath.com\/functions-and-graphs\/2a-domain-and-range.php\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/www.intmath.com\/functions-and-graphs\/2a-domain-and-range.php\">source<\/a>)<\/p>\n<p class=\"has-pale-cyan-blue-background-color has-background\">The domain of a function is the set of all possible input values. If Python throws the <code>ValueError: math domain error<\/code>, you&#8217;ve passed an undefined input into the <code>math<\/code> function. Fix the error by passing a valid input for which the function is able to calculate a numerical output.<\/p>\n<p>Here are a few examples:<\/p>\n<h2>Python Math Domain Error Sqrt<\/h2>\n<p>The math domain error appears if you pass a negative argument into the <code>math.sqrt()<\/code> function. It&#8217;s mathematically impossible to calculate the square root of a negative number without using complex numbers. Python doesn&#8217;t get that and throws a <code>ValueError: math domain error<\/code>. <\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"736\" height=\"396\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-30.png\" alt=\"Graph square root\" class=\"wp-image-17128\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-30.png 736w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-30-300x161.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-30-150x81.png 150w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/figure>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from math import sqrt\nprint(sqrt(-1)) '''\nTraceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\code.py\", line 2, in &lt;module> print(sqrt(-1))\nValueError: math domain error '''<\/pre>\n<p>You can fix the math domain error by using the <code>cmath<\/code> package that allows the creation of complex numbers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from cmath import sqrt\nprint(sqrt(-1))\n# 1j<\/pre>\n<h2>Python Math Domain Error Log<\/h2>\n<p>The <code>math domain error<\/code> for the <code>math.log()<\/code> function appears if you pass a zero value into it&#8212;the logarithm is not defined for value 0. <\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"687\" height=\"391\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-29.png\" alt=\"Graph logarithm\" class=\"wp-image-17124\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-29.png 687w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-29-300x171.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-29-150x85.png 150w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/figure>\n<p>Here&#8217;s the code on an input value outside the domain of the logarithm function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from math import log\nprint(log(0))<\/pre>\n<p>The output is the math domain error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\code.py\", line 3, in &lt;module> print(log(0))\nValueError: math domain error<\/pre>\n<p>You can fix this error by passing a valid input value into the <code>math.log()<\/code> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from math import log\nprint(log(0.000001))\n# -13.815510557964274<\/pre>\n<p>This error can sometimes appear if you pass a very small number into it&#8212;Python&#8217;s float type cannot express all numbers. To pass a value &#8220;close to 0&#8221;, use the <code>Decimal<\/code> module with higher precision, or pass a very small input argument such as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">math.log(sys.float_info.min)<\/pre>\n<h2>Python Math Domain Error Acos<\/h2>\n<p>The <code>math domain error<\/code> for the <code>math.acos()<\/code> function appears if you pass a value into it for which it is not defined&#8212;arccos is only defined for values between -1 and 1. <\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"717\" height=\"391\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-27.png\" alt=\"Graph arccos(x)\" class=\"wp-image-17118\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-27.png 717w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-27-300x164.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-27-150x82.png 150w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/figure>\n<p>Here&#8217;s the wrong code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\nprint(math.acos(2))<\/pre>\n<p>The output is the math domain error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\code.py\", line 3, in &lt;module> print(math.acos(2))\nValueError: math domain error<\/pre>\n<p>You can fix this error by passing a valid input value between [-1,1] into the <code>math.acos()<\/code> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\nprint(math.acos(0.5))\n# 1.0471975511965979<\/pre>\n<h2>Python Math Domain Error Asin<\/h2>\n<p>The <code>math domain error<\/code> for the <code>math.asin()<\/code> function appears if you pass a value into it for which it is not defined&#8212;arcsin is only defined for values between -1 and 1.<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"734\" height=\"396\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-28.png\" alt=\"Graph Arcsin\" class=\"wp-image-17121\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-28.png 734w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-28-300x162.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/image-28-150x81.png 150w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure>\n<p>Here&#8217;s the erroneous code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\nprint(math.asin(2))<\/pre>\n<p>The output is the math domain error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\code.py\", line 3, in &lt;module> print(math.asin(2))\nValueError: math domain error<\/pre>\n<p>You can fix this error by passing a valid input value between [-1,1] into the <code>math.asin()<\/code> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\nprint(math.asin(0.5))\n# 0.5235987755982989<\/pre>\n<h2>Python Math Domain Error Pow<\/h2>\n<p>The <code>math domain error<\/code> for the <code>math.pow(a,b)<\/code> function to calculate a**b appears if you pass a negative base value into it and try to calculate a negative power of it. The reason it is not defined is that any negative number to the power of 0.5 would be the square number&#8212;and thus, a complex number. But complex numbers are not defined by default in Python!<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math\nprint(math.pow(-2, 0.5))<\/pre>\n<p>The output is the math domain error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\code.py\", line 3, in &lt;module> print(math.pow(-2, 0.5))\nValueError: math domain error<\/pre>\n<p>If you need a complex number, a<sup>b<\/sup> must be rewritten into e<sup>b ln a<\/sup>. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import cmath\nprint(cmath.exp(0.5 * cmath.log(-2)))\n# (8.659560562354932e-17+1.414213562373095j)<\/pre>\n<p>You see, it&#8217;s a complex number!<\/p>\n<h2>NumPy Math Domain Error &#8212; np.log(x)<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt # Plotting y = log(x)\nfig, ax = plt.subplots()\nax.set(xlim=(-5, 20), ylim=(-4, 4), title='log(x)', ylabel='y', xlabel='x')\nx = np.linspace(-10, 20, num=1000)\ny = np.log(x) plt.plot(x, y)<\/pre>\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"391\" height=\"279\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-64.png\" alt=\"\" class=\"wp-image-8765\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-64.png 391w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-64-300x214.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/figure>\n<p>This is the graph of <code>log(x)<\/code>. Don\u2019t worry if you don\u2019t understand the code, what\u2019s more important is the following point. You can see that log(x) tends to negative infinity as x tends to 0. Thus, it is mathematically meaningless to calculate the log of a negative number. If you try to do so, Python raises a math domain error.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> math.log(-10)\nTraceback (most recent call last): File \"&lt;stdin>\", line 1, in &lt;module>\nValueError: math domain error<\/pre>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-math-domain-error\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python Math Domain Error (How to Fix This Stupid Bug)<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may encounter a special ValueError when working with Python\u2019s math module. ValueError: math domain error Python raises this error when you try to do something that is not mathematically possible or mathematically defined. To understand this error, have a look at the definition of the domain: &#8220;The domain of a function is the complete [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-120943","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=120943"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120943\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=120943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=120943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=120943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}