# anyone with very good math? need your help

• 03-27-2014, 08:31 PM
gevin20
anyone with very good math? need your help
problem statement--

Consider a triangle with three sides measuring a, b, and c units. A triangle is a right-angled triangle if
(a*a)+ (b*b) = (c*c)
Allow a tolerance of 0.000001 in the comparison in the above case i.e
(a*a) + (b*b) = (c*c) +/- 0.000001
A triangle is an isosceles triangle if any two of its sides are equal. A triangle is an equilateral triangle if all the three sides are equal. Three values can be the dimensions of a triangle if and only if the sum of every pair of values is greater than the third value. Otherwise, it is an invalid triangle.
Write a program that reads three real numbers and finds out whether they can be the sides of the triangle and if they do, prints what type of triangle it is. Even though all equilateral triangles are isosceles, your program should classify an equilateral triangle to be an equilateral only. Similarly isosceles right-angled triangles should be classified as right-angled and not isosceles.

Code:

``` public class First {                 public static void main(String[] args)         {                 double a,b,c,d,e,f;                 try                 {                         Scanner sc=new Scanner(System.in);                         a=sc.nextDouble();                         b=sc.nextDouble();                         c=sc.nextDouble();                         d=(a*a)+(b*b);                 e=(a*a)+(c*c);                 f=(b*b)+(c*c);                 if((a>(b+c))||(b>(a+c))||(c>(a+b)))                 {                         System.out.println("invalid");                 }                 else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))                 ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001))))                 {                         System.out.println("right-angled");                 }                 else if(((a==c)&&(a!=b))||((a==b)&&(a!=c))||((b==c)&&(b!=a)))                 {                         System.out.println("isosceles");                 }                 else if((a==b)&&(b==c)&&(a==c))                 {                         System.out.println("equilateral");                 }                 }                 catch(Exception ex)                 {                         ex.printStackTrace();                 }                                }         }```
the code is failing when i give isosceles right angle triangle.it is printing Isosceles instead of Right-angled. What changes should i do??
• 03-27-2014, 08:44 PM
Norm
Re: anyone with very good math? need your help
Quote:

when i give isosceles right angle triangle.it is printing Isosceles instead of Right-angled.
That would say that all the boolean expressions in the else if statement are false. Which one should be true for the if to be true?
• 03-27-2014, 09:16 PM
gevin20
Re: anyone with very good math? need your help
consider a=8.00000 b=8.000000 and c=11.3137085
then
(8*8)+(8*8)=(11.3137085*11.3137085)
i.e right angle triangle but it is showing isosceles
• 03-27-2014, 09:25 PM
Norm
Re: anyone with very good math? need your help
What are the values of those two expressions?
It would be unusual for the product of int values to be equal to the product of floating point numbers.

How close would those two values need to be for you to consider them to be "equal"?
• 03-27-2014, 09:58 PM
jim829
Re: anyone with very good math? need your help
A couple of things to think about.

1. Would it be easier to figure all this out if you knew which side was the largest, the smallest, and the one in the middle (regardless of whether any
of them are equal or not)?
2. There are only five possibilities. Invalid, Right, Isoceles, Equilateral, and Scalene. If any four of those five fail the test, guess what's left. This could help you structure your code (even though you aren't asked to determine if the tirangle is scalene or not).

Regards,
Jim
• 03-28-2014, 08:44 AM
gevin20
Re: anyone with very good math? need your help
Quote:

Originally Posted by Norm
What are the values of those two expressions?
It would be unusual for the product of int values to be equal to the product of floating point numbers.

How close would those two values need to be for you to consider them to be "equal"?

there is not a single int in program all variable's are of double type .in your view where should i make changes?
• 03-28-2014, 12:59 PM
Norm
Re: anyone with very good math? need your help
Quote:

where should i make changes?
How close in value should the values be to consider them equal? Did you print out the the two values in post#3 and compare them?
Perhaps something like this: Math.abs(value1 - value2) < CloseEnough
where CloseEnough = 0.00001
• 03-28-2014, 01:04 PM
Tolls
Re: anyone with very good math? need your help
This is the bit of the requirements you posted that Norm is suggesting you are missing:
"Allow a tolerance of 0.000001 in the comparison in the above case."
• 03-28-2014, 01:51 PM
gimbal2
Re: anyone with very good math? need your help
... but it is not missing is it? I mean I see this in the posted code which seems to match exactly what the requirements state:

Code:

```else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))                 ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001))))```
I must be really stupid, what am I not seeing that you guys are?
• 03-28-2014, 02:00 PM
jim829
Re: anyone with very good math? need your help
The OP is checking tolerances with == and not < or >. That is wrong. Use Norm's suggestion.

Regards,
Jim
• 03-28-2014, 02:06 PM
Tolls
Re: anyone with very good math? need your help
Quote:

Originally Posted by gimbal2
... but it is not missing is it? I mean I see this in the posted code which seems to match exactly what the requirements state:

Code:

```else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))                 ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001))))```
I must be really stupid, what am I not seeing that you guys are?

What if c*c equals d + 0.0000005 (that's half of 0.000001, so you don't have to count the zeros...:))
• 03-28-2014, 02:21 PM
gimbal2
Re: anyone with very good math? need your help
DOH! Yes, I'm stupid. Oh man I'm going to feel ashamed for this one for a loooooooong time.
• 03-28-2014, 04:04 PM
Tolls
Re: anyone with very good math? need your help
It's Friday...:)