[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Trigonometry

I am attempting to find the angle of a triangle based on the length of the sides. I have no knowledge of any angles of the triangle.

I am using the equation: b^2 = a^2 + c^2  - 2ac cos B

where ^ denotes power of
B is the angle I am trying to find
b is the length of the side opposite B
a and c are the lengths of th other two sides.

So, if I have the data; b = 5, c = 4, a = 6

25 = 16 + 36 - 2*4*6 cos B
=> 25 = 52 - 48 cos B
=> 48 cos B = 27
=> cos B = 27/48
=> cos B = 0.5625

Is there a method to find B on its own? i.e. inverse of cos B?

0
krispin
Asked:
krispin
  • 6
  • 6
  • 2
4 Solutions
 
zzynxSoftware engineerCommented:
B = Math.acos(0.5625);
0
 
krispinAuthor Commented:
I thought that myself but it doesn't seem to work. I know the arc cosine should be the inverse of cosine, but java doesn't seem to calculate it correctly. For example:

double angle = 45;
double cos;
            
cos = Math.cos(angle);
            
angle = Math.acos(cos);
            
System.out.println(cos);
System.out.println(angle);


The println on angle at the end does not print out 45

0
 
CEHJCommented:
double B = Math.acos(a*a + b*b + c*c) / 2 * a * c);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>The println on angle at the end does not print out 45

The result is given in radians. You need Math.toDegrees
0
 
zzynxSoftware engineerCommented:
>> The result is given in radians. You need Math.toDegrees
Exactly.
From the docs:
acos returns the arc cosine of an angle, in the range of 0.0 through pi.
0
 
CEHJCommented:
Typo - missing bracket, should be

 double B = Math.acos((a*a + b*b + c*c) / 2 * a * c);
0
 
zzynxSoftware engineerCommented:
>> You need Math.toDegrees
But as the doc says:
The conversion from radians to degrees is generally inexact;
Users should not expect cos(toRadians(90.0)) to exactly equal 0.0.
0
 
CEHJCommented:
Another typo ;-)


 double B = Math.acos((a*a - b*b + c*c) / 2 * a * c);
0
 
zzynxSoftware engineerCommented:
System.out.println(Math.toDegrees(Math.acos(Math.cos(Math.toRadians(45.0)))));
returns
    45.0

So, your code should be:

double angle = 45;
double cos;
         
cos = Math.cos(Math.toRadians(angle));
angle = Math.acos(cos);
         
System.out.println(cos);
System.out.println(Math.toDegress(angle));
0
 
CEHJCommented:
Or

double Bdegrees = Math.toDegrees(Math.acos((a*a - b*b + c*c) / 2 * a * c));
0
 
krispinAuthor Commented:
>> cos = Math.cos(Math.toRadians(angle));

thanks. That's what I was missing
0
 
zzynxSoftware engineerCommented:
>> thanks. That's what I was missing
Yep ;°)
0
 
zzynxSoftware engineerCommented:
thanks
0
 
CEHJCommented:
:-)

>>thanks. That's what I was missing

Well not really ;-) My last comment shows you don't need to introduce any call to toRadians. You don't even need to call toDegrees if you don't want a degree result
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now