Floating point calculation problem
schiz_man at 21stcentury.com
Sat Feb 2 12:50:09 CET 2013
> Highly unlikely. I'd say impossible, unless you type a different value for
> of course. By the time the input() function returns, the result is already
> a float. Wrapping it in float() again cannot possibly change the value. If
> you have found a value that does change, please tell us what it is.
> The only examples I can think of that will behave that way involve NANs
> INFs. If you don't know what they are, don't worry about it, and forget I
> mentioned them. For regular floating point values, I can't think of any
> possible way that float(input(x)) and input(x) could give different
No, the calculation returns in neither NaN or Inf at any point.
> float(x) converts x into a float.
> - if x is already a float, it leaves it unchanged;
> - if x is a string, it converts it to the nearest possible float;
> - if x is some other numeric value (e.g. int, Decimal or Fraction,
> but not complex) it converts it to the nearest possible float.
> float(input()) is a waste of time. The dangerous part happens in the call
> input(): a malicious user could type a Python command, and run arbitrary
> code; or they could type something like "10**100**100" and lock up your
> computer. Calling float *after* the call to input doesn't do anything.
> In Python 3.x, raw_input is gone, but float(input()) is safe -- it is
> exactly equivalent to float(raw_input()) in Python 2.x.
> One other difference between Python 2.7 and 3.3 is that they sometimes
> display floats slightly differently. Sometimes 3.3 will show more decimal
Thanks for that, I'll post the problematic code here shortly.
More information about the Python-list