1

I'm trying to convert the variable tri2 to degress by wrapping it in the toDegree() and setting it to a new variable called triDegree. But when I cast the variable and run the application I get the following in my logcat: http://pastebin.com/HKDSAuQK

The application was running fine and calculating up to the point where I converted it to degrees. Can anyone see what is wrong with my implementation here?

public void onClick(View v) {
    // TODO Auto-generated method stub
    try {
        String getoffsetlength = data.offsetLength.getText().toString(); 
        String getoffsetdepth = data.offsetDepth.getText().toString(); 
        String getductdepth = data.ductDepth.getText().toString(); 

        double tri1,tri2;
        double marking1,marking2;
        double triDegree;

        double off1 = Double.parseDouble(getoffsetlength);//length
        double off2 = Double.parseDouble(getoffsetdepth);//depth
        double off3 = Double.parseDouble(getductdepth);//duct depth

        marking1 = Math.sqrt(Math.pow(off1,2) + Math.pow(off2,2));
        tri1 = Math.atan(off2 / off1);
        tri2 = (180 - tri1) / 2;
        triDegree = Math.toDegrees(tri2);
        marking2 = off3 / Math.tan(triDegree);

        Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
        myIntent.putExtra("number1", marking1);
        myIntent.putExtra("number2", marking2);
        startActivity(myIntent);
        //make a toast 
        Toast.makeText( getBaseContext(), "Calculating!", Toast.LENGTH_SHORT
             ).show(); 
    }
    catch (NumberFormatException e) {
        // TODO: handle exception
        System.out.println("Must enter a numeric value!");
    }
}
Jost
  • 1,549
  • 12
  • 18
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

2

The error happens here:

tri2 = (180 - tri1) / 2;

At this point, you treat tri1 as degrees, before the conversion. If you want to subtract an angle from the value of the "straight" 180° angle, use π instead of 180, the measure of the straight angle in radians:

tri2 = (Math.PI - tri1) / 2;

This call is also incorrect

triDegree = Math.toDegrees(tri2);
marking2 = off3 / Math.tan(triDegree);

because triDegree is expressed in degrees, while tan is expecting a value expressed in radians.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So I should change `tri1` to degrees before its used in `tri2` .Could you show a code example of how to correct this? – Brian Var Oct 10 '13 at 13:41
  • @BrianJ Either that, or use `Math.PI`, the equivalent of 180 when angles are measured in radians. – Sergey Kalinichenko Oct 10 '13 at 13:42
  • I tried this but it's still crashing.Not sure what the problem is in the code? – Brian Var Oct 10 '13 at 13:45
  • So instead I should remove `triDegree` and use the original `tri2` variable in the `marking` calculation? But then how do I convert this to degrees for the output? – Brian Var Oct 10 '13 at 15:33
  • 1
    @BrianJ Keep the number in radians until the moment when you are ready to do the output, then call `Math.toDegrees` to show the value in degrees. – Sergey Kalinichenko Oct 10 '13 at 15:35
  • `tri2 = (180 - tri1) / 2;` `marking2 = off3 / Math.tan(tri2);` `marking2final = toDegree(marking2);` – Brian Var Oct 11 '13 at 20:51
  • I didn't try the previous yet but do you think it would work?I just cast marking2 toDegree which is then passed to the result activity. – Brian Var Oct 11 '13 at 20:54
  • @BrianJ Subtracting `tri1` which is radians from 180 which is degrees is incorrect. Dividing a distance `off3` by a tangent does not give you an angle, so converting `marking2` to degrees is wrong, too. – Sergey Kalinichenko Oct 11 '13 at 21:19
  • @BrianJ I do not understand what you are trying to do, but whatever it is, it's a math problem, not a programming problem. Consider making a careful drawing of it, and post a question to [math.stackexchange.com](http://math.stackexchange.com). There's a bunch of people who are much better at trigonometry than me, they should be able to help you derive whatever formulas that you need. Good luck! – Sergey Kalinichenko Oct 11 '13 at 21:20