I think if I'd know the math behind it I might have figured it out by myself.
When I call math.log10(0.0000000000001) I get -13. But how do I convert this back into 0.0000000000001? and how i'd handle it when using math.log(0.0000000000001)?
Both ways, the fundamental, arithmetic one and the one using a built-in python function would interest me.
a = math.log10(0.0000000000001)
b = 10 ** a
math.log() takes two parameter, a number and a base. Default value is e (mathematical constant), a special constant which tends to 2.71828.
You can get the number by evaluating base to the power log_value
>>> a = math.log(10)
>>> a
2.302585092994046
>>> 2.71828**a
9.999984511610785
If I would take e's value with higher precision, I can get the result close to 10.
Or, you can use math.exp()
>>>math.exp(a)
10.000000000000002
You can also use math.pow()
>>> math.pow(2.71828,a)
9.999984511610785
Related
I want to write a function that takes in 2 values and calculates whether the division is an integer, but i am having trouble handling the case when say 5.1 and 0.1 is entered as the result is not 51 as expected.
def(a,b):
return a/b.is_integer()
An alternative version i tried, to convert the values to decimals as well but still have the same issue.:
def(a,b):
return Decimal(a)%Decimal(b) == 0
This a problem of arithmetic precision in python (and other languages that use floating-point in computers), you could do one of two things in those case, the first case is assuming that it will be some limits and deal with it, or the second case is to use a specialized library that tries to deal with those problems.
A good compromise could be to use limited precision in your divisor, for example you could assume that any divisor b in your function could be a number with max 3 digits of precision, from like 5.999 or 0.001, then you could first multiply dividend a and divisor b by 1000 and try to do the division there
Let's rewrite your function (and assume you are using Python 3)
def is_division_integer(a, b):
a = 1000*a
b = 1000*b
d = a/b
if type(d) == int:
return True
return d.is_integer()
The other alternative could be using a library like numpy, mpmath or others but probably you don't need them for general simple cases.
so I am having issues trying to do basic math in Python. I can do basic math, but when I add in exponents, square roots, etc, I have errors with the IDE. How do I do this?
Here are a few of my problems that I am having issues with:
(n(n-1))/2)
(4)* pi * r ** 2=
(r(cos(a)**2) + r(sin(b))**2)**(.5)
((y**2) - (y**1))/((x**2) - (x**1))=
(n*(n-1))/2 should work if you have already given n a numeric value (e.g. n=2 or something). Your original expression has unbalanced parentheses (three closing parens and only two opening parens) and it is missing the multiplication sign (*) which is necessary in python, otherwise n(n-1) would be interpreted as the function n supplied with the input n-1, in which case you get a message like "TypeError: 'int' object is not callable", assuming you had previously defined n=2 or the like. It's telling you that the integer n cannot be called like a function, which is how it interprets n().
To get pi (3.14159...) in python, you should import the math package and then use math.pi like this:
import math
r = 2
x = 4*math.pi*r**2
You don't need parentheses around the 4. Parentheses are used for grouping, when you need operations to be done in a different order than the standard order of operations. You don't need the trailing equal sign (that's a syntax error).
In the third expression you are using implicit multiplication notation which is fine for pencil and paper but in python you need to use * every time you multiply. Also, you need to import the math package and then use math.sin and math.cos.
import math
a = 90
b = 45
x = (r*(math.cos(a)**2) + r*(math.sin(b))**2)**(.5)
There doesn't appear to be anything wrong with the last expression except the trailing = sign which should be removed. Store the result of this expression in a variable if you want to keep it for future use:
z = ((y**2) - (y**1))/((x**2) - (x**1))
If you just type the expression at the command line it will print the result immediately:
x = 3
y = 2
((y**2) - (y**1))/((x**2) - (x**1))
but if you are using this expression in a script you want to save the result in a variable so you can use it later or print it out or something:
z = ((y**2) - (y**1))/((x**2) - (x**1))
As was previously pointed out in the comments, x**1 is the same, mathematically, as x so it's not clear why you would want to write it this way, but it's not wrong.
You can use math.pow() or the simpler ** syntax:
There are two ways to complete basic maths equations in python either:
use the ** syntax. e.g:
>>> 2 ** 4
16
>>> 3 ** 3
27
or use math.pow(). e.g:
>>> import math
>>> math.pow(5, 2)
25.0
>>> math.pow(36, 0.5)
6.0
As you can see, with both of these functions you can use any real power so negative for inverse or decimals for roots.
In general, for these types of equations, you want to look into the math module. It has lost of useful functions and defined constants that you may find useful. In particular for your specific problems: math.pi and these trig. functions.
I hope these examples and the links I made are useful for you :)
This question already has answers here:
Python cos(90) and cos(270) not 0
(3 answers)
Closed 9 years ago.
Is there a way to get the exact Tangent/Cosine/Sine of an angle (in radians)?
math.tan()/math.sin()/math.cos() does not give the exact for some angles:
>>> from math import *
>>> from decimal import Decimal
>>> sin(pi) # should be 0
1.2246467991473532e-16
>>> sin(2*pi) # should be 0
-2.4492935982947064e-16
>>> cos(pi/2) # should be 0
6.123233995736766e-17
>>> cos(3*pi/2) # 0
-1.8369701987210297e-16
>>> tan(pi/2) # invalid; tan(pi/2) is undefined
1.633123935319537e+16
>>> tan(3*pi/2) # also undefined
5443746451065123.0
>>> tan(2*pi) # 0
-2.4492935982947064e-16
>>> tan(pi) # 0
-1.2246467991473532e-16
I tried using Decimal(), but this does not help either:
>>> tan(Decimal(pi)*2)
-2.4492935982947064e-16
numpy.sin(x) and the other trigonometric functions also have the same issue.
Alternatively, I could always create a new function with a dictionary of values such as:
def new_sin(x):
sin_values = {math.pi: 0, 2*math.pi: 0}
return sin_values[x] if x in sin_values.keys() else math.sin(x)
However, this seems like a cheap way to get around it. Is there any other way? Thanks!
It is impossible to store the exact numerical value of pi in a computer. math.pi is the closest approximation to pi that can be stored in a Python float. math.sin(math.pi) returns the correct result for the approximate input.
To avoid this, you need to use a library that supports symbolic arithmetic. For example, with sympy:
>>> from sympy import *
>>> sin(pi)
0
>>> pi
pi
>>>
sympy will operate on an object that represents pi and can give exact results.
When you're dealing with inexact numbers, you need to deal with error explicitly. math.pi (or numpy.pi) isn't exactly π, it's, e.g., the closest binary rational number in 56 digits to π. And the sin of that number is not 0.
But it is very close to 0. And likewise, tan(pi/2) is not infinity (or NaN), but huge, and asin(1)/pi is very close to 0.5.
So, even if the algorithms were somehow exact, the results still wouldn't be exact.
If you've never read What Every Computer Scientist Should Know About Floating-Point Arithmetic, you should do so now.
The way to deal with this is to use epsilon-comparisons rather than exact comparisons everywhere, and explicitly round things when printing them out, and so on.
Using decimal.Decimal numbers instead of float numbers makes this easier. First, you probably think in decimal rather than binary, so it's easier for you to understand and make decisions about the error. Second, you can explicitly set precision and other context information on Decimal values, while float are always IEEE double values.
The right way to do it is to do full error analysis on your algorithms, propagate the errors appropriately, and use that information where it's needed. The simple way is to just pick some explicit absolute or relative epsilon (and the equivalent for infinity) that's "good enough" for your application, and use that everywhere. (You'll probably also want to use the appropriate domain-specific knowledge to treat some values as multiples of pi instead of just raw values.)
Looking to format a output to show a very small number..
a= 6500
b= 3600
c=.000900
d= int((b/a)-c)
print d
Answer comes out to be zero, but looking for the .###### numbers after the .
sorry try this
Looks like you got bitten by integer division in python 2.x.
>>> a= 6500
>>> b= 3600
>>> c=.000900
>>> from __future__ import division
>>> print b/a - c
0.552946153846
There may be two issues with your calculation:
If you're using Python 2, division using / between two integers will always be an integer also. This will discard the fractional part of the calculation, which can be unexpected. One fix for this is to force one of the values to be a float value (e.g. float(b)/a). An alternative is to put at the top of your file from __future__ import division, which tells Python to use the Python 3 semantics for division (you'll only get an integer from integer division if the result is an exact integer).
The second issue seems to be about formatting a floating point value. First you seem to want only the fractional part of the value (not the integer part). Usually the best option is to simply subtract the integer part of the value:
d = someFloatingPointValue()
d_frac = d - int(d)
Next, to get a string to output (where you want several decimal places, but not a huge number of them), you probably want to explore Python's string formatting operations:
d = someFloatingPointValue()
d_to4decimalPlaces = "{:.4f}".format(d)
So far, I would do int(float('3.5'))
Any other good way to do?
Note: 3.5 is a string.
I want to use the the built-in API that specify for this sort of problem.
You're on the right track, and the best solution is probably as mentioned:
>>> int(float("3.5"))
This truncates the float.
If you want a different type of rounding, you can use the math package:
>>> import math
>>> x = "3.5"
>>> math.floor(float(x)) # returns FP; still needs to be wrapped in int()
3.0
>>> math.ceil(float(x)) # same
4.0
>>> math.trunc(float(x)) # returns an int; essentially the same as int(float(x))
3
If on the other hand you wish to round the number to the nearest integer, you may use the floating-point built-in operation round before converting to an integer, e.g.
>>> int(round(float(x))) # 3.5 => 4
4
>>> int(round(3.4999))
3
The only code which could possibly be simpler and clearer than what you have is int('3.5'), which doesn't work. Therefore, what you have is the simplest, clearest working code.
All that you need is
int(3.5)
Note that this truncates; it doesn't round.
Maybe int(eval('3.5'))