Vector2D.Normalize() wrong result
McGum
1/8/2013 1:08 PM
|
---|
Hi together,
i call:
var vector = new Vector2D(998,0);
vector.Normalize();
then vector is
X 0.99999999999999989 double
Y 0 double
must be a Bug ;)
|
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: ( |
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: / |
rammi
2/18/2013 2:15 PM
|
---|
Newer versions of CadLib also provide two Vector2D.AreApproxEqual() methods.
The method
bool AreApproxEqual(Vector2D u, Vector2D v)
internally uses an absolute epsilon value, which is only useful for vectors with magnitude 1.0.
So in general you should use
bool AreApproxEqual(Vector2D u, Vector2D v, double tolerance)
with a tolerance which is matching your result space.
For example I often use the bounding box of the model to determine a useful tolerance value.
Regards,
Rammi
|