[Tutor] Float Addition

DaSmith@technip.com DaSmith at technip.com
Tue Aug 16 18:20:00 CEST 2005





Hi, I am a relatively new Python Programmer who is a bit puzzled at some of
the behavior I am observing. .

I have implemented the following loop, to create a running total of
distances between 3D co-oordinate tuples, in a group of nodes from a Finite
Element Node mesh:

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) )
        Dis_Tot = float ( Dis_N ) + float ( DEF_CARRIER_DIS[N-1] )
        print ( "New Total = " + str( Dis_Tot) )
        DEF_CARRIER_DIS.append( Dis_Tot )

 # End For

  here is the output from my print statements for the first few iterations
of the loop:

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

I would expect the Distance to increase by approximately 0.3366 in each
iteration of the loop, but this is clearly not happening in every second
Iteration of the loop. indicatings to me that there must be a problem wit
this statement:

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

Unfortuntaely I cannot see what the problem is, and am especially surprised
by the fact that it happens only in every second loop iteration

I wonder if anyone could help me ?

Regards,

Daniel Smith.



More information about the Tutor mailing list