While Statement
Joel Ross
joelc at cognyx.com
Fri May 22 10:03:15 EDT 2009
Dave Angel wrote:
>
>
> Tim Wintle wrote:
>> On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote:
>>
>>> number/total = 998/999 = 0
>>> number/total*100 = 0*100 = 0
>>> float(number/total*100) = float(0) = 0.0
>>>
>>> Change "float(number/total*100)" to "float(number)/total*100" and it
>>> should work:
>>>
>>
>> I'd use:
>>
>> (number * 100.)/total
>>
>> - works because
>> <int> * <float> => <float>
>> It's a minor thing, but it's much faster to cast implicitly as you miss
>> the python function call overhead - it's no extra work to write, and for
>> numerical things it can really speed things up.
>>
>>
>>>>> a = timeit.Timer("float(200)/5*100")
>>>>> b = timeit.Timer("(200*100.)/5")
>>>>> a.timeit(10000000)
>>>>>
>> 12.282480955123901
>>
>>>>> b.timeit(10000000)
>>>>>
>> 3.6434230804443359
>>
>> Tim W
>>
>>
>>
>>
> It's the old-timer in me, but I'd avoid the float entirely. You start
> with ints, and you want to end with ints. So simply do the multiply
> first, then the divide.
>
> number * 100/total
>
> will get the same answer.
>
Cheers thanks for the support. if it works faster I'll definitely do it
More information about the Python-list
mailing list