I started coding a couple of days ago and made a simple trig calculator. It uses degrees and gives out degrees as the answer for most values, except some.
For example. If I want the answer to sin(90), it gives it to me, but if I want the answer to sin(180), it gives me an incorrect one.
Here's the code for the sin function
Scanner input = new Scanner(System.in);
System.out.println ("sin, cos, or tan?");
String trig = input.next();
if (trig.equals ("sin")) {
System.out.println ("input sin value in degrees");
Scanner angle = new Scanner(System.in);
double a,b;
a = (angle.nextDouble());
b = Math.sin(Math.toRadians(a));
System.out.println("Answer: "+ (b) );
It gives me answers like 1.2246467991473532E-16 for the following angles: 180, 360, 540, 720 etc.
This also happens for the cos and tan functions (it only happens when the tan functions are undefined though, so I can understand that one).
Math.toRadians(180)
is not exactly equal to the mathematical constant π, and thereforeMath.sin(Math.toRadians(180)
is not exactly 0. – njuffa