[Tutor] Float Addition

Alan G alan.gauld at freenet.co.uk
Thu Aug 18 09:44:29 CEST 2005


I'm not exactky sure what's causing the errant behaviour but it might
help if we start off by simplifying the code a little.


> for N in range( 2, len( DEF_CARRIER_TUP) ):
>        Dis_N = DistanceCalc( DEF_CARRIER_TUP[N], 
> DEF_CARRIER_TUP[N-1] )
>        print ( "Distance = " + str(Dis_N) )

         print "Distance = ", Dis_N

>        Dis_Tot = float ( Dis_N ) + float ( DEF_CARRIER_DIS[N-1] )

         Dis_Tot = Dis_N + DEF_CARRIER_DIS[N-1]

Dis_N is already a float from the print statement. And DisTot is
simply the collection of totals so is also floats, no need for
conversion.

>        print ( "New Total = " + str( Dis_Tot) )

         print "New Total = ", Dis_Tot

>        DEF_CARRIER_DIS.append( Dis_Tot )

Also I don't see any uinitialisation of DEF_CARRIER_DIS, but as 
written
the first time through the loop you access N-1, ir element 1. I have
no idea what is in element 1, but you then procede to append to it,
now the new element will go to element len(DEF_CARRIER_DIS).

Its not obvious to me the relationship between the data being
appended to the list and the data being used to do the calculation
from index N-1.

> New Total = 0.336672732923 > Distance = 0.336681748175
> New Total = 0.673377528119 > Distance = 0.336678894036
> New Total = 0.673351626959 > Distance = 0.336692518582
> New Total = 1.0100700467   > Distance = 0.336678428545
> New Total = 1.0100300555   > Distance = 0.336681289645
> New Total = 1.34675133635  > Distance = 0.336684966109
> New Total = 1.34671502161  > Distance = 0.336689614642
> New Total = 1.68344095099  > Distance = 0.336680540776
> New Total = 1.68339556239

Looking at these results I think there is a mismatch between N_1 and
the current position in the list. I think you are somehow reading
one behind where you are writing. I'd look at the initialisation
of that list.

Another option might be to use -1 as the index to ensure you
always use the last element:

         Dis_Tot = Dis_N + DEF_CARRIER_DIS[-1]

HTH,

Alan G.




More information about the Tutor mailing list