Results 1 to 4 of 4
  1. #1
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default 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.
    Java 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.
    Java 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.
    Java 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));
    
    
    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Zeronitic is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need help with sin in java

    Nevermind. It was actually very trivial and I figured it out.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Need help with sin in java

    Ah, ok, sine. I thought someone was looking for a confessional here

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •