Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Division problem

 
Tomm Schuman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the code below print 1.0 if heading is 50? The real answer is 1.8
Rad is a double and heading an int.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If heading is an int, then the result of 180/50/2 will be truncated.



What about Math.toDegrees() and Math.toRadians()

Looking at the source of Math class :

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the toRadians(double degrees) method...

for example



Regards
Prashanth Lingala
 
Tomm Schuman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you like this Commons-Math Library.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic