Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Dealing with Degrees and Radians

Thread view: 
dimo414 - 05 Sep 2006 07:27 GMT
Hello,

I'm trying to determine the length of the opposite leg of a triangle
using the Math.tan() function.  However it seems Math is set in radians
(at least, I'm getting negative numbers returned from tangent, and I
belive that's not possible in degrees) and I can't figure out how to
tell it to run in degrees.  I tried using Math.toDegrees() but I still
receive strange/negative numbers for tangent.

Anyone know how to change this setting?
Gordon Beaton - 05 Sep 2006 07:46 GMT
> I'm trying to determine the length of the opposite leg of a triangle
> using the Math.tan() function. However it seems Math is set in
> radians (at least, I'm getting negative numbers returned from
> tangent, and I belive that's not possible in degrees)

The units used to express angles do not change the range of possible
values of the tangent function. A given angle has a given tangent
regardless of whether it's expressed in degrees or radians.

However the trig methods in java.lang.Math require values in radians,
and will give you the wrong results otherwise, as you've discovered.

> and I can't figure out how to tell it to run in degrees. I tried
> using Math.toDegrees() but I still receive strange/negative numbers
> for tangent.
>
> Anyone know how to change this setting?

Math.toDegrees() and Math.toRadians() aren't settings, they're methods
for converting values betweem radians and degrees (and vice versa).

To use the trig methods with degrees, convert as necessary:

 double degrees = 60;
 double tan = Math.tan(Math.toRadians(degrees));

Perhaps you should read the API descriptions for the methods you're
using.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

dimo414 - 05 Sep 2006 21:29 GMT
> > and I can't figure out how to tell it to run in degrees. I tried
> > using Math.toDegrees() but I still receive strange/negative numbers
[quoted text clipped - 12 lines]
> Perhaps you should read the API descriptions for the methods you're
> using.

I used Math.toDegrees() after reading the API descriptions; after
looking at your example, I realize I should have been using
Math.toRadians to convert my input to a radian unit.  My script works
now, so thank you.  However, Math.toRadians/toDegrees are just
approximations.  This works for my current needs, but is there any way
to actually tell java, or Math, to process and return trigonometric
functions in degrees instead?
Oliver Wong - 05 Sep 2006 22:09 GMT
>> > and I can't figure out how to tell it to run in degrees. I tried
>> > using Math.toDegrees() but I still receive strange/negative numbers
[quoted text clipped - 20 lines]
> to actually tell java, or Math, to process and return trigonometric
> functions in degrees instead?

   You can't tell Math.

   To "tell Java" you would probably end up re-implementing much of Math.
Perhaps get a book on numerical computation to avoid falling into common
pitfalls involved with floating point arithmatic where high precision is
required.

   I'd recommend you design your program to use radians internally
everywhere, and only convert to degrees right before displaying it to the
user, assuming that the reason for your preference for degrees over radians
is for the convenience of the end user.

   - Oliver
Matt Humphrey - 05 Sep 2006 22:22 GMT
>> > and I can't figure out how to tell it to run in degrees. I tried
>> > using Math.toDegrees() but I still receive strange/negative numbers
[quoted text clipped - 20 lines]
> to actually tell java, or Math, to process and return trigonometric
> functions in degrees instead?

I'm not sure, but I'd bet that the Math routines are really just interfaces
to floating point instructions and I wouldn't count on there being a set for
radians and a set for degrees.  The real problem, of course, is that
converting radians to degrees means multiplying by the floating point
representation of PI / 180 which is can be only an approximation of the real
PI / 180, which is irrational.

So I thought I'd try it out with this test program:

 int d = 90;
 double t = d;

 System.out.println ("cos(toRadians(double)) " +
Math.cos(Math.toRadians(t)));
 System.out.println ("cos(double * PI / 180) " + Math.cos(t * Math.PI /
180));
 System.out.println ("cos(int * PI / 180) " + Math.cos(d * Math.PI / 180));
 System.out.println ("cos(PI / 2) " + Math.cos(Math.PI / 2));

Which printed for output
cos(toRadians(double)) 6.123233995736766E-17
cos(double * PI / 180) 6.123233995736766E-17
cos(int * PI / 180) 6.123233995736766E-17
cos(PI / 2) 6.123233995736766E-17

Which shows the imperfection and is nicely consistent.

So I think the answer is that you can't get to PI / 2 close enough this way
to make cos give you the exact value for output.  There may be some clever
way to finagle PI / 2 to yield the correct result, but you're probably
better off using standard precision approaches, such as choosing a value for
epsilon so that something as close to 0 as 0.00000000000000006123233... will
count as 0.

static final double Epsilon = 0.0000000001;
static double cos (double a) {
 double t = cos (a);
 if (Math.abs (t) < Epsilon) return 0;
 if ((1 - t) < Epsilon)  return 1;
 if ((1 + t) < Epsilon) return -1;
 return t;
}

There may be better ways to do this--numerical analysis wasn't my strong
point.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Dale King - 06 Sep 2006 05:10 GMT
> I used Math.toDegrees() after reading the API descriptions; after
> looking at your example, I realize I should have been using
> Math.toRadians to convert my input to a radian unit.  My script works
> now, so thank you.  However, Math.toRadians/toDegrees are just
> approximations.

And the tan and other trigonometric functions are just approximations as
well. In addition, floating point numbers are approximations as well. See

> This works for my current needs, but is there any way
> to actually tell java, or Math, to process and return trigonometric
> functions in degrees instead?

If it did so, it would have to simply convert to radians using the same
method that you would use by calling toRadians. The reason is that radians
is not just some arbitrary choice. It has a mathematical significance. And
there are formulas for computing things like tangent using an angle
specified in radians (limited to a range of angles). This is a power series
for computing tangent.

 tan( x ) = x + (x^3)/3 + 2(x^5)/15 + 17(x^7)/315 + ...  for |x| < pi / 2

This is an infinite series so you get into an approximation first by how
many terms in the series you want to include. Note that this formula only
involves integer values besides x so is relatively straightforward to
compute. Since degrees are simple scaling you could create a formula in
terms of degrees, but that formula is going to involve powers of PI in the
denominator and cannot be computed easily.

It is much easier for computers to compute using radians. It is also not
that difficult for programmers to work in radians as Oliver suggested and
only convert to degrees when presenting values to the user.

Signature

Dale King

Dale King - 06 Sep 2006 14:10 GMT
>> I used Math.toDegrees() after reading the API descriptions; after
>> looking at your example, I realize I should have been using
[quoted text clipped - 4 lines]
> And the tan and other trigonometric functions are just approximations as
> well. In addition, floating point numbers are approximations as well. See

I meant the tan and trigonometric functions in Java or any other computer
language are approximations. The mathematical functions are of course exact,
but form an infinite series.

And I meant to say see
http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf.

Signature

Dale King



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.