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.
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
recreating E without using the factorial function in python?
I have to idea how to use E (elements of) in python.
How come e means 10 in Python numbers like 7.3e^-10? Isn't e suppose to be 2.71828..?
Trying to calculate the exponential function (e^-x) with its series, getting different results with (supposedly) equivalent methods.
Videos
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)EDIT: Thank you everyone for pointing out my error! You guys are awesome!
currently my output is 15.440000000000001, and the correct is 16. I've read and looked up enough that I should use braces but I just get syntax errors. Can someone please explain what I need to do, what Im missing. Ive worked this for hours and just don't know. Thanks for the help.
EDIT - Ive read the links and Idk, I feel like I understand math and most of python that Im learning but can't use epsilon correctly.
STAB = 1
Type = 0.25
Critical = 2
Other = 1
Random = 1
Level = 50
Attack = 125
Defense = 110
Base = 60
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#In the Pokemon game franchise, damage is calculated using this formula:
#https://studio.edx.org/asset-v1:GTx+CS1301+1T2017+type@asset+block@DamageCalc.png
#
#In that formula, the variable Modifier is calculated using this formula:
#https://studio.edx.org/asset-v1:GTx+CS1301+1T2017+type@asset+block@ModifierCalc.png
#
#Add code below such that the program prints the total damage
#caused based on the STAB, Type, Critical, Other, Random,
#Level, Attack, Defense, and Base given above.
#
#Hint: Don't try to do all these calculations at once! Break
#the complicated formual down into bite-sized little chunks.
#mod = STAB * Type * Critical * Other * Random {0.85,1}
#print(mod)
modifier = STAB * Type * Critical * Other * Random
damage = ((2 * Level + 10) / 250) * ((Attack / Defense) * Base + 2) * modifier
print(damage)