This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problems with Math.tan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems with Math.tan" Watch "problems with Math.tan" New topic
Author

problems with Math.tan

Kevin de Berk
Greenhorn

Joined: Nov 04, 2003
Posts: 3
The following code:

Gives the following output:
degrees = 72.0
tanDegrees = -0.26241737750193517

While my calculator returns something different:
3.077683537

What am I doing wrong in the Java source?
[ January 10, 2004: Message edited by: Kevin de Berk ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You're assuming that the tan funtion uses degrees. It doesn't - the API says it uses radians (pi=3.1419... radians equals 180 degrees). Most calculators have several possible modes - a "DRG" button toggles between degrees mode, radians mode, and grad mode (which has 100 grads in a 90 degree angle; seldom used). Your calculator is currently in degrees mode. Java's Math class doesn't have these modes; it always uses radians because that's the most mathematically natural mode to use. (Computations are actually simplest for the computer this way.) But you can use the toDegrees() and toRadians() methods to convert between radians and degrees.


"I'm not back." - Bill Harding, Twister
Kevin de Berk
Greenhorn

Joined: Nov 04, 2003
Posts: 3
Oh..
Well, thanks mate
 
jQuery in Action, 2nd edition
 
subject: problems with Math.tan
 
Similar Threads
did you know that cos(90) != 0
how degrees can be converted to radians
What operations produce NaN?
comparison methods
How to calculate the angle of 2 points on a circle in clockwise direction