Re: Fw: Distance between 2 points
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL> Sep 30, 1998
- 567 views
>try this instead. 1 function call vs 3, should be worlds Yes, I could inline it also. I just loven another thread on the sum () function, it will add atoms it can find. I could just have used the '+' operator, since Patrat was prolly refering to a 2D field. However, it is cleaner to use recursive functions. And to be dimension free, who knows how many dimensions we are gonna explore in the future ? Dont want no Y2K typ of problem with my software when we reach 4K Just kidding, I dunno why I do this things... impulses maybe ? >faster for atoms and sequences both, i think... For both atoms and sequences ? Actually I think with atoms, it will should return the difference. pos (a - b) That is also the problem with your function here, you forget to substract. > function distance(object a, object b) > return sqrt( (a*a)+(b*b) ) > end function I think you meant this: -- Assuming 2D world: function distance (sequence a, sequence b) return sqrt ( ((a[1] - b[1]) * (a[1] - b[1])) + ((a[2] - b[2]) * (a[2] - b[2])) ) end function -- Assuming 3D world: function distance (sequence a, sequence b) return sqrt ( ((a[1] - b[1]) * (a[1] - b[1])) + ((a[2] - b[2]) * (a[2] - b[2])) + ((a[3] - b[3]) * (a[3] - b[3])) ) end function -- Assuming 1D world function distance (atom a, atom b) return sqrt ( (a - b) * (a - b)) -- in other words: the positive difference between 'a' and 'b' end function >and something that should be benchmarked against the >above 2 distance() functions as well should be: > function distance(object a, object b) > return power( (a*a)+(b*b),0.5 ) > end function It might be less precize, because the result will always be a float because you use .5 I wonder when Robert is gonna make calculations with floats convert back to machine integers when possible. >to see if power is faster than sqrt. Something I dont expect. First of all, sqrt takes less arguments, and thus needs less conversion. (if the argument passed is an float.. use the sqrt_float routine, other wise the sqrt_sequence or the sqrt_integer routine) .. for power I suspect there are internally 9 different routines. (Robert, if i'm wrong please say so .. ) Off course a simple benchmark would tell wouldn't it ? And now here's my fastest function I could come up with (allowing a number of dimension ranging from 0 to eternity) function distance (object a, object b) if atom (a) and atom(b) then if a > b then return a-b else return b-a end if end if a = power(a - b,2) b = a[1] for index = 2 to length(a) do b = b + a[index] end for return sqrt (b) end function Now, how does this benchmark ? Ralf