python logarithm - python

i want to find out a log10 of an integer in python and i get an error like
math domain error
my code is this
w=math.log10(q*q1)/math.log10(2)
where q1,q2 are integers
yeah q1 is 0 sometimes

You can only compute the logarithm of a positive number. Trying to compute the logarithm for a negative number or zero will result in a "math domain error" in Python.
By the way: it looks like you're actually trying to compute a logarithm base 2. You can do this with math.log:
w=math.log(q*q1, 2)
The second, optional, parameter is the base. It defaults to e (ie: natural log).

Is q or q1 equal to zero or one of them negative?

math.log10(0) is minus infinity.
See: http://en.wikipedia.org/wiki/Logarithm

try to make sure the value whose log you are trying to find can never be 0. As log(0) tends to negative infinity, the function call will give you a math domain error. Correct that and I think you'll be fine.

Related

How can I accurately calculate a mathematical expression in Python?

(Sorry for my poor English:)
I know that I can use eval to calculate the result of a mathematical expression.
However, this is not accurate(e.g: 0.3-0.2 != 0.1). After searching, I found I can use Fraction or some other methods to calculate.
But I didn't find out how to use these methods to directly calculate a string expression. For example, Fraction('239/3289') is correct but I can't use Fraction('(239/3289+392)/(12+993)').
Is there a simple way to accurately calculate an mathematical expression?
edit: Actually I'm just wondering how to parse such a formula and return the result in Fraction instead of splitting the string and using Fraction(Fraction('239/3289') + 392, 12 + 993)...Does someone know how to do it?
Try math.isclose and cmath.isclose as suggested here
The argument to Fraction is not an arbitrary expression, it's just for creating one fraction. You can then use that like a number in a more general expression.
Fraction(Fraction('239/3289') + 392, 12 + 992)

How to estimate how big one number is from another?

I'm trying to find an efficient solution to this problem. I receive two numbers from an API call (we can call them n1, n2). Suppose n2 is bigger than n1. I want to know how much bigger n2 is. The difference between the two is not enough because I don't know how to evaluate the result of the subtraction. I don't see any other solution but to define a tolerance range that fits my domain of application and check if their difference falls within that range. Any idea?
Why don't you just take the division?
Suppose n1 is 1 and n2 is 10.
If you divide n2 by n1 you will see that n1 is 10 times bigger than n2.
It's a common problem when using floating-point, that results can be almost equal and you want to treat them as if they were equal.
You need a formula to measure the difference between the numbers relative to the size of the numbers, and it's remarkably tricky to find. One problem is avoiding division by zero. As always, first check the libraries: in this case, the math module includes math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
So let the mathematics experts handle it for you:
import math
if math.isclose(a,b):
# they agree to around nine decimal places

Why get different answer between tan(pi) on calculator?

The tan(pi) is 0, but When I calculate tan(math.pi) with Python. Answer is -1.2246467991473532e-16.
I can't comprehend.
e-16 means * 10^-16, quite close to zero (not perfectly, partly because pi was not perfect). On the calculator, it is just rounded to zero.
The Python answer is equal to zero to 10 significant digits.
-0.00000000000000012246467991473532
xxxxxxxxxxxyyy
Calculators tend to use about 13 digits internally for their calculations, but only 10 for display. Either way, this value is 0 by the calculator's standards.

Getting PyEphem to return angles with sign

How do I get PyEphem to give angles with sign and a zero padded to degrees. Currently it returns 2:46:32.8 but I want it in the form +02:46:32.8.
Now, I could define a function to return it in that form, but I was wondering if there was a simpler way.
No, the underlying C code inside of libastro does not support leading plus signs or leading zeros. You will have to write a little Python function of your own to add them.

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. I'm running Python 3.3, and it's finding the problem at the last line. How do I fix this? (Also, I don't want to hear anything about my overuse of parentheses. It is my preference for there to be this many.):
import math
a=float(input('a=?'))
b=float(input('b=?'))
c=float(input('c=?'))
d=float(input('d=?'))
critical_point_n=((-2*b)-math.sqrt(abs((4*(math.pow(b, 2)))-(12*a*c))))/(6*a)
first_root=critical_point_n-1
if first_root==0 and c==0:
first_root+=(-0.01)
for x in range(10):
first_root=first_root-((a*(math.pow(first_root, 3)))+(b*(math.pow(first_root, 2))+(c*first_root)+d)/(3*(a*(math.pow(first_root, 2))))+(2*(b*first_root))+c)
I know you don't want to hear about your excessive use of parenthesis, but the problem is that you have the parenthesis in the wrong places. With the sheer number of parenthesis you used, it took a while to find the problem.
I think the following code is much cleaner, easier to debug, and vastly easier to maintain in the future. I also included what I think is the corrected version of your one-liner.
import math
a=float(input('a=?'))
b=float(input('b=?'))
c=float(input('c=?'))
d=float(input('d=?'))
critical_point_n=((-2*b)-math.sqrt(abs((4*(math.pow(b, 2)))-(12*a*c))))/(6*a)
first_root=critical_point_n-1
if first_root==0 and c==0:
first_root+=(-0.01)
for x in range(10):
f = a*first_root**3 + b*first_root**2 + c*first_root + d
fp = 3*a*first_root**2 + 2*b*first_root + c
first_root = first_root - (f/fp)
#first_root=first_root-(((a*(math.pow(first_root, 3)))+(b*(math.pow(first_root, 2))+(c*first_root)+d)))/((3*(a*(math.pow(first_root, 2))))+(2*(b*first_root))+c)
print(first_root)
You are overflowing the internal representation of floats. Use sys.float_info to check your system's limits for floating point numbers. http://docs.python.org/3.3/library/sys.html#sys.float_info
I recommend trying out your operations by "hand" on wolframalpha to see the magnitude of the actual values. http://www.wolframalpha.com/
The math range of functions work on doubles... so you're out of range for that - re-write as normal Python floats which will scale as needs be, or look at using decimal.Decimal which also has sqrt, power etc.. functions: http://docs.python.org/2/library/decimal.html

Categories