I have the following line of code in my program:
theta=(180/math.pi)*0.5*math.asin((9.8*dist)/(vel**2))
when I use the above code after import math it gives me the following math domain error:
Traceback (most recent call last):
File "traj.py", line 36, in <module>
processCase(caseNumber,V,D)
File "traj.py", line 20, in processCase
theta=(180/math.pi)*0.5*math.asin((9.8*dist)/(vel**2))
ValueError: math domain error
the input was:
vel= 119 dist= 1445
What is causing this error. When I use import cmath, the error disappears but I get a complex number as output. Why is that?
Due to floating point errors rounding (9.8*dist)/(vel**2) to values above 1.0, the asin function gives a domain error.
You can workaround it by limiting the number in the math.asin call to a maximum of 1.0, regardless of rounding. You could use Decimalarithmetic to do it in the a proper "mathematic" way, but with a huge impact in performance and complexity.
My advice is to simply put a call to min in the asin call:
theta=(180/math.pi)*0.5*math.asin(min(1.0, ((9.8*dist)/(vel**2)) )
Here is the error math.asin() it gives math domain error. it takes as math.asin(1.0000000000000002)
Related
I need to get the ceiling of a square root, such as 10**10001 (10^10001). If I do:
from math import sqrt
print(int(sqrt(10**10001))+1)
I look, and it gives me an OverflowError.
More precisely, a
Traceback (most recent call last)
File <stdin>, line 1, in <module>
OverflowError: int too large to convert to float.
I need a integer for an answer. The OverflowError is coming from the math.sqrt(x) function.
If anybody can help, it would be appreciated.
the math module has a ceil and floor function
try print(math.ceil(math.sqrt(NUMBER)))
I've found the following definition in the docs but it's still not clear to me:
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
Do you have any examples?
Take a look at math#sqrt - it takes a number, but this number must be non-negative. If you try calling it with a negative number (which can't be done in real numbers' math), you'll get a ValueError:
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
An example that gives the specific ValueError is this:
import
math.log(0)
which gives back:
Traceback (most recent call last):
File "<ipython-input-6-f7278b7c2ed1>", line 1, in <module>
math.log(0)
ValueError: math domain error
Many other math function will produce the same when given bad inputs (math.sqrt(-1), math.ceil(math.nan) and so on...)
Here is what I am writing:
>>> import math
>>> 2/3*math.log(2/3,2)
Here is the error I'm getting:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
Can someone please explain what I'm doing wrong?
Thanks.
I'm assuming this is Python 2.7.
In 2.7, 2/3 evaluates to 0 since division floors by default. Therefore you're attempting a log 0, hence the error. Python 3 on the other hand does floating point division by default.
To get the correct behaviour you can either:
from __future__ import division, which gives you Python 3 division behaviour in Python 2.7.
Replace each 2/3 with 2/float(3), or 2/3.0.
The problem is that you are probably using python 2.7. In this version 2/3 gives as result 0 (zero). And log function is not defined for 0. Try this:
2/3.0*math.log(2/3.0,2)
In Python 3.5 this problem does not happen.
I'm trying to generate a random.gauss numbers but I have message error. Here is my code:
import sys,os
import numpy as np
from random import gauss
previous_value1=1018.163072765074389
previous_value2=0.004264112033664
alea_var_n=random.gauss(1,2)
alea_var_tau=random.gauss(1,2)
new_var_n= previous_value1*(1.0+alea_var_n)
new_var_tau=previous_value2*(1.0+alea_var_tau)
print 'new_var_n',new_var_n
print 'new_var_tau',new_var_tau
I got this error:
Traceback (most recent call last):
File "lolo.py", line 15, in <module>
alea_var_n=random.gauss(1,2)
AttributeError: 'builtin_function_or_method' object has no attribute 'gauss'
Someone know what's wrong, I'm a newbye with python. Or is it a numpy version problem.
For a faster option, see Benjamin Bannier's solution (which I gave a +1 to). Your present code that you posted will not work for the following reason: your import statement
from random import gauss
adds gauss to your namespace but not random. You need to do this instead:
alea_var_n = gauss(1, 2)
The error in your post, however, is not the error you should get when you run the code that you have posted above. Instead, you will get the following error:
NameError: name 'random' is not defined
Are you sure you have posted the code that generated that error? Or have you somehow included the wrong error in your post?
Justin Barber shows you an immediate solution for your problem.
Since you are using NumPy you could however use their generators as well since they appear to be significantly faster (about a factor 5-7 on my machine), e.g.
alea_var_n = np.random.normal(1, 2)
I am getting the error:
Warning: invalid value encountered in log
From Python and I believe the error is thrown by numpy (using version 1.5.0). However, since I am calling the "log" function in several places, I'm not sure where the error is coming from. Is there a way to get numpy to print the line number that generated this error?
I assume the warning is caused by taking the log of a number that is small enough to be rounded to 0 or smaller (negative). Is that right? What is the usual origin of these warnings?
Putting np.seterr(invalid='raise') in your code (before the errant log call)
will cause numpy to raise an exception instead of issuing a warning.
That will give you a traceback error message and tell you the line Python was executing when the error occurred.
If you have access to the numpy source, you should be able to find the line that prints that warning (using grep, etc) and edit the corresponding file to force an error (using an assertion, for example) when an invalid value is passed. That will give you a stack trace pointing to the place in your code that called log with the improper value.
I had a brief look in my numpy source, and couldn't find anything that matches the warning you described though (my version of numpy is older than yours, though).
>>> import numpy
>>> numpy.log(0)
-inf
>>> numpy.__version__
'1.3.0'
Is it possible that you're calling some other log function that isn't in numpy? For example, here is one that actually throws an exception when given invalid input.
>>> import math
>>> math.log(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error