Plz. tell how to calculate angle between 2 vectors(with 2 components) in degrees.in java.Plz. check my code.where is the error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Plz. tell how to calculate angle between 2 vectors(with 2 components) in degrees.in java.Plz. check my code.where is the error?

Code: https://code.sololearn.com/cOF57dV9dry4/

25th Jun 2020, 10:43 AM
Priyankita Srivastava
Priyankita Srivastava - avatar
16 Answers
+ 10
AJ Anant While many will know what you meant in your original answer, the phrase "cast _directly_" may be ambiguous for beginners. It might be clearer to say: "Double floating points aren't _implicitly_ casted to integers." Alternatively, your followup clarification was an example of "_explicitly_ type casting from double to int." I hope my clarification makes sense and is helpful for you. 😉
25th Jun 2020, 12:08 PM
David Carroll
David Carroll - avatar
+ 8
Priyankita Srivastava Math.PI is double value. You can't cast directly with int value. Edited :- You can't assign double value to int value. You need to do type cast.
25th Jun 2020, 10:48 AM
A͢J
A͢J - avatar
+ 6
David Carroll thank you for the commendation. The only Java experience I have is from the Sololearn Java Tutorial and the Code Coach Challenges, where I ran into casting issues. So I am no expert, but glad I could help.
25th Jun 2020, 10:40 PM
Paul K Sadler
Paul K Sadler - avatar
+ 5
Priyankita Srivastava do typecast with int int angle = (int) ((180*r)/(Math.PI));
25th Jun 2020, 11:00 AM
A͢J
A͢J - avatar
+ 4
Priyankita Srivastava The reason for the NaN is the values you provided result in the value of `a` being set to -3.0. The input for arcsine should be between -1 and 1. So... the following will result in NaN: Math.asin(-3.0); Try using the input: 1 3 4 5 That should work without modifying your current code. Otherwise, you may need to convert your -3 value to a single rotation on the sin wave.
25th Jun 2020, 7:04 PM
David Carroll
David Carroll - avatar
+ 4
Priyankita Srivastava in your code you start to get into trouble on this line: float m2=(-l/k); Java is doing integer math and then assigning the result to m2 which is a float (the result of dividing the two integers is zero) You can add some println() methods to see what is happening like these below. float m2=(-l/k); System.out.println(-l/k); outputs 0 ...integer math System.out.println(-2.0/4.0); outputs -0.5 ...as expected If you apply what the others pointed out previously, you should solve your issue. I used your input 1 3 4 2
25th Jun 2020, 7:21 PM
Paul K Sadler
Paul K Sadler - avatar
+ 4
Paul K Sadler You definitely isolated the root source of the issue. 👌 I spent a minute to see why NaN was resulting with the Math.asin(a) line. I hadn't had a chance to figure out what it should have been. I just thought the inputs were invalid for the approach. I'm glad you dug deeper. Nice job!
25th Jun 2020, 10:23 PM
David Carroll
David Carroll - avatar
+ 3
Priyankita Srivastava Yes when you assign double value to a int variable then you should do type cast with int. int r = 2 * 3.14; here 3.14 is double value so it will give error. you can do it like int r = (int) (2 * 3.14) Or you can change int r to double r double r = 2 * 3.14; I didn't get NaN. What was your input? You need to learn about type conversion. https://www.google.com/amp/s/www.geeksforgeeks.org/type-conversion-java-examples/amp/
25th Jun 2020, 12:26 PM
A͢J
A͢J - avatar
+ 2
Priyankita Srivastava Change all int declarations to float or double.
26th Jun 2020, 6:12 AM
David Carroll
David Carroll - avatar
+ 1
Priyankita Srivastava You lost me on your last response. Take a break and come back to it. Also, it would be nice if you also acknowledged what worked as well as what didn't. Otherwise, it just sounds like you aren't making any progress with our respective feedback.
26th Jun 2020, 1:53 PM
David Carroll
David Carroll - avatar
0
So how to do it?
25th Jun 2020, 10:50 AM
Priyankita Srivastava
Priyankita Srivastava - avatar
0
Is there a need to typecast double to any other data type?plz recheck my code.the output is NaN....
25th Jun 2020, 11:58 AM
Priyankita Srivastava
Priyankita Srivastava - avatar
0
I am not typecasting .I have declared all variables as double but still not getting desired output.I think there is some other error in my code. Input - 1 3 4 2 Output - 45(degrees) But my output - NaN Thanks for answering my queries..🙂🙂
25th Jun 2020, 12:49 PM
Priyankita Srivastava
Priyankita Srivastava - avatar
0
Thanks for suggestions & sorry for bothering u once again .I am still badly stuck... Acc. To maths m1 should be -3 m2 should be -0.5, on solving a=-1 That means Math.asin(-1) should return π & thereafter, in deg. 180. But for returning the above input using java pragramming, I am unable to figure out the mistake.Trust me I have tried a no. of times.
26th Jun 2020, 5:43 AM
Priyankita Srivastava
Priyankita Srivastava - avatar
0
I hv already taken all declarations as double as u told.
26th Jun 2020, 1:59 PM
Priyankita Srivastava
Priyankita Srivastava - avatar
- 1
The first 3 outputs are correct bt last 2 are wrong.There is definitely some fault in the following line- double r = Math.asin(a); My inputs are- 1,3,2,1 I am totally exhausted with this.
26th Jun 2020, 1:24 PM
Priyankita Srivastava
Priyankita Srivastava - avatar