Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Division problem

Tomm Schuman
Greenhorn
Posts: 11
Why does the code below print 1.0 if heading is 50? The real answer is 1.8

I'm trying to convert degrees to rad, tell me if there's a better way to do it than the one i use. Thx in advance

Carol Enderlin
drifter
Ranch Hand
Posts: 1364
If heading is an int, then the result of 180/50/2 will be truncated.

Looking at the source of Math class :

Prashanth Lingala
Ranch Hand
Posts: 66

for example

Regards
Prashanth Lingala

Tomm Schuman
Greenhorn
Posts: 11
O there was already a method which does that

I have had sin,cos,tan in school but I don't really know how to use them in programming. Could someone give me an example about how you calculate the X in the triangle below.

Layne Lund
Ranch Hand
Posts: 3061
Using a little bit of trigonometry, you can set up an equation:

Solving this for x, you get

You can convert this almost directly into Java using the Math class:

The Math class also provides methods for cosine, tangent, and other nifty mathematical functions. See the link provided above for the Javadocs for this class.

HTH

Layne
[ March 10, 2005: Message edited by: Layne Lund ]

Surasak Leenapongpanit
Ranch Hand
Posts: 341
May be you like this Commons-Math Library.