From 1a6c27346e02a497b5e3cfca8d7e6e77248a3f38 Mon Sep 17 00:00:00 2001 From: marcandre Date: Sun, 16 Sep 2018 04:18:50 +0000 Subject: lib/matrix: Fix potential bug of Vector#angle_with Could happen for some linearly dependent vectors. Patch by Vasiliy Petrov. [Fix GH-1803] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64761 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/matrix.rb | 10 +++++++--- test/matrix/test_vector.rb | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/matrix.rb b/lib/matrix.rb index 5cc9697714..7639b1de6d 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -2080,7 +2080,7 @@ class Vector 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 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 #-- diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb index 52785120b1..631b4d809a 100644 --- a/test/matrix/test_vector.rb +++ b/test/matrix/test_vector.rb @@ -223,6 +223,8 @@ class TestVector < Test::Unit::TestCase 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]) } -- cgit v1.2.3