Prints 1.6197751905438615. Should be 1Code:`angle = Math.tan(45);`

System.out.println(angle);

What on earth is going on?!

Printable View

- 08-09-2011, 06:37 AMMrFishWhy doesn't Math.tan() work?Code:
`angle = Math.tan(45);`

System.out.println(angle);

What on earth is going on?! - 08-09-2011, 06:39 AMJosAH
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 AMMrFish
Ah! I've been searching google for "java tan bug" this whole time. Thanks!

- 08-09-2011, 06:49 AMJosAH
- 08-09-2011, 06:54 AMMrFish
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);

Prints angle: 0.017451520651465824

This works-

Code:`angle = Math.tan(Math.toRadians(45));`

System.out.println("angle: " + angle);

- 08-09-2011, 06:58 AMDarrylBurke
Read the API, don't guess around. In any case, why should the argument to

**a**tan(...) be in radians (or even degrees, for that matter)?

db - 08-09-2011, 07:01 AMJosAH
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));`

kind regards,

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

Quote:

Read the API, don't guess around.

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.

- 08-09-2011, 07:24 AMDarrylBurke
- 08-09-2011, 08:11 AMMrFish
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 AMJosAH
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 AMJunky
Yikes!

You are 4 versions behind. - 08-09-2011, 10:14 AMstchman
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 AMJosAH
- 04-29-2012, 08:13 PMstchmanRe: Why doesn't Math.tan() work?