Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical error in Java programming

Tags:

java

android

math

case R.id.bTanx:
        temp=(float) (number/0.0174532925);
        num=Math.tan(temp);
        display.setText("Your Result is   " + num);

Guys I'm not able to get "Your Result is 1" when number = 45 ,by this code.Please help. As tan(45)=1 in degrees.i have converted it.but no desired result.

like image 793
user3118200 Avatar asked Mar 03 '26 09:03

user3118200


1 Answers

To convert degrees to radian you first need to convert the degrees to a factor (of the circles circumference) by dividing by 360 degrees. Next you multiply by 2PI rad (which is the circumference of a 'unit circle').

When looking at the units you do this: degrees / degrees * radians = radians

So where you divide by 0.017 (2*PI / 360), you need to multiply instead:

temp = (float) (number * 0.0174532925);

Furthermore it is nicer (more clear) if you do not use 'magic numbers' and add comments (so people know what you are doing):

// Convert to rad
temp = (float) (number * 2 * Math.PI / 360);

And/or even use the available Java functionality:

// Convert to rad
temp = Math.toRadians(number);
like image 113
Veger Avatar answered Mar 06 '26 00:03

Veger