Added preventing of division-by-zero cases when normalizing.

This commit is contained in:
Jarl Gullberg 2017-05-31 21:31:11 +02:00
parent 754e577623
commit e4ec841e7a
No known key found for this signature in database
GPG key ID: 750FF6F6BDA72D23

View file

@ -114,6 +114,8 @@ module Vector3 =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = v.Length let l = v.Length
// Dividing by zero is not supported
if not (approxEq l 0.0f) then
let norm = v.Normalized() let norm = v.Normalized()
Assert.ApproximatelyEqual(v.X / l, norm.X) Assert.ApproximatelyEqual(v.X / l, norm.X)
@ -123,10 +125,13 @@ module Vector3 =
[<Property>] [<Property>]
let ``Normalization of instance works`` (a, b, c) = let ``Normalization of instance works`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = v.Length
if not (approxEq l 0.0f) then
let norm = Vector3(a, b, c) let norm = Vector3(a, b, c)
norm.Normalize() norm.Normalize()
let l = v.Length
Assert.ApproximatelyEqual(v.X / l, norm.X) Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y) Assert.ApproximatelyEqual(v.Y / l, norm.Y)
@ -146,6 +151,7 @@ module Vector3 =
[<Property>] [<Property>]
let ``Normalization by reference works`` (a : Vector3) = let ``Normalization by reference works`` (a : Vector3) =
if not (approxEq a.Length 0.0f) then
let scale = 1.0f / a.Length let scale = 1.0f / a.Length
let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale) let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale)
let vRes = Vector3.Normalize(ref a) let vRes = Vector3.Normalize(ref a)
@ -154,6 +160,7 @@ module Vector3 =
[<Property>] [<Property>]
let ``Normalization works`` (a : Vector3) = let ``Normalization works`` (a : Vector3) =
if not (approxEq a.Length 0.0f) then
let scale = 1.0f / a.Length let scale = 1.0f / a.Length
let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale) let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale)