Monday, 25 April 2016

How do I calculate the sine and cosine of a double in Java?

I'm trying to convert the sine of an angle from radians to degrees and I keep getting inaccurate numbers. My code looks like this:



public class PhysicsSolverAttempt2 {
public static void main(String[] args) {
double[] numbers = {60, 30, 0};

double launchAngle = Double.parseDouble(numbers[0]);
double iV = Double.parseDouble(numbers[1]);
System.out.println(launchAngle);
double iVV = iV * Math.toDegrees(Math.sin(launchAngle));
System.out.println(Math.toDegrees(Math.sin(launchAngle)));
}
}


When I use Math.sin(launchAngle), it gives me a perfect output in radians. However, when I convert the radians to degrees using the Math.toDegrees() function at the end, it gives me -17.464362139918286, though performing the same calculation with a calculator yields the number 0.86602540378. Am I using Math.toDegrees() incorrectly, or do I need to perform extra steps to get an accurate result?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...