aboutsummaryrefslogtreecommitdiffstats
path: root/lib/matrix.rb
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-16 04:18:50 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-16 04:18:50 +0000
commit97b2703cf02445a6a23e5686cc2285c014630ddb (patch)
treec29474ef0f4c169dcc428ab1f1e141fba68f59c7 /lib/matrix.rb
parent1c26183839bf2362a8447eb87f8e7de4a7630c0e (diff)
downloadruby-97b2703cf02445a6a23e5686cc2285c014630ddb.tar.gz
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
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb10
1 files changed, 7 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
#--