0

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());
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
JayT
  • 3
  • 1
  • What do you do with your `Point point = new Point(x, y);`? It gets deleted right away because you didn't assign it to anything... – FCin Mar 01 '17 at 10:36
  • I assume it is the final product. really all I need are the x, y points. – JayT Mar 01 '17 at 10:38
  • @JayT, Provide a [mcve] that can be used to reproduce the problem. – Nkosi Mar 01 '17 at 10:45
  • @FCin I removed that line of code and I still get the same result. – JayT Mar 01 '17 at 10:46
  • @Nkosi I don't know what you are saying... – JayT Mar 01 '17 at 10:47
  • I provided the message I get when running the test. and the test. and the class file. what else am I missing? – JayT Mar 01 '17 at 10:51
  • Everything you need should be there. The points are two doubles. The getX() and getY() ONLY return x or y respectively. – JayT Mar 01 '17 at 10:59
  • Any how the problem is that the calculated value and the expected values are not the same. after exercising the method under test the value of x in the point turns to `2.0000000000000004` which when compared to `expectedX (2.0)` would fail. – Nkosi Mar 01 '17 at 11:03
  • so then I need to? Reading the comment, the math is supposed to take a 1, 0 point and output 0,1 point. effectively rotating my point around the 0,0 pivot. when I imput a point 1,0. I get "Message: Assert.AreEqual failed. Expected:<0>. Actual:<6.12303176911189E-17>." – JayT Mar 01 '17 at 11:04
  • https://msdn.microsoft.com/en-us/library/ms243458.aspx – Nkosi Mar 01 '17 at 11:06
  • No. the values returned are ~2.0 and -4 which are different to your expectations – Nkosi Mar 01 '17 at 11:07
  • Well yes. and I do not know why that is but the .001 delta will solve my problem effectively and it is the same as I have used in other test methods. so thanks. Do you know why the math does that? it there a more precise formula for rotating my point around the (0,0) pivot? – JayT Mar 01 '17 at 11:10
  • That I do not know. You are going to have to do some research on that – Nkosi Mar 01 '17 at 11:15
  • Possible duplicate of [Why is floating point arithmetic in C# imprecise?](http://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise) – Manfred Radlwimmer Mar 01 '17 at 11:59

1 Answers1

2

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);
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61