3

I have coded a simple calculation app using MIT AppInventor and I want to transfer the exact set of calculations to Java for an Android app but they are different frameworks.So I did my best to trandfer the calculation to Java as shown below.But the Java calculation is wrong in comparison with the Kawa code.

Can someone point out where the Java code is changed from how I'm calculating in the AppInventor code?

This is the Kawa code for the set of calculations,offset depth,legth and duct depth are user inputted values:

 Tri 1=atan(Offset Depth/Offset Length)
 Mark 1=sqrt(Offset Length^2+Offset Depth^2)
 Tri 2=(180-Tri1)/2
 Mark 2=Duct Depth/(tan(Tri 2))

Then this is how I tried to translate that calculation to Java:

tri1,tri2,marking1,marking2 are of type double and the user inputs offsetLength,depth and ductDepth are also double

 tri1 = Math.atan(offsetDepth / offsetLength);
 marking1 = Math.sqrt(Math.pow(offsetLength,2) + Math.pow(offsetDepth,2));  
 tri2 = (180 - tri1) / 2;
 marking2 = ductDepth / Math.tan(tri2);

This is a screenshot of the application so people can get a better understanding of the calculation:

Main screen

Result screen

Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

1

It's possible that some of the divisions are being performed on ints, so the decimals are getting lost (it's hard to tell without knowing the types of the variables). Give this a try, to be sure I'm making explicit the casts:

tri1 = Math.atan(((double)offsetDepth) / offsetLength);
marking1 = Math.sqrt(Math.pow(offsetLength,2) + Math.pow(offsetDepth,2));  
tri2 = (180 - tri1) / 2.0;
marking2 = ductDepth / Math.tan(tri2);

Also, as pointed by @CommonsWare, it's a mistake to assume that Math.PI is equal to 180.

EDIT

You updated the question to indicate that the variables are in fact, doubles. So the above doesn't apply, but I'd advise you to remember that in Java all trigonometric operations receive their input in radians, if you're using degrees (as it seems to be the case, because you're subtracting from 180) then you'll have to make sure that all the operations are getting their inputs in radians, convert the input that is in degrees to radians before passing it to the trigonometric functions.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    Yeah that could be it because I changed the PI to 180 and I'm still getting an incorrect result.I'll implement your solution now and test it.. – Brian Var Dec 14 '13 at 20:05
  • Also to answer your question the tri1,tri2,marking1,marking2 are of type `double` and the user inputs offsetLength,depth and ductDepth are also `double` – Brian Var Dec 14 '13 at 20:08
  • 1
    @BrianJ if they're already `double` then my suggestion is not going to make a difference. The other thing that you have to bear in mind is that in Java all trigonometric operations receive an input in radians, if you're using degrees (as it seems to be the case, because you're subtracting from 180) then you'll have to make sure that all the operations are getting their inputs in radians – Óscar López Dec 14 '13 at 20:10
  • Yes the calculation is using degrees,is there a way I could parse the input so that it's taking degrees instead of radians? – Brian Var Dec 14 '13 at 20:16
  • @BrianJ you'll have to convert the input in degrees to radians before passing it to the trigonometric functions, there's no other way around this. – Óscar López Dec 14 '13 at 20:17
  • Also I can provide further screenshots of what the app is doing so it will make it clearer as to what I'm I'm trying to calculate? – Brian Var Dec 14 '13 at 20:18
  • I'm really confused here,could you give a code example of what you mean? – Brian Var Dec 14 '13 at 20:21
  • I'm not inputting any degrees,just the length,depth and duct depth.I will add screenshots above so you can get a better understanding of what I'm trying to do. – Brian Var Dec 14 '13 at 20:22
  • http://stackoverflow.com/questions/20594658/how-to-convert-a-degrees-calculation-to-radians-kawa-to-java – Brian Var Dec 15 '13 at 13:21