Results 1 to 15 of 15
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Why doesn't Math.tan() work?

    Java Code:
    angle = Math.tan(45);
    		
    System.out.println(angle);
    Prints 1.6197751905438615. Should be 1

    What on earth is going on?!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    The trig methods take radians, not degrees. See the Math class for methods that convert degrees to radians.

    kind regards,

    Jos
    Last edited by JosAH; 08-09-2011 at 06:43 AM. Reason: duplicate word ...
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Ah! I've been searching google for "java tan bug" this whole time. Thanks!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MrFish View Post
    Ah! I've been searching google for "java tan bug" this whole time. Thanks!
    Java uses the same math function implementations as C (or C++) do.They're all in the flibm library; if there would be a bug it should be in that lib but it has been heavily debugged for years so I trust it to be correct ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Ok. Sorry to come back with another question but I'm pretty bad at math in general.

    Java Code:
    angle = Math.atan(Math.toRadians(1));
    		
    System.out.println("angle: " + angle);
    Shouldn't this be 45?

    Prints angle: 0.017451520651465824

    This works-

    Java Code:
    angle = Math.tan(Math.toRadians(45));
    		
    System.out.println("angle: " + angle);
    Prints 0.9999 (repeating)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default

    Read the API, don't guess around. In any case, why should the argument to atan(...) be in radians (or even degrees, for that matter)?

    db

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MrFish View Post
    Ok. Sorry to come back with another question but I'm pretty bad at math in general.

    Java Code:
    angle = Math.atan(Math.toRadians(1));
    		
    System.out.println("angle: " + angle);
    Shouldn't this be 45?

    Prints angle: 0.017451520651465824

    This works-

    Java Code:
    angle = Math.tan(Math.toRadians(45));
    		
    System.out.println("angle: " + angle);
    Prints 0.9999 (repeating)
    The tan( ... ) method takes a number in radians and returns a number without a dimension so the inverse of the method (atan) takes a number without a dimension and returns a number measured in radians; so make that snippet:

    Java Code:
    angle = Math.toRadians(Math.atan(1));
    The IEEE754 number system isn't exact as you have seen.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Ok. Again thank you. This saved me from an hour of hair pulling.

    Read the API, don't guess around.
    I did. Believe me I try google before I ask questions. Here is what the API said-

    public static double atan(double a)
    Returns the arc tangent of an angle, in the range of -pi/2 through pi/2. Special cases:
    If the argument is NaN, then the result is NaN.
    If the argument is positive zero, then the result is positive zero; if the argument is negative zero, then the result is negative zero.
    A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

    Parameters:
    a - the double value whose arc tangent is to be returned.
    Returns:
    the arc tangent of the argument.
    But from this I still had no idea what to do.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default

    Quote Originally Posted by MrFish View Post
    Here is what the API said-
    public static double atan(double a)
    Returns the arc tangent of an angle ...
    Can you provide a link to that? The API I refer to says
    Returns the arc tangent of a value ...
    db

  10. #10
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Certainly.

    Java 2 Platform SE v1.3.1: Class Math

    I think it may be an older API. I've been using the older api without issues thus far though.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MrFish View Post
    Certainly.

    Java 2 Platform SE v1.3.1: Class Math

    I think it may be an older API. I've been using the older api without issues thus far though.
    Fun; that old API documentation is dead wrong; as I wrote (see above) the tan( ... ) method takes a number in radians and returns a dimensionless number. That atan( ... ) method works the other way around (as an inverse function should): it takes a dimensionless value and returns a number measured in radians. It's probably a copy/paste error ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yikes!

    You are 4 versions behind.

  13. #13
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    A mathematical definition for tangent in Trigonometry:
    The tangent of an angle is the opposite side over the adjacent side.

    Therefore we can deduce that a tangent takes an angle and returns a unit-less number.

    Arctangent:
    Arctangent is the inverse function of tangent.

    Therefore the arctangent takes a unit-less number and returns an angle.

    In Java these angles have units of radians.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stchman View Post
    A mathematical definition for tangent in Trigonometry:
    The tangent of an angle is the opposite side over the adjacent side.

    Therefore we can deduce that a tangent takes an angle and returns a unit-less number.

    Arctangent:
    Arctangent is the inverse function of tangent.

    Therefore the arctangent takes a unit-less number and returns an angle.

    In Java these angles have units of radians.
    Yep, that's what I already wrote a couple of times; I think the OP doesn't need more repetition of the same.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Why doesn't Math.tan() work?

    Quote Originally Posted by MrFish View Post
    Java Code:
    angle = Math.tan(45);
            
    System.out.println(angle);
    Prints 1.6197751905438615. Should be 1

    What on earth is going on?!
    45 degrees - pi/4.

    Also the word "angle" is not appropriate for the result of the tan calculation. Now the atan method will return an angle.

    This should give you the desired answer:

    Java Code:
    double result = Math.tan( Math.PI / 4.0 );
    
    System.out.println( result );
    Last edited by stchman; 04-29-2012 at 08:15 PM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  2. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  3. Can't get my date math to work
    By pahiker in forum New To Java
    Replies: 8
    Last Post: 07-13-2010, 02:06 AM
  4. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  5. my loop doesn't work.. pls help???
    By ashton in forum New To Java
    Replies: 5
    Last Post: 01-16-2009, 08:24 AM

Posting Permissions

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