[Tutor] Need more precise digits
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sat, 1 Dec 2001 16:10:17 -0800 (PST)
On Sat, 1 Dec 2001, Mike Yuen wrote:
> I've trying to calculate an average and it looks like i'm losing some
> precision in digits.
>
> For example: i've got the following formula
> avgsize = float(totalsize/groups)
The problem is one of timing: by the time float() works on its argument,
we've already lost precision. In Python,
###
>>> float(1/2)
0.0
###
is evaluated in steps: first, Python needs to figure out what '1/2' looks
like:
###
>>> 1/2
0
###
and then Python can call float() on '0'.
###
>>> float(0)
0.0
###
When we call functions, Python needs to figure out first what the
arguments will look like. This is the bug that you're running into.
Hope this helps!