Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Need a ruby math genius - potential ruby bug.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Matthew Moss, post: 4502864"] 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: [i] v is not a copy of the parameter passed in but a reference to it. And you are changing the elements of v directly. In these two cases here, you don't notice the destructive behaviour, since you assign the result to the parameter. But here: 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 =3D inner_product(self) if div.zero? self else self * (1 / Math.sqrt(div)) end end def Vector.normalize(v) v.normalize end end[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Need a ruby math genius - potential ruby bug.
Top