When realising the following operation:
do = 1.0 - 2.718281828**(-(23**2)/730)
print do Python returns 0.632120558766
When you do it on the calculator or when google it, it returns 0.515509538
:-O
When realising the following operation:
do = 1.0 - 2.718281828**(-(23**2)/730)
print do Python returns 0.632120558766
When you do it on the calculator or when google it, it returns 0.515509538
:-O
Short answer: this is not a math bug but integer (classic) division in Python 2. The sub-expression is evaluated with integer operands, so the sequence is: 23**2 -> 529, apply unary minus -> -529, then integer division -529/730 floors to -1 — which makes the exponent be -1 and yields the ~0.632 value you saw. (peps.python.org)
What to do:
float()), or enable true division with from __future__ import division in Python 2, or run the code under Python 3 (where / already means true division). , and pointed this out earlier. (peps.python.org)For clearer, safer math use the math module instead of typing an approximate e and raising it manually. Also prefer expm1 when you compute 1 - exp(-y) for small y to avoid loss of precision (it computes exp(x)-1 more accurately). Example (avoids repeating the forum snippets):
import math
y = float(23 * 23) / 730.0
# numerically stable and explicit:
result = -math.expm1(-y) # equal to 1 - math.exp(-y), but more accurate for small y
print(result) See the Python math docs for exp, expm1 and the e constant for details. (docs.python.org)
Quick checklist when results look wrong:
float()).math.exp/math.expm1 and math.e for readability and numerical stability.from __future__ import division or migrating to Python 3. (peps.python.org)Jump to Post— snippsat 661>>> 1.0 - 2.718281828**(-(23.0**2)/730) 0.5155095380022271 >>> #Or import division from future >>> from __future__ import division >>> 1.0 - 2.718281828**(-(23**2)/730) 0.5155095380022271 >>>
Jump to Post— woooee 814"/ 730" has to be a float (or you have to convert somehow).
##1.0 - 2.718281828**(-(23**2)/730) a = 23**2 print a a *= -1 print a print "---------------" print a/730 a /= 730.0 print a print "---------------" b = 2.718281828**a print b print 1.0 - b
>>> 1.0 - 2.718281828**(-(23.0**2)/730)
0.5155095380022271
>>> #Or import division from future
>>> from __future__ import division
>>> 1.0 - 2.718281828**(-(23**2)/730)
0.5155095380022271
>>> "/ 730" has to be a float (or you have to convert somehow).
##1.0 - 2.718281828**(-(23**2)/730)
a = 23**2
print a
a *= -1
print a
print "---------------"
print a/730
a /= 730.0
print a
print "---------------"
b = 2.718281828**a
print b
print 1.0 - b Something like
q = 3/5
would signify a division of integers and would give you
q = 0
in most computer languages and Python2
Python3 has changed this '/' is now a floating point division and '//' is an integer division.
If you want q to be a floating point in Python2, then you simply use
q = 3/5.0
print (23**2)/730 # 0
print (23**2)/730.0 # 0.724657534247 Oh... I though that using
.0 in only one element was enough. Tks
The compiler will take your math expression and turn it into a series of smaller stepwise expressions. That one time .0 can easily get lost. Anyway, Python3 takes care of that ambiguity.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.