From: Robert Klemme Date: 2008-03-31T18:47:56+09:00 Subject: Re: Is it me or Math::sqrt? 2008/3/31, Phillip Gawlowski : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hello list. > > I'm a bit stupefied by the different results of this algorithm I > implemented (Euclidean distance for 3 dimensions): > > # Calculates the relative distance_to an arbitrary > # Note: The algorithm is not yet optimized for speed. > # Hopefully, it is accurate, though. > def distance_to(actor) > ~ # Stub, will use vector math or so to approximate the true distance > ~ distance = (self.x - actor.x)^2 + (self.y - actor.y)^2 + (self.z - > actor.z)^2 > ~ Math::sqrt(distance.abs) > end > > actor1 = Gondor::AI::Actor.new(:x => 0,:y => 0,:z => 0) > actor2 = Gondor::AI::Actor.new(:x => 16, :y => 16, :z => 16) > actor1.distance(actor2) > produces: 3.74165738677394 > > actor1 = Gondor::AI::Actor.new(:x => 1,:y => 1,:z => 1) > actor2 = Gondor::AI::Actor.new(:x => 15, :y => 15, :z => 15) > produces: 4.0 > > Why is that? From my understanding, these results should be identical > (The algebraic sign is eliminated by exponent 2, everything else is > addition). > > Another thing that has me stumped is, that Math::sqrt requires the > absolute, but the result should be positive (addition of positive > algebraic signs). > > Do I need to brush up my math more than I thought? > > - -- Phillip Gawlowski > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.8 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org > > iEYEARECAAYFAkfwsX8ACgkQbtAgaoJTgL+4nwCeJ4ICdvn0ruCpxc9U5OTIjV8y > OnsAn1nOr6/Fjk0epFFpb3sYgqHoDVhj > =gHrp > -----END PGP SIGNATURE----- - without words - irb(main):002:0> (0-16)^2 => -14 irb(main):003:0> (0-16) ** 2 => 256 :-) Kind regards robert -- use.inject do |as, often| as.you_can - without end