> 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?

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