Floating point calculation problem
Schizoid Man
schiz_man at 21stcentury.com
Sat Feb 2 10:48:24 EST 2013
> Ah, there may well be something in that. Definitely post the code and
> outputs; chances are someone'll spot the difference.
Ok, I *think* I found the source of the difference:
This is the base code that runs fine in v 3.3.0 (the output is correct):
def Numer(s, k):
return math.log(s / k)
s = float(input("Enter s: "))
k = float(input("Enter k: "))
print("Result: ", Numer(s, k))
For the v2.7 version, the only difference is the input lines:
s = input("Enter s: ")
k = input("Enter k: ")
So for values of s=60 and k=50, the first code returns 0.1823215567939546
(on the PC), whereas the second returns 0.0 (on the Mac). This this
expression is evaluated in the numerator, it never returns a divide by zero
error, and the result of 0 is treated as legitimate.
However, if I cast the v2.7 inputs as float() then it does indeed return the
right result. But what threw me was that no cast didn't result in a runtime
error in 2.7, but did in 3.3.
Also, if the cast is necessary, then now exactly does the dynamic typing
work?
Thanks.
More information about the Python-list
mailing list