What causes the problem?
Nội dung chính
- Python sqrt: Math domain error
- Python pow: Math domain error
- What is sqrt domain error?
- How do you get rid of domain error in math?
- What does math domain error mean?
- What does domain error mean in Python?
- How do you use math sqrt in Python?
from math import sqrt
print "a : "
a = float[raw_input[]]
print "b : "
b = float[raw_input[]]
print "c : "
c = float[raw_input[]]
d = [a + b + c]/2
s = sqrt[d*[d-a]*[d-b]*[d-c]]
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
Error:
Traceback [most recent call last]:
File "C:/Python27/fájlok/háromszög terület2.py", line 11, in
s = sqrt[d*[d-a]*[d-b]*[d-c]]
ValueError: math domain error
Bhargav Rao
47.3k27 gold badges122 silver badges137 bronze badges
asked Mar 31, 2015 at 18:36
2
The problem is that the Heron's formula holds good only when the sum of the two numbers are greater than the third. You need to check that explicitly.
A better way as you are using a code to do that is by using Exception handling
try:
s = sqrt[d*[d-a]*[d-b]*[d-c]]
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
except ValueError:
print "Please enter 3 valid sides"
If you want to do
it without try
block you can do it as
delta = [d*[d-a]*[d-b]*[d-c]]
if delta>0:
s = sqrt[delta]
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
else:
print "Please enter 3 valid sides"
answered Mar 31, 2015 at 18:40
Bhargav RaoBhargav Rao
47.3k27 gold badges122 silver badges137 bronze badges
2
sqrt
gives that error when you try to use it with a
negative number. sqrt[-4]
gives that error because the result is a complex number.
For that, you need cmath
:
>>> from cmath import sqrt
>>> sqrt[-4]
2j
>>> sqrt[4]
[2+0j]
answered Mar 31, 2015 at 18:46
aneroidaneroid
12.5k3 gold badges38 silver badges61 bronze badges
I got the same error with my code until I used cmath
instead of math
like aneroid said:
import sys
import random
import cmath
x = random.randint[1, 100]
y = random.randint[1, 100]
a = 2 * x * cmath.sqrt[1 - x * 2 - y * 2]
b = 2 * cmath.sqrt[1 - x * 2 - y * 2]
c = 1 - 2 * [x * 2 + y * 2]
print [ 'The point on the sphere is: ', [a, b, c] ]
This way ran my code properly.
aneroid
12.5k3 gold badges38 silver badges61 bronze badges
answered Dec 21, 2016 at 1:01
2
Use cmath instead..
import cmath
num=cmath.sqrt[your_number]
print[num]
Now regardless of whether the number is negetive or positive you will get a result...
answered Feb 18, 2018 at 15:49
4
You come across a special ValueError when working with Python’s math module. It is ValueError: math domain error. Python raises this error when you try to do something that is not mathematically possible or mathematically defined.
To solve a domain error in Python, passing a valid input for which the function can calculate a numerical output. The math domain error occurs when you’ve passed an undefined input into the math function. For example, you are doing a log of a number less than or equal to zero. That’s mathematically undefined, so Python’s log function raises an exception.
from math import log print[log[-1]]
Output
Traceback [most recent call last]: File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in print[log[-1]] ValueError: math domain error
And we get the ValueError: math domain error.
Whenever you are getting the math domain error for either reason, you are trying to use a negative number inside the log function or a zero value.
Logarithms decide the base after being given a number and the power it was raised to.
The log[0] means that something raised to the power of 2 is 0. An exponent can never result in 0*, which means that log[0] has no answer, thus throwing the math domain error.
The domain of a function is the set of all possible input values. If Python throws the ValueError: math domain error, you’ve passed an undefined input into the math function.
In our case, don’t calculate the log of a negative number or zero, and it will resolve the error.
There are various scenarios in which this error can occur. Let’s see some of them one by one.
Python sqrt: Math domain error
To calculate the square root of a number in Python, use math.sqrt[] method.
The math domain error appears if you pass a negative argument into the math.sqrt[] function.
It’s mathematically impossible to calculate the square root of a negative number without using complex numbers.
from math import sqrt print[sqrt[-1]]
Output
Traceback [most recent call last]: File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in print[sqrt[-1]] ValueError: math domain error
The square root of a negative number is not mathematically possible. That’s why it throws an error.
Python pow: Math domain error
The math domain error for the math.pow[a,b] function to calculate a**b arises if you pass the negative base value into it and try to calculate a negative power.
from math import pow print[pow[-1, 0.5]]
Output
Traceback [most recent call last]: File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in print[pow[-1, 0.5]] ValueError: math domain error
And we get the ValueError.
That is it for this tutorial.
See also
String Indices must be Integers in Python
Python expected an indented block Error
Python cannot import name
Return outside function Python
Python ValueError
What is sqrt domain error?
Domain errors occur when the parameters to the functions are invalid, such as a negative number as a parameter to sqrt [the square root function].
How do you get rid of domain error in math?
The ValueError: math domain error is raised when you perform a mathematical function on a negative or zero number which cannot be computed. To solve this error, make sure you are using a valid number for the mathematical function you are using.
What does math domain error mean?
A domain error occurs if an input argument is outside the domain over which the mathematical function is defined. Paragraph 3 states. A pole error [also known as a singularity or infinitary] occurs if the mathematical function has an exact infinite result as the finite input argument[s] are approached in the limit.
What does domain error mean in Python?
Python math domain error The math domain error occurs when you've passed an undefined input into the math function. For example, you are doing a log of a number less than or equal to zero. That's mathematically undefined, so Python's log function raises an exception. from math import log print[log[-1]]
How do you use math sqrt in Python?
sqrt[] function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt[x] Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.