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

1. Member
Join Date
Jan 2010
Posts
43
Rep Power
0

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

3. Member
Join Date
Jan 2010
Posts
43
Rep Power
0
Ah! I've been searching google for "java tan bug" this whole time. Thanks!

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

5. Member
Join Date
Jan 2010
Posts
43
Rep Power
0
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. 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. Originally Posted by MrFish
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

8. Member
Join Date
Jan 2010
Posts
43
Rep Power
0
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. Originally Posted by MrFish
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. Member
Join Date
Jan 2010
Posts
43
Rep Power
0
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. 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

12. Yikes!

You are 4 versions behind.

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

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

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

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

#### Posting Permissions

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