-4

Ok so here's the situation...

I'm currently working on a project for my Math and Physics for Games class.

I finished coding my solution, and ran the xUnit tests that my teacher made for us.

90% of them fail.

I have a Calculator.cs file that contains all of the methods that I have coded. Each trigonometric method is made to return a Tuple, and that Tuple's items are then used in an xUnit.Assert.Equal(expectedResult, Math.Round(calculatorTuple.Item1, 4))...

For example... I have a method named Trig_Calculate_Adjacent_Hypotenuse that accepts two doubles as it's parameters (Angle in degrees and Opposite)

The calculator finds that Adjacent is equal to 15.4235... but my real-life calculations show me that it's 56.7128. Therefore when the test runs, it does Assert.Equal(56.7128, 15.4235) and finds that these two answers are not equal. (obviously)

I looked over the code in my Calculator.cs file multiple times... and cannot for the life of me find the problem.

Here's my method so you can take a look at it:

public static Tuple<double,double> Trig_Calculate_Adjacent_Hypotenuse(double Angle, double Opposite)
    {
        double Hypotenuse;
        double Adjacent;
        // SOH CAH TOA

        // Using TOA to find Adjacent
        // so Adjacent = Opposite / Tan(Angle)
        // so Adjacent = 10 / Tan(10)
        // which means Adjacent = 56.7128
        // However my calculator finds 15.4235 instead...
        Adjacent = Opposite / Math.Tan(Calculator.DegreesToRadians(Angle));

        // Using SOH to find Hypotenuse
        // so Hypotenuse = Opposite / Sin(Angle)
        // so Hypotenuse = 10 / Sin(10)
        // which means Hypotenuse = 57.5877
        // However my calculator finds something different... (unknown due to Adjacent's failure)
        Hypotenuse = Opposite / Math.Sin(Calculator.DegreesToRadians(Angle));

        return new Tuple<double, double>(Adjacent, Hypotenuse);
    }

And here's the test method:

        [Theory]
    // Student Data
    [InlineData(10, 10, 56.7128, 57.5877)]
    public void TestCalculateAdjacentHypotenuse(double Angle, double Opposite, double Adjacent, double Hypotenuse)
    {
        // Act - performing the action
        Tuple<double, double> results = Calculator.Trig_Calculate_Adjacent_Hypotenuse(Angle, Opposite);

        // Assert - did we get back the correct answer
        Assert.Equal(Adjacent, Math.Round(results.Item1, 4));
        Assert.Equal(Hypotenuse, Math.Round(results.Item2, 4));
    }

I hope you guys can help me find out what the problem is! :)

Thank you!

Pierre Gravelle
  • 53
  • 2
  • 15

1 Answers1

5

Math.Tan(Angle) works with radians, not with degrees (Also Sin(), Cos() work with radians).

Try Math.Tan(Angle * Math.PI / 180);

Roman
  • 11,966
  • 10
  • 38
  • 47