[Tutor] Using Python for accurate calculations

bob gailer bgailer at gmail.com
Mon May 12 18:46:21 CEST 2008


Gloom Demon wrote:
> Hello
>
> I am working on a function which is going to calculate correlation 
> between two given sets of numbers. 
>
> Example:
> x=[10.0, 10.8, 11.3, 10.0, 10.1, 11.1, 11.3, 10.2, 13.5, 12.3, 14.5, 
> 11.0, 12.0, 11.8, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1]
> y=[0.70, 0.73, 0.75, 0.70, 0.65, 0.65, 0.70, 0.61, 0.70, 0.63, 0.70, 
> 0.65, 0.72, 0.69, 0.78, 0.70, 0.60, 0.85, 0.80, 0.75]
>
>
> However Python stores these numbers like this:
> >>> x
> [10.0, 10.800000000000001, 11.300000000000001, 10.0, 10.1, 11.1, 
> 11.300000000000001, 10.199999999999999, 13.5, 12.300000000000001, 
> 14.5, 11.0, 12.0, 11.800000000000001, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1]
> >>> y
> [0.69999999999999996, 0.72999999999999998, 0.75, 0.69999999999999996, 
> 0.65000000000000002, 0.65000000000000002, 0.69999999999999996, 
> 0.60999999999999999, 0.69999999999999996, 0.63, 0.69999999999999996, 
> 0.65000000000000002, 0.71999999999999997, 0.68999999999999995, 
> 0.78000000000000003, 0.69999999999999996, 0.59999999999999998, 
> 0.84999999999999998, 0.80000000000000004, 0.75]
>
> The extra 
> digits added by Python are adding error probability to my calculations. Is there a way to overcome this?

See http://docs.python.org/tut/node16.html.

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list