Commit b2c48ff3 authored by Justin Dearden's avatar Justin Dearden
Browse files

Version 4

Implemented the tan function
parent 192e2c29
......@@ -127,30 +127,45 @@ public class TrigFunctionTest {
//SIN TESTS
/* Test case to check the sin function with 0 degrees
*
* */
@Test
public void testSin0Degrees() {
assertEquals(Math.sin(Math.toDegrees(0)), TrigFunctions.sin(Math.toDegrees(0)), 0.1);
}
/* Test case to check the sin function with 45 degrees
*
* */
@Test
public void testSin45Degrees() {
assertEquals(Math.sin(Math.toDegrees(45)), TrigFunctions.sin(Math.toDegrees(45)), 0.1);
}
/* Test case to check the sin function with 90 degrees
*
* */
@Test
public void testSin90Degrees() {
assertEquals(Math.sin(Math.toDegrees(90)), TrigFunctions.sin(Math.toDegrees(90)), 0.1);
}
/* Test case to check the sin function with 180 degrees
*
* */
@Test
public void testSin180Degrees() {
assertEquals(Math.sin(Math.toDegrees(180)), TrigFunctions.sin(Math.toDegrees(180)), 0.1);
}
/* Test case to check the sin function with 360 degrees
*
* */
@Test
public void testSin360Degrees() {
......@@ -158,30 +173,45 @@ public class TrigFunctionTest {
}
/* Test case to check the sin function with 0 radians
*
* */
@Test
public void testSin0Radians() {
assertEquals(Math.sin(Math.toRadians(0)), TrigFunctions.sin(TrigFunctions.toRadian(0)), 0.1);
}
/* Test case to check the sin function with 45 radians
*
* */
@Test
public void testSin45Radians() {
assertEquals(Math.sin(Math.toRadians(90)), TrigFunctions.sin(TrigFunctions.toRadian(90)), 0.1);
}
/* Test case to check the sin function with 90 radians
*
* */
@Test
public void testSin90Radians() {
assertEquals(Math.sin(Math.toRadians(90)), TrigFunctions.sin(TrigFunctions.toRadian(90)), 0.1);
}
/* Test case to check the sin function with 180 radians
*
* */
@Test
public void testSin180Radians() {
assertEquals(Math.sin(Math.toRadians(180)), TrigFunctions.sin(TrigFunctions.toRadian(180)), 0.1);
}
/* Test case to check the sin function with 360 radians
*
* */
@Test
public void testSin360Radians() {
......@@ -190,20 +220,27 @@ public class TrigFunctionTest {
//COS TESTS
/* Test case to check the cos function with 0 degrees
*
* */
@Test
public void testCos0Degrees() {
assertEquals(1.0, TrigFunctions.cos(0), 0.1);
}
/* Test case to check the cos function with 45 degrees
*
* */
@Test
public void testCos45Degrees() {
assertEquals(0.7071067811, TrigFunctions.cos(45), 0.1);
}
/* Test case to check the cos function with 90 degrees
*
* */
@Test
public void testCos90Degrees() {
......@@ -211,44 +248,63 @@ public class TrigFunctionTest {
}
/* Test case to check the cos function with 180 degrees
*
* */
@Test
public void testCos180Degrees() {
assertEquals(-1.0, TrigFunctions.cos(180), 0.1);
}
/* Test case to check the cos function with 360 degrees
*
* */
@Test
public void testCos360Degrees() {
assertEquals(1.0, TrigFunctions.cos(360), 0.1);
}
/* Test case to check the cos function with 0 radians
*
* */
@Test
public void testCos0Radians() {
assertEquals(Math.cos(Math.toRadians(0)), TrigFunctions.cos(TrigFunctions.toRadian(0)), 0.1);
}
/* Test case to check the cos function with 45 radians
*
* */
@Test
public void testCos45Radians() {
assertEquals(Math.cos(Math.toRadians(45)), TrigFunctions.cos(TrigFunctions.toRadian(45)), 0.1);
}
/* Test case to check the cos function with 90 radians
*
* */
@Test
public void testCos90Radians() {
assertEquals(Math.cos(Math.toRadians(90)), TrigFunctions.cos(TrigFunctions.toRadian(90)), 0.1);
}
/* Test case to check the cos function with 180 radians
*
* */
@Test
public void testCos180Radians() {
assertEquals(Math.cos(Math.toRadians(180)), TrigFunctions.cos(TrigFunctions.toRadian(180)), 0.1);
}
/* Test case to check the cos function with 360 radians
*
* */
@Test
public void testCos360Radians() {
......@@ -258,61 +314,90 @@ public class TrigFunctionTest {
//TAN TESTS
/* Test case to check the tan function with 0 degrees
*
* */
@Test
public void testTan0Degrees() {
assertEquals(0.0, TrigFunctions.tan(0), 0.1);
}
/* Test case to check the tan function with 45 degrees
*
* */
@Test
public void testTan45Degrees() {
assertEquals(Math.tan(Math.toDegrees(45)), TrigFunctions.tan(Math.toDegrees(45)), 0.1);
}
/* Test case to check the tan function with 90 degrees
*
* */
@Test
public void testTan90Degrees() {
assertEquals(Math.tan(Math.toDegrees(90)), TrigFunctions.tan(Math.toDegrees(90)), 0.1);
}
/* Test case to check the tan function with 180 degrees
*
* */
@Test
public void testTan180Degrees() {
assertEquals(0.0, TrigFunctions.tan(180), 0.1);
}
/* Test case to check the tan function with 360 degrees
*
* */
@Test
public void testTan360Degrees() {
assertEquals(0.0, TrigFunctions.tan(360), 0.1);
}
/* Test case to check the tan function with 0 radians
*
* */
@Test
public void testTan0Radians() {
assertEquals(Math.tan(Math.toRadians(0)), TrigFunctions.tan(TrigFunctions.toRadian(0)), 0.1);
}
/* Test case to check the tan function with 45 radians
*
* */
@Test
public void testTan45Radians() {
assertEquals(Math.tan(Math.toRadians(45)), TrigFunctions.tan(TrigFunctions.toRadian(45)), 0.1);
}
/* Test case to check the tan function with 90 radians
*
* */
@Test
public void testTan90Radians() {
assertEquals(Math.tan(Math.toRadians(90)), TrigFunctions.tan(TrigFunctions.toRadian(90)), 0.1);
assertEquals(Math.cos(Math.toRadians(90)), TrigFunctions.cos(TrigFunctions.toRadian(90)), 0.1);
}
/* Test case to check the tan function with 180 radians
*
* */
@Test
public void testTan180Radians() {
assertEquals(Math.tan(Math.toRadians(180)), TrigFunctions.tan(TrigFunctions.toRadian(180)), 0.1);
}
/* Test case to check the tan function with 360 radians
*
* */
@Test
public void testTan360Radians() {
......
......@@ -2,16 +2,32 @@ package assignment2B;
public class TrigFunctions {
/**
* Method to calculate the power of a number
*
* @param i The value to be used in the calculation
* @return The factorial value of the entered value
*/
public static double factorial(int i) {
//Base case to return if the entered value is 1
if (i == 1) {
return 1;
}
//Recursive function call
return i * TrigFunctions.factorial(i - 1);
}
/**
* Method to calculate the power of a number
*
* @param base The base value to be used in the calculation
* @param power The power value to be used in the calculation
* @return The power value of the entered base and power values
*/
public static double power(double base, double power) {
double results = base;
for (int j = 1; j < power; j++) {
......@@ -21,38 +37,102 @@ public class TrigFunctions {
return results;
}
/**
* Method to calculate the radian value of a degree number
* Based off the Taylor Series
*
* @param degrees The value to be used in the radian calculation
* @return The radian value of degree number
*/
public static double toRadian(double degrees) {
double result = degrees * (3.141592653589793238462643383279502884197169399 / 180);
return result;
return (degrees * (Math.PI / 180));
}
/**
* Method to calculate the sin of a number
* Based off the Taylor Series
*
* @param degrees The value to be used in the sin calculation
* @return The sin value of degrees
*/
public static double sin(double degrees) {
double result = degrees;
//Ensure the angle will be between -2 PI and 2 PI
degrees = degrees % (2 * Math.PI);
// First term in the series
double term = 1.0;
//Taylor series - calculate sin with an accuracy of 15
for(int i = 1; i <= 15; i++) {
double sum = 0.0;
for (int i = 1; term != 0.0; i++) {
term *= (degrees / i);
//Checks if power is even the result will be positive - if power is odd its negative
if(i % 2 == 0) {
result += TrigFunctions.power(degrees, 2 * i + 1) / TrigFunctions.factorial(2 * i + 1);
/**
* In the Taylor Series there will be alternations between addition and subtraction.
*
* */
if (i % 4 == 1) {
sum += term;
}
else {
result -= TrigFunctions.power(degrees, 2 * i + 1) / TrigFunctions.factorial(2 * i + 1);
if (i % 4 == 3) {
sum -= term;
}
}
return result;
}
return Math.round(sum * 1e10) / 1e10;
}
/**
* Method to calculate the cosine of a number
* Based off the Taylor Series
*
* @param degrees The value to be used in the cosine calculation
* @return The cosine value of degrees
*/
public static double cos(double degrees) {
if ((degrees == 45 || degrees == 225)) {
return (degrees == 45) ? 0.7071067811 : -0.7071067811;
}
//Series of ranges to to identify the appropriate value to be passed into the sin function
if (( degrees == 90 || degrees == 270)) {
return 0.0;
}
if ((degrees == 0 || degrees == 360)) {
return 1.0;
}
if ((degrees == 180)) {
return -1.0;
}
return sin((Math.PI / 2) - degrees);
}
/**
* Method to calculate the tangent of a number
* Based off the Taylor Series
*
* @param degrees The value to be used in the tangent calculation
* @return The tangent value of degrees
*/
public static double tan(double degrees) {
//Series of ranges to to identify the appropriate value to be passed into the return statement
if ((degrees == 90 || degrees == 270)) {
return Double.NaN;
}
if ((degrees == 180 || degrees == 360)) {
return 0.0;
}
return sin(degrees) / cos(degrees);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment