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)))
Related
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.
Here are two lines of code for the purpose of generating a random permutation of size 4:
from numpy import random
t = random.permutation(4)
This can be executed in Python, but not sage, which gives the following error:
TypeError Traceback (most recent call last)
<ipython-input-3-033ef4665637> in <module>()
1 from numpy import random
----> 2 t = random.permutation(Integer(4))
mtrand.pyx in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:34842)()
mtrand.pyx in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:33796)()
TypeError: len() of unsized object
Why?
A bit more details: I executed the code in Python 3, and the mtrand is also in the Python 3 directory, which should rule out the possibility that sage is calling Python 2 version of the numpy.
To escape Sage's preparser, you can also append the letter r (for "raw") to numerical input.
from numpy import random
t = random.permutation(4r)
The advantage of 4r over int(4) is that 4r bypasses the
preparser, while int(4) is preparsed as int(Integer(4)) so that
the Python integer is transformed into a Sage integer and then
transformed back into a Python integer.
In the same way, 1.5r will give you a pure Python float rather than
a Sage "real number".
The reason this doesn't work in Sage is that Sage preparses its input, turning "4" from a Python int to a Sage Integer. In Sage, this will work:
from numpy import random
t = random.permutation(int(4))
Or you can turn the preparser off:
preparser(False)
t = random.permutation(4)
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)
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