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

• 08-09-2011, 06:37 AM
MrFish
Why doesn't Math.tan() work?
Code:

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

What on earth is going on?!
• 08-09-2011, 06:39 AM
JosAH
The trig methods take radians, not degrees. See the Math class for methods that convert degrees to radians.

kind regards,

Jos
• 08-09-2011, 06:41 AM
MrFish
Ah! I've been searching google for "java tan bug" this whole time. Thanks!
• 08-09-2011, 06:49 AM
JosAH
Quote:

Originally Posted by MrFish
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
• 08-09-2011, 06:54 AM
MrFish
Ok. Sorry to come back with another question but I'm pretty bad at math in general.

Code:

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

Prints angle: 0.017451520651465824

This works-

Code:

```angle = Math.tan(Math.toRadians(45));                 System.out.println("angle: " + angle);```
Prints 0.9999 (repeating)
• 08-09-2011, 06:58 AM
DarrylBurke
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
• 08-09-2011, 07:01 AM
JosAH
Quote:

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

Code:

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

Prints angle: 0.017451520651465824

This works-

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:

Code:

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

kind regards,

Jos
• 08-09-2011, 07:10 AM
MrFish
Ok. Again thank you. This saved me from an hour of hair pulling.

Quote:

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

Quote:

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.
• 08-09-2011, 07:24 AM
DarrylBurke
Quote:

Originally Posted by MrFish
Here is what the API said-
Quote:

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
Quote:

Returns the arc tangent of a value ...
db
• 08-09-2011, 08:11 AM
MrFish
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.
• 08-09-2011, 08:20 AM
JosAH
Quote:

Originally Posted by MrFish
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
• 08-09-2011, 08:20 AM
Junky
Yikes!

You are 4 versions behind.
• 08-09-2011, 10:14 AM
stchman
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.
• 08-09-2011, 10:33 AM
JosAH
Quote:

Originally Posted by stchman
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
• 04-29-2012, 08:13 PM
stchman
Re: Why doesn't Math.tan() work?
Quote:

Originally Posted by MrFish
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:

Code:

```double result = Math.tan( Math.PI / 4.0 ); System.out.println( result );```