Vector2D.Normalize() wrong result
McGum
1/8/2013 1:08 PM


Hi together, i call: var vector = new Vector2D(998,0); then vector is

Wout
1/8/2013 1:25 PM


Hi, It's not a bug but a floating point rounding error, it's the result of: C# Code: (998d * (1d / System.Math.Sqrt(998d * 998d))) The division is done once, and then the multiplication is done for both x and y (multiplication is faster than division). In practice this doesn't give any problems.  Wout 
McGum
1/8/2013 2:14 PM


mhhmm ... and, how can I check if a Vector is 1:0? So for me it gives problems :( 
rammi
2/18/2013 1:51 PM


When using IEEE floating point math as it is implemented in modern computers you should never rely on exactness. So always be prepared for a small delta, usually called epsilon. As you know that both vectors you are comparing are of magnitude 1 the comparision is simple enough: C# Code: /// <summary> /// Are two vectors of length <c>1.0</c> equal with only a small difference? /// </summary> /// <remarks> /// Use only for normalized vectors! /// </remarks> /// <param name="v1">First vector.</param> /// <param name="v2">Second vector.</param> /// <returns><c>true</c> if both vectors are nearly equal, <c>false</c> otherwise.</returns> public static bool AreNearlyEqual(Vector2D v1, Vector2D v2) { const double epsilon = 1e12; return System.Math.Abs(v1.X  v2.X) < epsilon && System.Math.Abs(v1.Y  v2.Y) < epsilon; } Be warned that the value of epsilon should be relative to the magnitude of the numbers you are comparing, as the exactness of the underlying IEEE number representation is relative to the magnitude. So in general you should eg multiply a reasonable epsilon with the maximum length of the compared vectors (or faster: their largest absolut component). The relative epsilon used (here 1e12 which is a reasonable generalpurpose value for doubles) depends on the algorithms involved before comparing the results. Some algorithms introduce errors very fast, and it is not easy to reduce them. CadLib takes care in various places (eg spline calculation) to make algorithms robust by taking measures to not inroduce too large errors. Hope this helps, 
rammi
2/18/2013 2:15 PM


Newer versions of CadLib also provide two Vector2D.AreApproxEqual() methods. The method So in general you should use Regards, 