ruby-changes:52549
From: marcandre <ko1@a...>
Date: Sun, 16 Sep 2018 13:18:55 +0900 (JST)
Subject: [ruby-changes:52549] marcandRe: r64761 (trunk): lib/matrix: Fix potential bug of Vector#angle_with
marcandre 2018-09-16 13:18:50 +0900 (Sun, 16 Sep 2018) New Revision: 64761 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64761 Log: lib/matrix: Fix potential bug of Vector#angle_with Could happen for some linearly dependent vectors. Patch by Vasiliy Petrov. [Fix GH-1803] Modified files: trunk/lib/matrix.rb trunk/test/matrix/test_vector.rb Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 64760) +++ lib/matrix.rb (revision 64761) @@ -2080,7 +2080,7 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L2080 end # - # Returns an angle with another vector. Result is within the [0...Math::PI]. + # Returns an angle with another vector. Result is within the [0..Math::PI]. # Vector[1,0].angle_with(Vector[0,1]) # # => Math::PI / 2 # @@ -2089,8 +2089,12 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L2089 Vector.Raise ErrDimensionMismatch if size != v.size prod = magnitude * v.magnitude raise ZeroVectorError, "Can't get angle of zero vector" if prod == 0 - - Math.acos( inner_product(v) / prod ) + dot = inner_product(v) + if dot.abs >= prod + dot.positive? ? 0 : Math::PI + else + Math.acos(dot / prod) + end end #-- Index: test/matrix/test_vector.rb =================================================================== --- test/matrix/test_vector.rb (revision 64760) +++ test/matrix/test_vector.rb (revision 64761) @@ -223,6 +223,8 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L223 assert_in_epsilon(Math::PI/2, Vector[1, 0].angle_with(Vector[0, -1])) assert_in_epsilon(Math::PI/4, Vector[2, 2].angle_with(Vector[0, 1])) assert_in_delta(0.0, Vector[1, 1].angle_with(Vector[1, 1]), 0.00001) + assert_equal(Vector[6, 6].angle_with(Vector[7, 7]), 0.0) + assert_equal(Vector[6, 6].angle_with(Vector[-7, -7]), Math::PI) assert_raise(Vector::ZeroVectorError) { Vector[1, 1].angle_with(Vector[0, 0]) } assert_raise(Vector::ZeroVectorError) { Vector[0, 0].angle_with(Vector[1, 1]) } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/