From: Matthew Moss Date: 2006-01-27T06:41:31+09:00 Subject: Re: Need a ruby math genius - potential ruby bug. I think (offhand, didn't actually run your code) your problem is not math but pass-by-reference vs. pass-by-value issues. Look at your Vector.normalize function: > def Vector.normalize(v) > divisor = 0 > v.elements.each do |elem| > divisor += elem*elem > end > > if divisor != 0 > divisor = Math.sqrt(divisor); > i = 0 > v.elements.each do |elem| > v.elements[i] /= divisor > i += 1 > end > end > v > end v is not a copy of the parameter passed in but a reference to it. And you are changing the elements of v directly. > l = Vector.normalize(l) > a = Vector.normalize(a) In these two cases here, you don't notice the destructive behaviour, since you assign the result to the parameter. But here: > b2 = Vector.normalize(b1) You are assigning the result to a different variable, but you've actually changed b1 as well because it's not pass-by-value. I would write a Vector normalize like such: class Vector def normalize div = inner_product(self) if div.zero? self else self * (1 / Math.sqrt(div)) end end def Vector.normalize(v) v.normalize end end