What is a ValueError - python

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...)

Related

Why while loop keep asking to define a variable even if it's already defined?

I'm practicing loops in VsCode and watching basic tutorials. I decided to run a very simple code that looks like this
x = 1
while x <= 100:
print(x)
x = x + 1
However I keep getting this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
According to all tutorials I saw it should work. But it doesn't. What could be the problem here?
========================================================================

Get ceiling of sqrt without float point. Python 3.7.3

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)))

looping on array throws: ValueError: need more than 1 value to unpack

Edit: Issue was with environment. Terminal restart made things work again.
I was given an assignment, write a function get_name(names) , call it with [{"name":"one"},{"name":"two"}] and do an action with the name.
My code is:
def get_name(names):
for name in names:
print name
and when I call it with
get_name([{"name":"one"},{"name":"two"}])
I get an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 2, in get_name
for name in names:
ValueError: need more than 1 value to unpack
I read all links regarding this error on the first 5 google result pages and a few more directly on stackoverflow - none give a hind to the issue I'm having.
Which version of python are you using?
I have python 3.6.4 and when i try exactly your code i have
{'name': 'one'}
{'name': 'two'}
as output

Math Domain Error math vs. cmath different behavior

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)

Tracing Python warnings/errors to a line number in numpy and scipy

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

Categories

Resources