Java – Logic errors in Java programming

Logic errors in Java programming… here is a solution to the problem.

Logic errors in Java programming

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

Guys, when number = 45, I can’t get “your result is 1” through this code. Please help.
Since tan(45)=1 degrees. I have converted it. But there is no desired result.

Solution

To convert degrees to

radians, you first need to convert the degrees to a factor (circumference) by dividing by 360 degrees. Next, multiply by 2PI rad (which is the circumference of the “unit circle”).

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

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

temp = (float) (number * 0.0174532925);

Also, it would be better (clearer) if you didn’t use the “Magic Number” and added comments (so people know what you’re doing):

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

and/or even use the available Java features:

// Convert to rad
temp = Math.toRadians(number);

Related Problems and Solutions