 JayT -4 years ago 150
C# Question

# Rotate point test is failing. Why?

This is my Point.cs file that is failing.

``````// Rotates the point counter-clockwise by deg degrees.  For example, applying
// a 90 degree rotation to (1, 0) should give (0, 1).
public void rotate(double deg)
{
double rotCos = Math.Cos(deg * Math.PI / 180);
double rotSin = Math.Sin(deg * Math.PI / 180);
double oldX = x;
double oldY = y;
x = oldX * rotCos - oldY * rotSin;
y = oldX * rotSin + oldY * rotCos;
Point point = new Point(x, y);
}
``````

I run this test and get the result:

"Message: Assert.AreEqual failed. Expected:<2>. Actual:<2>."

``````[TestMethod]
public void Rotate_Test()
{
double rotate = -90;
double i = 4;
double o = 2;
Point point = new Point(i, o);
double expectedX = 2;
double expectedY = 4;

point.rotate(rotate);

Assert.AreEqual(expectedX, point.getX());
Assert.AreEqual(expectedY, point.getY());
}
`````` Connell.O&#39;Donnell
Answer Source

Doubles are not exact numbers. They should not be compared for equality without specifying an acceptable difference between them.

https://msdn.microsoft.com/en-us/library/ms243458.aspx

https://msdn.microsoft.com/en-us/library/ya2zha7s(v=vs.110).aspx

Try

``````Assert.AreEqual(expectedX, point.getX(), 0.001);
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
Latest added