Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jsmwoolf

macrumors regular
Original poster
Aug 17, 2011
123
0
I've been working on a Calculator application as one of my first big projects. I recently programmed my calculator to give the results of an inverse trig function. It works fine in radians, but gives very weird numbers in degrees that are incorrect. For example, the correct value of inverse sine of 1 in degrees is 90 degrees. However, if I run it through my program, I get the result 0.01745417873758517 for inverse sine of 1.

This deals with the Inverse Sine function.

Code:
		case 25: //Inverse Sine
		{
			isValid=true;
			if((procedure > 1) && (inOperation==false))
			{
			currentNumber=totalNumber;
			}
			if (degreeAngle==true)
			{
				currentNumber=StrictMath.toRadians(currentNumber);
			}
			if ((inOperation==false) && (isValid==true))
			{
			totalNumber = StrictMath.asin(currentNumber);
			}
			else if (isValid==true)
			{
			currentNumber=StrictMath.asin(currentNumber);
			inFunction=true;
			}
			break;
		}

If degreeAngle equals true, then it would convert the number to degrees. But even though you deal with Radians by default, if I use the toDegrees method, then it doesn't yield the correct number.

What's exactly going on? Am I doing something wrong? I'm using Eclipse as my developer software.
 
What language is this? I think Java, but I can't be sure. Can you pull your calls to toRadians and asin into a very short, self-contained program that we can compile and check out? From what you've posted we don't know the types of your variables, etc. which makes this very hard to reproduce. Set a variable to the test value you want and run it, tell us your result, what you expected, etc.

-Lee
 
Code:
public class Inverse {
	public static void main(String[] args) {
		System.out.println(StrictMath.asin(1) + " is in radians");
		System.out.println(StrictMath.asin(StrictMath.toRadians(1)) + " yields an incorrect answer in degrees with a toRadians");
		System.out.println(StrictMath.asin(StrictMath.toDegrees(1)) + " yields a NAN with a toDegrees");
		System.out.println(StrictMath.sin(1) + " is in radians");
		System.out.println(StrictMath.sin(StrictMath.toRadians(1)) + " yields the correct answer in degrees with toRadians");
		System.out.println(StrictMath.sin(StrictMath.toDegrees(1)) + " yields an incorrect answer in degrees with toDegrees");
	}

}

A basic program to yield sine and inverse sine alone, using toRadians, and using toDegrees.

Yes, it is Java.
 
Code:
public class Inverse {
	public static void main(String[] args) {
		System.out.println(StrictMath.asin(1) + " is in radians");
		System.out.println(StrictMath.asin(StrictMath.toRadians(1)) + " yields an incorrect answer in degrees with a toRadians");
		System.out.println(StrictMath.asin(StrictMath.toDegrees(1)) + " yields a NAN with a toDegrees");
		System.out.println(StrictMath.sin(1) + " is in radians");
		System.out.println(StrictMath.sin(StrictMath.toRadians(1)) + " yields the correct answer in degrees with toRadians");
		System.out.println(StrictMath.sin(StrictMath.toDegrees(1)) + " yields an incorrect answer in degrees with toDegrees");
	}

}

A basic program to yield sine and inverse sine alone, using toRadians, and using toDegrees.

Yes, it is Java.

The problem is, you are saying, effectively, arcsin(degreesToRadian(number)) when you want radiansToDegrees(arcsin(number)). The input to arcsin is neither in degrees nor radians, it is a plain number. The output of arcsin is in radians, and that is what you want to convert to degrees.
 
Last edited:
I think you might be a little confused. These functions are all dealing with radians except for toRadians (obviously) which accepts degrees. So, if you want the result of arcsin in degrees:
StrictMath.toDegrees(StrictMath.asin(1))

This:
StrictMath.asin(StrictMath.toDegrees(1))
Is a problem because StrictMath.toDegrees(1) yields:
57.29577951308232
or 180 / pi. When you try to take the arcsin of that, no good, since that number is greater than 1.

Basically the only time you should use toDegrees is to see the "result" of one of these calculations in radians in degrees.

Make sense?

-Lee

Oops, a little late. dylanryan beat me to it.
 
I tried to put the toDegrees part before the trig function, but it still doesn't give me the correct number
Code:
				if (degreeAngle==true)
				{
					totalNumber=StrictMath.toDegrees(StrictMath.sin(1));
				}
				else
				{
					totalNumber=StrictMath.sin(1);
				}
Gives me 48.21273601220948 when degreeAngle is true when inputting 1 rather than 0.017452406437284 which is the correct answer when inputting 1. I'm not trying to use arcsine this time in the example, but it does yield the correct answer when doing it in inverse.
 
sin(1) is
0.84147098 radians
To get degrees,
0.84147098 * (180/pi) = 48.212736 degrees

Sounds like you got the right result.

-Lee
 
Strange! When I input 1 for arcsin in degrees on my TI-84 and the Calculator app from Mac OS X, I get 0.017452406437284, not 48.21273601220948.

EDIT: It was actually sine, not arcsine. My bad.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.