Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top