Inverse Sine gives inaccurate result in degrees

Discussion in 'Mac Programming' started by jsmwoolf, Aug 21, 2011.

  1. jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    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.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #3
    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.
     
  4. dylanryan, Aug 21, 2011
    Last edited: Aug 21, 2011

    dylanryan macrumors member

    Joined:
    Aug 21, 2011
    #4
    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.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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.
     
  6. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #6
    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.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    sin(1) is
    0.84147098 radians
    To get degrees,
    0.84147098 * (180/pi) = 48.212736 degrees

    Sounds like you got the right result.

    -Lee
     
  8. jsmwoolf, Aug 21, 2011
    Last edited: Aug 21, 2011

    jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #8
    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.
     

Share This Page