[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:
• 6
• 6
• 2
4 Solutions

Software engineerCommented:
B = Math.acos(0.5625);
0

Author 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

Commented:
double B = Math.acos(a*a + b*b + c*c) / 2 * a * c);
0

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

The result is given in radians. You need Math.toDegrees
0

Software 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

Commented:
Typo - missing bracket, should be

double B = Math.acos((a*a + b*b + c*c) / 2 * a * c);
0

Software 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

Commented:
Another typo ;-)

double B = Math.acos((a*a - b*b + c*c) / 2 * a * c);
0

Software 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

Commented:
Or

double Bdegrees = Math.toDegrees(Math.acos((a*a - b*b + c*c) / 2 * a * c));
0

Author Commented:
>> cos = Math.cos(Math.toRadians(angle));

thanks. That's what I was missing
0

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

Software engineerCommented:
thanks
0

Commented:
:-)

>>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

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