[{"data":1,"prerenderedAt":1486},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fvalueerror-math-domain-error-fix":3},{"id":4,"title":5,"body":6,"description":1479,"extension":1480,"meta":1481,"navigation":103,"path":1482,"seo":1483,"stem":1484,"__hash__":1485},"content\u002Ferrors\u002Fvalueerror-math-domain-error-fix.md","ValueError: math domain error (Fix)",{"type":7,"value":8,"toc":1447},"minimark",[9,13,25,28,57,71,76,201,210,214,217,248,251,255,258,291,300,304,307,313,350,353,370,375,409,411,423,429,464,466,478,482,485,514,518,596,598,607,610,617,621,631,711,723,727,738,826,830,833,941,947,953,956,1036,1039,1046,1053,1061,1097,1099,1112,1119,1123,1126,1181,1184,1188,1191,1208,1211,1241,1244,1311,1314,1318,1321,1355,1365,1369,1373,1376,1383,1390,1397,1405,1409,1415,1419,1443],[10,11,5],"h1",{"id":12},"valueerror-math-domain-error-fix",[14,15,16,20,21,24],"p",{},[17,18,19],"code",{},"ValueError: math domain error"," happens when you pass a value to a function in Python’s ",[17,22,23],{},"math"," module that is outside the valid input range.",[14,26,27],{},"This usually happens with functions like:",[29,30,31,37,42,47,52],"ul",{},[32,33,34],"li",{},[17,35,36],{},"math.sqrt()",[32,38,39],{},[17,40,41],{},"math.log()",[32,43,44],{},[17,45,46],{},"math.log10()",[32,48,49],{},[17,50,51],{},"math.asin()",[32,53,54],{},[17,55,56],{},"math.acos()",[14,58,59,60,63,64,66,67,70],{},"For example, ",[17,61,62],{},"math.sqrt(-1)"," fails because the ",[17,65,23],{}," module expects a real number, and ",[17,68,69],{},"-1"," is not a valid input for a real square root.",[72,73,75],"h2",{"id":74},"quick-fix","Quick fix",[77,78,83],"pre",{"className":79,"code":80,"language":81,"meta":82,"style":82},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import math\n\nx = -1\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"x must be 0 or greater\")\n","python","",[17,84,85,98,105,122,127,146,173,181],{"__ignoreMap":82},[86,87,90,94],"span",{"class":88,"line":89},"line",1,[86,91,93],{"class":92},"sVHd0","import",[86,95,97],{"class":96},"su5hD"," math\n",[86,99,101],{"class":88,"line":100},2,[86,102,104],{"emptyLinePlaceholder":103},true,"\n",[86,106,108,111,115,118],{"class":88,"line":107},3,[86,109,110],{"class":96},"x ",[86,112,114],{"class":113},"smGrS","=",[86,116,117],{"class":113}," -",[86,119,121],{"class":120},"srdBf","1\n",[86,123,125],{"class":88,"line":124},4,[86,126,104],{"emptyLinePlaceholder":103},[86,128,130,133,136,139,142],{"class":88,"line":129},5,[86,131,132],{"class":92},"if",[86,134,135],{"class":96}," x ",[86,137,138],{"class":113},">=",[86,140,141],{"class":120}," 0",[86,143,145],{"class":144},"sP7_E",":\n",[86,147,149,153,156,159,162,165,167,170],{"class":88,"line":148},6,[86,150,152],{"class":151},"sptTA","    print",[86,154,155],{"class":144},"(",[86,157,23],{"class":158},"slqww",[86,160,161],{"class":144},".",[86,163,164],{"class":158},"sqrt",[86,166,155],{"class":144},[86,168,169],{"class":158},"x",[86,171,172],{"class":144},"))\n",[86,174,176,179],{"class":88,"line":175},7,[86,177,178],{"class":92},"else",[86,180,145],{"class":144},[86,182,184,186,188,192,196,198],{"class":88,"line":183},8,[86,185,152],{"class":151},[86,187,155],{"class":144},[86,189,191],{"class":190},"sjJ54","\"",[86,193,195],{"class":194},"s_sjI","x must be 0 or greater",[86,197,191],{"class":190},[86,199,200],{"class":144},")\n",[14,202,203,204,206,207,161],{},"This error happens when you pass a value that is outside the valid range for a math function, such as ",[17,205,62],{}," or ",[17,208,209],{},"math.log(0)",[72,211,213],{"id":212},"what-this-error-means","What this error means",[14,215,216],{},"This error means:",[29,218,219,222,225],{},[32,220,221],{},"A math function received a value it cannot use",[32,223,224],{},"The value is outside the function's valid input range",[32,226,227,228],{},"Common examples include:\n",[29,229,230,233,239],{},[32,231,232],{},"square root of a negative number",[32,234,235,236],{},"logarithm of ",[17,237,238],{},"0",[32,240,241,242,244,245],{},"inverse trig functions with values outside ",[17,243,69],{}," to ",[17,246,247],{},"1",[14,249,250],{},"In short, Python is telling you: “This number is not allowed for this math function.”",[72,252,254],{"id":253},"when-this-error-happens","When this error happens",[14,256,257],{},"You will often see this error in cases like these:",[29,259,260,266,276,288],{},[32,261,262,263,265],{},"Using ",[17,264,36],{}," with a negative number",[32,267,262,268,206,270,272,273,275],{},[17,269,41],{},[17,271,46],{}," with ",[17,274,238],{}," or a negative number",[32,277,262,278,206,280,282,283,285,286],{},[17,279,56],{},[17,281,51],{}," with a value less than ",[17,284,69],{}," or greater than ",[17,287,247],{},[32,289,290],{},"Passing user input that was converted into an invalid number",[14,292,293,294,299],{},"If you are new to exceptions, see ",[295,296,298],"a",{"href":297},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained"," for a simple overview.",[72,301,303],{"id":302},"example-that-causes-the-error","Example that causes the error",[14,305,306],{},"Here are some common examples.",[308,309,311],"h3",{"id":310},"mathsqrt-1",[17,312,62],{},[77,314,316],{"className":79,"code":315,"language":81,"meta":82,"style":82},"import math\n\nprint(math.sqrt(-1))\n",[17,317,318,324,328],{"__ignoreMap":82},[86,319,320,322],{"class":88,"line":89},[86,321,93],{"class":92},[86,323,97],{"class":96},[86,325,326],{"class":88,"line":100},[86,327,104],{"emptyLinePlaceholder":103},[86,329,330,333,335,337,339,341,343,346,348],{"class":88,"line":107},[86,331,332],{"class":151},"print",[86,334,155],{"class":144},[86,336,23],{"class":158},[86,338,161],{"class":144},[86,340,164],{"class":158},[86,342,155],{"class":144},[86,344,345],{"class":113},"-",[86,347,247],{"class":120},[86,349,172],{"class":144},[14,351,352],{},"Output:",[77,354,356],{"className":79,"code":355,"language":81,"meta":82,"style":82},"ValueError: math domain error\n",[17,357,358],{"__ignoreMap":82},[86,359,360,364,367],{"class":88,"line":89},[86,361,363],{"class":362},"sZMiF","ValueError",[86,365,366],{"class":144},":",[86,368,369],{"class":96}," math domain error\n",[308,371,373],{"id":372},"mathlog0",[17,374,209],{},[77,376,378],{"className":79,"code":377,"language":81,"meta":82,"style":82},"import math\n\nprint(math.log(0))\n",[17,379,380,386,390],{"__ignoreMap":82},[86,381,382,384],{"class":88,"line":89},[86,383,93],{"class":92},[86,385,97],{"class":96},[86,387,388],{"class":88,"line":100},[86,389,104],{"emptyLinePlaceholder":103},[86,391,392,394,396,398,400,403,405,407],{"class":88,"line":107},[86,393,332],{"class":151},[86,395,155],{"class":144},[86,397,23],{"class":158},[86,399,161],{"class":144},[86,401,402],{"class":158},"log",[86,404,155],{"class":144},[86,406,238],{"class":120},[86,408,172],{"class":144},[14,410,352],{},[77,412,413],{"className":79,"code":355,"language":81,"meta":82,"style":82},[17,414,415],{"__ignoreMap":82},[86,416,417,419,421],{"class":88,"line":89},[86,418,363],{"class":362},[86,420,366],{"class":144},[86,422,369],{"class":96},[308,424,426],{"id":425},"mathacos2",[17,427,428],{},"math.acos(2)",[77,430,432],{"className":79,"code":431,"language":81,"meta":82,"style":82},"import math\n\nprint(math.acos(2))\n",[17,433,434,440,444],{"__ignoreMap":82},[86,435,436,438],{"class":88,"line":89},[86,437,93],{"class":92},[86,439,97],{"class":96},[86,441,442],{"class":88,"line":100},[86,443,104],{"emptyLinePlaceholder":103},[86,445,446,448,450,452,454,457,459,462],{"class":88,"line":107},[86,447,332],{"class":151},[86,449,155],{"class":144},[86,451,23],{"class":158},[86,453,161],{"class":144},[86,455,456],{"class":158},"acos",[86,458,155],{"class":144},[86,460,461],{"class":120},"2",[86,463,172],{"class":144},[14,465,352],{},[77,467,468],{"className":79,"code":355,"language":81,"meta":82,"style":82},[17,469,470],{"__ignoreMap":82},[86,471,472,474,476],{"class":88,"line":89},[86,473,363],{"class":362},[86,475,366],{"class":144},[86,477,369],{"class":96},[72,479,481],{"id":480},"how-to-fix-it","How to fix it",[14,483,484],{},"There are a few simple ways to fix this error.",[29,486,487,490,493,499,505],{},[32,488,489],{},"Check the valid range before calling the function",[32,491,492],{},"Clean and validate user input first",[32,494,495,496,498],{},"Use ",[17,497,132],{}," statements to block invalid values",[32,500,495,501,504],{},[17,502,503],{},"try-except"," if a value may sometimes be invalid",[32,506,495,507,510,511,513],{},[17,508,509],{},"cmath"," instead of ",[17,512,23],{}," if you need complex-number results",[308,515,517],{"id":516},"fix-1-check-the-value-before-calling-the-function","Fix 1: Check the value before calling the function",[77,519,521],{"className":79,"code":520,"language":81,"meta":82,"style":82},"import math\n\nx = 25\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"x must be 0 or greater\")\n",[17,522,523,529,533,542,546,558,576,582],{"__ignoreMap":82},[86,524,525,527],{"class":88,"line":89},[86,526,93],{"class":92},[86,528,97],{"class":96},[86,530,531],{"class":88,"line":100},[86,532,104],{"emptyLinePlaceholder":103},[86,534,535,537,539],{"class":88,"line":107},[86,536,110],{"class":96},[86,538,114],{"class":113},[86,540,541],{"class":120}," 25\n",[86,543,544],{"class":88,"line":124},[86,545,104],{"emptyLinePlaceholder":103},[86,547,548,550,552,554,556],{"class":88,"line":129},[86,549,132],{"class":92},[86,551,135],{"class":96},[86,553,138],{"class":113},[86,555,141],{"class":120},[86,557,145],{"class":144},[86,559,560,562,564,566,568,570,572,574],{"class":88,"line":148},[86,561,152],{"class":151},[86,563,155],{"class":144},[86,565,23],{"class":158},[86,567,161],{"class":144},[86,569,164],{"class":158},[86,571,155],{"class":144},[86,573,169],{"class":158},[86,575,172],{"class":144},[86,577,578,580],{"class":88,"line":175},[86,579,178],{"class":92},[86,581,145],{"class":144},[86,583,584,586,588,590,592,594],{"class":88,"line":183},[86,585,152],{"class":151},[86,587,155],{"class":144},[86,589,191],{"class":190},[86,591,195],{"class":194},[86,593,191],{"class":190},[86,595,200],{"class":144},[14,597,352],{},[77,599,601],{"className":79,"code":600,"language":81,"meta":82,"style":82},"5.0\n",[17,602,603],{"__ignoreMap":82},[86,604,605],{"class":88,"line":89},[86,606,600],{"class":120},[14,608,609],{},"This is the safest approach when you know the required range.",[14,611,612,613,161],{},"For more on square roots, see ",[295,614,616],{"href":615},"\u002Fstandard-library\u002Fmath.sqrt-function-explained","math.sqrt() explained",[308,618,620],{"id":619},"fix-2-validate-values-for-logarithms","Fix 2: Validate values for logarithms",[14,622,623,625,626,628,629,161],{},[17,624,41],{}," and ",[17,627,46],{}," require a value greater than ",[17,630,238],{},[77,632,634],{"className":79,"code":633,"language":81,"meta":82,"style":82},"import math\n\nx = 10\n\nif x > 0:\n    print(math.log(x))\nelse:\n    print(\"x must be greater than 0\")\n",[17,635,636,642,646,655,659,672,690,696],{"__ignoreMap":82},[86,637,638,640],{"class":88,"line":89},[86,639,93],{"class":92},[86,641,97],{"class":96},[86,643,644],{"class":88,"line":100},[86,645,104],{"emptyLinePlaceholder":103},[86,647,648,650,652],{"class":88,"line":107},[86,649,110],{"class":96},[86,651,114],{"class":113},[86,653,654],{"class":120}," 10\n",[86,656,657],{"class":88,"line":124},[86,658,104],{"emptyLinePlaceholder":103},[86,660,661,663,665,668,670],{"class":88,"line":129},[86,662,132],{"class":92},[86,664,135],{"class":96},[86,666,667],{"class":113},">",[86,669,141],{"class":120},[86,671,145],{"class":144},[86,673,674,676,678,680,682,684,686,688],{"class":88,"line":148},[86,675,152],{"class":151},[86,677,155],{"class":144},[86,679,23],{"class":158},[86,681,161],{"class":144},[86,683,402],{"class":158},[86,685,155],{"class":144},[86,687,169],{"class":158},[86,689,172],{"class":144},[86,691,692,694],{"class":88,"line":175},[86,693,178],{"class":92},[86,695,145],{"class":144},[86,697,698,700,702,704,707,709],{"class":88,"line":183},[86,699,152],{"class":151},[86,701,155],{"class":144},[86,703,191],{"class":190},[86,705,706],{"class":194},"x must be greater than 0",[86,708,191],{"class":190},[86,710,200],{"class":144},[14,712,713,714,716,717,719,720,161],{},"If ",[17,715,169],{}," is ",[17,718,238],{}," or negative, do not call ",[17,721,722],{},"math.log(x)",[308,724,726],{"id":725},"fix-3-validate-inverse-trig-input","Fix 3: Validate inverse trig input",[14,728,729,625,731,733,734,244,736,161],{},[17,730,51],{},[17,732,56],{}," only accept values from ",[17,735,69],{},[17,737,247],{},[77,739,741],{"className":79,"code":740,"language":81,"meta":82,"style":82},"import math\n\nx = 0.5\n\nif -1 \u003C= x \u003C= 1:\n    print(math.acos(x))\nelse:\n    print(\"x must be between -1 and 1\")\n",[17,742,743,749,753,762,766,787,805,811],{"__ignoreMap":82},[86,744,745,747],{"class":88,"line":89},[86,746,93],{"class":92},[86,748,97],{"class":96},[86,750,751],{"class":88,"line":100},[86,752,104],{"emptyLinePlaceholder":103},[86,754,755,757,759],{"class":88,"line":107},[86,756,110],{"class":96},[86,758,114],{"class":113},[86,760,761],{"class":120}," 0.5\n",[86,763,764],{"class":88,"line":124},[86,765,104],{"emptyLinePlaceholder":103},[86,767,768,770,772,774,777,779,782,785],{"class":88,"line":129},[86,769,132],{"class":92},[86,771,117],{"class":113},[86,773,247],{"class":120},[86,775,776],{"class":113}," \u003C=",[86,778,135],{"class":96},[86,780,781],{"class":113},"\u003C=",[86,783,784],{"class":120}," 1",[86,786,145],{"class":144},[86,788,789,791,793,795,797,799,801,803],{"class":88,"line":148},[86,790,152],{"class":151},[86,792,155],{"class":144},[86,794,23],{"class":158},[86,796,161],{"class":144},[86,798,456],{"class":158},[86,800,155],{"class":144},[86,802,169],{"class":158},[86,804,172],{"class":144},[86,806,807,809],{"class":88,"line":175},[86,808,178],{"class":92},[86,810,145],{"class":144},[86,812,813,815,817,819,822,824],{"class":88,"line":183},[86,814,152],{"class":151},[86,816,155],{"class":144},[86,818,191],{"class":190},[86,820,821],{"class":194},"x must be between -1 and 1",[86,823,191],{"class":190},[86,825,200],{"class":144},[308,827,829],{"id":828},"fix-4-validate-user-input","Fix 4: Validate user input",[14,831,832],{},"A bad value often comes from input entered by a user.",[77,834,836],{"className":79,"code":835,"language":81,"meta":82,"style":82},"import math\n\ntext = input(\"Enter a number: \")\nx = float(text)\n\nif x >= 0:\n    print(math.sqrt(x))\nelse:\n    print(\"Please enter a number that is 0 or greater\")\n",[17,837,838,844,848,869,885,889,901,919,925],{"__ignoreMap":82},[86,839,840,842],{"class":88,"line":89},[86,841,93],{"class":92},[86,843,97],{"class":96},[86,845,846],{"class":88,"line":100},[86,847,104],{"emptyLinePlaceholder":103},[86,849,850,853,855,858,860,862,865,867],{"class":88,"line":107},[86,851,852],{"class":96},"text ",[86,854,114],{"class":113},[86,856,857],{"class":151}," input",[86,859,155],{"class":144},[86,861,191],{"class":190},[86,863,864],{"class":194},"Enter a number: ",[86,866,191],{"class":190},[86,868,200],{"class":144},[86,870,871,873,875,878,880,883],{"class":88,"line":124},[86,872,110],{"class":96},[86,874,114],{"class":113},[86,876,877],{"class":362}," float",[86,879,155],{"class":144},[86,881,882],{"class":158},"text",[86,884,200],{"class":144},[86,886,887],{"class":88,"line":129},[86,888,104],{"emptyLinePlaceholder":103},[86,890,891,893,895,897,899],{"class":88,"line":148},[86,892,132],{"class":92},[86,894,135],{"class":96},[86,896,138],{"class":113},[86,898,141],{"class":120},[86,900,145],{"class":144},[86,902,903,905,907,909,911,913,915,917],{"class":88,"line":175},[86,904,152],{"class":151},[86,906,155],{"class":144},[86,908,23],{"class":158},[86,910,161],{"class":144},[86,912,164],{"class":158},[86,914,155],{"class":144},[86,916,169],{"class":158},[86,918,172],{"class":144},[86,920,921,923],{"class":88,"line":183},[86,922,178],{"class":92},[86,924,145],{"class":144},[86,926,928,930,932,934,937,939],{"class":88,"line":927},9,[86,929,152],{"class":151},[86,931,155],{"class":144},[86,933,191],{"class":190},[86,935,936],{"class":194},"Please enter a number that is 0 or greater",[86,938,191],{"class":190},[86,940,200],{"class":144},[14,942,943,944,946],{},"This prevents calling ",[17,945,36],{}," with an invalid number.",[308,948,950,951],{"id":949},"fix-5-use-try-except","Fix 5: Use ",[17,952,503],{},[14,954,955],{},"If invalid values are possible and you want your program to continue running, catch the error.",[77,957,959],{"className":79,"code":958,"language":81,"meta":82,"style":82},"import math\n\nx = -4\n\ntry:\n    print(math.sqrt(x))\nexcept ValueError:\n    print(\"Invalid value for math.sqrt()\")\n",[17,960,961,967,971,982,986,993,1011,1021],{"__ignoreMap":82},[86,962,963,965],{"class":88,"line":89},[86,964,93],{"class":92},[86,966,97],{"class":96},[86,968,969],{"class":88,"line":100},[86,970,104],{"emptyLinePlaceholder":103},[86,972,973,975,977,979],{"class":88,"line":107},[86,974,110],{"class":96},[86,976,114],{"class":113},[86,978,117],{"class":113},[86,980,981],{"class":120},"4\n",[86,983,984],{"class":88,"line":124},[86,985,104],{"emptyLinePlaceholder":103},[86,987,988,991],{"class":88,"line":129},[86,989,990],{"class":92},"try",[86,992,145],{"class":144},[86,994,995,997,999,1001,1003,1005,1007,1009],{"class":88,"line":148},[86,996,152],{"class":151},[86,998,155],{"class":144},[86,1000,23],{"class":158},[86,1002,161],{"class":144},[86,1004,164],{"class":158},[86,1006,155],{"class":144},[86,1008,169],{"class":158},[86,1010,172],{"class":144},[86,1012,1013,1016,1019],{"class":88,"line":175},[86,1014,1015],{"class":92},"except",[86,1017,1018],{"class":362}," ValueError",[86,1020,145],{"class":144},[86,1022,1023,1025,1027,1029,1032,1034],{"class":88,"line":183},[86,1024,152],{"class":151},[86,1026,155],{"class":144},[86,1028,191],{"class":190},[86,1030,1031],{"class":194},"Invalid value for math.sqrt()",[86,1033,191],{"class":190},[86,1035,200],{"class":144},[14,1037,1038],{},"This is useful when values come from calculations or unpredictable input.",[14,1040,1041,1042,161],{},"If you want to learn this pattern, read ",[295,1043,1045],{"href":1044},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[308,1047,1049,1050,1052],{"id":1048},"fix-6-use-cmath-for-complex-results","Fix 6: Use ",[17,1051,509],{}," for complex results",[14,1054,1055,1056,1058,1059,161],{},"The ",[17,1057,23],{}," module works with real numbers. If you want the square root of a negative number, use ",[17,1060,509],{},[77,1062,1064],{"className":79,"code":1063,"language":81,"meta":82,"style":82},"import cmath\n\nprint(cmath.sqrt(-1))\n",[17,1065,1066,1073,1077],{"__ignoreMap":82},[86,1067,1068,1070],{"class":88,"line":89},[86,1069,93],{"class":92},[86,1071,1072],{"class":96}," cmath\n",[86,1074,1075],{"class":88,"line":100},[86,1076,104],{"emptyLinePlaceholder":103},[86,1078,1079,1081,1083,1085,1087,1089,1091,1093,1095],{"class":88,"line":107},[86,1080,332],{"class":151},[86,1082,155],{"class":144},[86,1084,509],{"class":158},[86,1086,161],{"class":144},[86,1088,164],{"class":158},[86,1090,155],{"class":144},[86,1092,345],{"class":113},[86,1094,247],{"class":120},[86,1096,172],{"class":144},[14,1098,352],{},[77,1100,1102],{"className":79,"code":1101,"language":81,"meta":82,"style":82},"1j\n",[17,1103,1104],{"__ignoreMap":82},[86,1105,1106,1108],{"class":88,"line":89},[86,1107,247],{"class":120},[86,1109,1111],{"class":1110},"sbsja","j\n",[14,1113,1114,1115,161],{},"If you are working with standard math functions, see the ",[295,1116,1118],{"href":1117},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F","Python math module overview",[72,1120,1122],{"id":1121},"common-valid-ranges-to-remember","Common valid ranges to remember",[14,1124,1125],{},"These are the most common valid ranges that cause this error:",[29,1127,1128,1140,1149,1158,1170],{},[32,1129,1130,1133,1134,1136,1137,1139],{},[17,1131,1132],{},"math.sqrt(x)",": ",[17,1135,169],{}," must be ",[17,1138,238],{}," or greater",[32,1141,1142,1133,1144,1146,1147],{},[17,1143,722],{},[17,1145,169],{}," must be greater than ",[17,1148,238],{},[32,1150,1151,1133,1154,1146,1156],{},[17,1152,1153],{},"math.log10(x)",[17,1155,169],{},[17,1157,238],{},[32,1159,1160,1133,1163,1165,1166,625,1168],{},[17,1161,1162],{},"math.acos(x)",[17,1164,169],{}," must be between ",[17,1167,69],{},[17,1169,247],{},[32,1171,1172,1133,1175,1165,1177,625,1179],{},[17,1173,1174],{},"math.asin(x)",[17,1176,169],{},[17,1178,69],{},[17,1180,247],{},[14,1182,1183],{},"If you are not sure about a function, check its documentation before using it.",[72,1185,1187],{"id":1186},"debugging-steps","Debugging steps",[14,1189,1190],{},"If you are not sure where the error is coming from, try these steps:",[29,1192,1193,1196,1199,1202,1205],{},[32,1194,1195],{},"Print the value before the math function runs",[32,1197,1198],{},"Check which math function caused the error",[32,1200,1201],{},"Read the function's allowed input range",[32,1203,1204],{},"Trace where the bad value came from",[32,1206,1207],{},"Add validation close to the input or calculation",[14,1209,1210],{},"Useful checks:",[77,1212,1214],{"className":79,"code":1213,"language":81,"meta":82,"style":82},"print(x)\nprint(type(x))\n",[17,1215,1216,1226],{"__ignoreMap":82},[86,1217,1218,1220,1222,1224],{"class":88,"line":89},[86,1219,332],{"class":151},[86,1221,155],{"class":144},[86,1223,169],{"class":158},[86,1225,200],{"class":144},[86,1227,1228,1230,1232,1235,1237,1239],{"class":88,"line":100},[86,1229,332],{"class":151},[86,1231,155],{"class":144},[86,1233,1234],{"class":362},"type",[86,1236,155],{"class":144},[86,1238,169],{"class":158},[86,1240,172],{"class":144},[14,1242,1243],{},"You can also inspect help for the function:",[77,1245,1247],{"className":79,"code":1246,"language":81,"meta":82,"style":82},"import math\nprint(help(math.sqrt))\nprint(help(math.log))\nprint(help(math.acos))\n",[17,1248,1249,1255,1275,1293],{"__ignoreMap":82},[86,1250,1251,1253],{"class":88,"line":89},[86,1252,93],{"class":92},[86,1254,97],{"class":96},[86,1256,1257,1259,1261,1264,1266,1268,1270,1273],{"class":88,"line":100},[86,1258,332],{"class":151},[86,1260,155],{"class":144},[86,1262,1263],{"class":151},"help",[86,1265,155],{"class":144},[86,1267,23],{"class":158},[86,1269,161],{"class":144},[86,1271,164],{"class":1272},"skxfh",[86,1274,172],{"class":144},[86,1276,1277,1279,1281,1283,1285,1287,1289,1291],{"class":88,"line":107},[86,1278,332],{"class":151},[86,1280,155],{"class":144},[86,1282,1263],{"class":151},[86,1284,155],{"class":144},[86,1286,23],{"class":158},[86,1288,161],{"class":144},[86,1290,402],{"class":1272},[86,1292,172],{"class":144},[86,1294,1295,1297,1299,1301,1303,1305,1307,1309],{"class":88,"line":124},[86,1296,332],{"class":151},[86,1298,155],{"class":144},[86,1300,1263],{"class":151},[86,1302,155],{"class":144},[86,1304,23],{"class":158},[86,1306,161],{"class":144},[86,1308,456],{"class":1272},[86,1310,172],{"class":144},[14,1312,1313],{},"This can help you confirm what values are allowed.",[72,1315,1317],{"id":1316},"common-mistakes","Common mistakes",[14,1319,1320],{},"These are the most common causes of this error:",[29,1322,1323,1328,1334,1338,1349,1352],{},[32,1324,1325,1326,265],{},"Calling ",[17,1327,36],{},[32,1329,1325,1330,272,1332],{},[17,1331,41],{},[17,1333,238],{},[32,1335,1325,1336,265],{},[17,1337,41],{},[32,1339,1325,1340,206,1342,1344,1345,244,1347],{},[17,1341,51],{},[17,1343,56],{}," with a value outside ",[17,1346,69],{},[17,1348,247],{},[32,1350,1351],{},"Using unexpected user input",[32,1353,1354],{},"Passing a value from an earlier calculation that turned out to be invalid",[14,1356,1357,1358,1360,1361,161],{},"A related point: this is still a ",[17,1359,363],{},", but it has a specific message. If you want a broader explanation, see ",[295,1362,1364],{"href":1363},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[72,1366,1368],{"id":1367},"faq","FAQ",[308,1370,1372],{"id":1371},"why-do-i-get-valueerror-math-domain-error-in-python","Why do I get ValueError: math domain error in Python?",[14,1374,1375],{},"You passed a value to a math function that is outside its allowed range.",[308,1377,1379,1380,1382],{"id":1378},"why-does-mathsqrt-1-fail","Why does ",[17,1381,62],{}," fail?",[14,1384,1055,1385,1387,1388,161],{},[17,1386,23],{}," module only works with real-number square roots. Negative numbers are not valid for ",[17,1389,36],{},[308,1391,1393,1394,1396],{"id":1392},"how-do-i-fix-mathlog0","How do I fix ",[17,1395,209],{},"?",[14,1398,1399,1400,1402,1403,161],{},"Make sure the value is greater than ",[17,1401,238],{}," before calling ",[17,1404,41],{},[308,1406,1408],{"id":1407},"can-i-calculate-the-square-root-of-a-negative-number-in-python","Can I calculate the square root of a negative number in Python?",[14,1410,1411,1412,1414],{},"Yes. Use the ",[17,1413,509],{}," module if you want a complex-number result.",[72,1416,1418],{"id":1417},"see-also","See also",[29,1420,1421,1425,1430,1434,1439],{},[32,1422,1423],{},[295,1424,1118],{"href":1117},[32,1426,1427],{},[295,1428,1429],{"href":615},"math.sqrt() function explained",[32,1431,1432],{},[295,1433,1364],{"href":1363},[32,1435,1436],{},[295,1437,1438],{"href":1044},"How to handle exceptions in Python",[32,1440,1441],{},[295,1442,298],{"href":297},[1444,1445,1446],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":82,"searchDepth":100,"depth":100,"links":1448},[1449,1450,1451,1452,1457,1467,1468,1469,1470,1478],{"id":74,"depth":100,"text":75},{"id":212,"depth":100,"text":213},{"id":253,"depth":100,"text":254},{"id":302,"depth":100,"text":303,"children":1453},[1454,1455,1456],{"id":310,"depth":107,"text":62},{"id":372,"depth":107,"text":209},{"id":425,"depth":107,"text":428},{"id":480,"depth":100,"text":481,"children":1458},[1459,1460,1461,1462,1463,1465],{"id":516,"depth":107,"text":517},{"id":619,"depth":107,"text":620},{"id":725,"depth":107,"text":726},{"id":828,"depth":107,"text":829},{"id":949,"depth":107,"text":1464},"Fix 5: Use try-except",{"id":1048,"depth":107,"text":1466},"Fix 6: Use cmath for complex results",{"id":1121,"depth":100,"text":1122},{"id":1186,"depth":100,"text":1187},{"id":1316,"depth":100,"text":1317},{"id":1367,"depth":100,"text":1368,"children":1471},[1472,1473,1475,1477],{"id":1371,"depth":107,"text":1372},{"id":1378,"depth":107,"text":1474},"Why does math.sqrt(-1) fail?",{"id":1392,"depth":107,"text":1476},"How do I fix math.log(0)?",{"id":1407,"depth":107,"text":1408},{"id":1417,"depth":100,"text":1418},"Master valueerror math domain error fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fvalueerror-math-domain-error-fix",{"title":5,"description":1479},"errors\u002Fvalueerror-math-domain-error-fix","9qiEJQQJgP4Nn3C_LFWu1cf_y7JMZtjkuEFAGkF24RM",1777585502623]