Need help with sin in java
I'm working sin in java but I have a issue.
How can I get rid of the unnecessary zero's in my program.
This is how it is supposed to end up. Code:
Angle Sin
-90.0 -1.0
-75.0 -0.965926
-60.0 -0.866025
-45.0 -0.707107
-30.0 -0.5
-15.0 -0.258819
0.0 0.0
15.0 0.258819
30.0 0.5
45.0 0.707107
60.0 0.866025
75.0 0.965926
90.0 1.0
Here is how mine ends up.
Code:
Angle Sin
-90.0 -1.000000
-75.0 -0.965926
-60.0 -0.866025
-45.0 -0.707107
-30.0 -0.500000
-15.0 -0.258819
0.0 0.000000
15.0 0.258819
30.0 0.500000
45.0 0.707107
60.0 0.866025
75.0 0.965926
90.0 1.000000
Here is my code.
Code:
import java.text.*;
public class prog232 {
public static void main(String[] args){
double a,b;
DecimalFormat numform = new DecimalFormat("0.000000");
a = -105.0;
while (a <= 75){
a = a + 15;
b = Math.sin(Math.toRadians(a));
System.out.println("" + a + " " + numform.format(b));
}
}
}
Re: Need help with sin in java
Please Google and read up on the limitations of using digital hardware to represent floating point numbers and you'll see why your request is not trivial. Also, personally, I like your out better than the first output.
Re: Need help with sin in java
Nevermind. It was actually very trivial and I figured it out.
Re: Need help with sin in java
Ah, ok, sine. I thought someone was looking for a confessional here :o:
db