You can use exp(x) function of math library, which is same as e^x. Hence you may write your code as:
import math
x.append(1 - math.exp( -0.5 * (value1*value2)**2))
I have modified the equation by replacing 1/2 as 0.5. Else for Python <2.7, we'll have to explicitly type cast the division value to float because Python round of the result of division of two int as integer. For example: 1/2 gives 0 in python 2.7 and below.
Videos
You can use exp(x) function of math library, which is same as e^x. Hence you may write your code as:
import math
x.append(1 - math.exp( -0.5 * (value1*value2)**2))
I have modified the equation by replacing 1/2 as 0.5. Else for Python <2.7, we'll have to explicitly type cast the division value to float because Python round of the result of division of two int as integer. For example: 1/2 gives 0 in python 2.7 and below.
Just saying: numpy has this too. So no need to import math if you already did import numpy as np:
>>> np.exp(1)
2.718281828459045
Hi everyone,
As part of an assignment I need to rewrite the formula which estimates E based on x number of terms the user inputs. I believe the crux of my problem is I don't know how to express a factorial in python. I'm not very math fluent, and and was wondering if anyone may be able to help modify my current code as I'm not getting the correct output.
for example if I enter 4, my output should be 2.666666665, however I get 2.44140625
Mathematical formula for ref:
https://en.wikipedia.org/wiki/E_(mathematical_constant)
Code:
n = int(input("Enter a term to approximate e: "))
#fact = 1
for i in range(1,n+1):
euler = (1 + (1/n))**n
print(euler)I vaguely remember in math that when we learned this stuff, e was different than when we used 10 as the base. It's an irrational number 2.718..etc
But when I print numbers in python, it says e but seems to mean 10? When I print out all the digits instead of scientific notation, the math only makes sense if I imagined e as 10.
From the Python 3.7 documentation:
'e':Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is
6.
'g':General format. For a given precision
p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.The precise rules are as follows: suppose that the result formatted with presentation type
'e'and precisionp-1would have exponentexp. Then if-4 <= exp < p, the number is formatted with presentation type'f'and precisionp-1-exp. Otherwise, the number is formatted with presentation type'e'and precisionp-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the'#'option is used.Positive and negative infinity, positive and negative zero, and nans, are formatted as
inf,-inf,0,-0andnanrespectively, regardless of the precision.A precision of
0is treated as equivalent to a precision of1. The default precision is6.
With alternative values:
>>> "%.3e" % 123
'1.230e+02'
>>> "%.3g" % 123
'123'
>>> "%.3e" % 1234
'1.234e+03'
>>> "%.3g" % 1234
'1.23e+03'
The difference then is clearly about how the precision is specified. g appears to use the precision as the normal definition of precision, whereas e uses the number of figures after the decimal point.
From the printf(3) man page:
e, E
The double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.
...
g, G
The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
So, they do different things even with the same precision.
**: exponentiation^: exclusive-or (bitwise)%: modulus//: divide with integral result (discard remainder)
You can find all of those operators in the Python language reference, though you'll have to scroll around a bit to find them all. As other answers have said:
- The
**operator does exponentiation.a ** bisaraised to thebpower. The same**symbol is also used in function argument and calling notations, with a different meaning (passing and receiving arbitrary keyword arguments). - The
^operator does a binary xor.a ^ bwill return a value with only the bits set inaor inbbut not both. This one is simple! - The
%operator is mostly to find the modulus of two integers.a % breturns the remainder after dividingabyb. Unlike the modulus operators in some other programming languages (such as C), in Python a modulus it will have the same sign asb, rather than the same sign asa. The same operator is also used for the "old" style of string formatting, soa % bcan return a string ifais a format string andbis a value (or tuple of values) which can be inserted intoa. - The
//operator does Python's version of integer division. Python's integer division is not exactly the same as the integer division offered by some other languages (like C), since it rounds towards negative infinity, rather than towards zero. Together with the modulus operator, you can say thata == (a // b)*b + (a % b). In Python 2, floor division is the default behavior when you divide two integers (using the normal division operator/). Since this can be unexpected (especially when you're not picky about what types of numbers you get as arguments to a function), Python 3 has changed to make "true" (floating point) division the norm for division that would be rounded off otherwise, and it will do "floor" division only when explicitly requested. (You can also get the new behavior in Python 2 by puttingfrom __future__ import divisionat the top of your files. I strongly recommend it!)