Odd math related issue.
javaboy.ms at gmail.com
Mon Jul 21 15:31:40 CEST 2008
On Jul 21, 5:30 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> Alexandru Palade wrote:
> > However, you should be carefully because using an %i modifier for a
> > what-should-be a float value truncates the value in a way you may not
> > expect.
> > What I mean is that if you have sent 2 out of 3 bytes, the math will be
> > 200/3 which with the %i modifier will print 66, rather than 66.6 (or at
> > least 67 which is closer - have a look at the round() function).
> My suggested workaround doesn't use floats. As for rounding, that's
> more of a usability issue -- seeing the download process getting stuck
> at 100% can be rather frustrating for the poor user. Better truncate
> towards zero.
> > Another thing, you could have just added a dot after the constant in
> > order to promote the expression to be evaluated as float. As in
> > percentage = bytes_transferred / /self/.__sessions[path].total_bytes
> > * 100.
> > (notice the last dot)
> Did you try that?
> >>> 2 / 3 * 100.
> You can fix this with parentheses, but I usually recommend an explicit
> "cast" instead, to make it obvious what you're doing:
> result = float(a) / b
Use 2.0 / 3 * 100 to solve this. Why make things look bigger?
More information about the Python-list