# Rounding & DOUBLE type

• 04-16-2002
Unregistered
Rounding & DOUBLE type
Ive written the program below to determine types of triangle from their side lengths. The problem is that it cannot seem to determine when its a right angled triangle. It gives either acute (all angles less than 90 degrees) or obtuse (one angle greater than 90 degrees).

The side lengths 3, 4 and 5 will give one interior angle of exactly 90 degrees, yet the program produces 89.99999 etc. Im sure its a rounding problem, but can anyone help me rewrite the program to cure it?

Code:

```#include <stdio.h> #include <math.h> int main() {  char another='Y';  double a,b,c,A,B,C;  printf("This is a program to categorise triangles by side length  and angle size\n");  while(another == 'Y' || another == 'y')   {   printf("Enter 3 real numbers for each side of the triangle\n");   scanf("%lf%lf%lf",&a,&b,&c);   if(a>0 && b>0 && c>0)   {     A = (b*b + c*c - a*a)/(2*b*c); /* calculates the value of cosA */     B = (a*a + c*c - b*b)/(2*a*c);     C = (a*a + b*b - c*c)/(2*a*b);     /* checks triangles are possible */     if((A>-1 && A<1) && (B>-1 && B<1) && (C>-1 && C<1))                                {     printf("The triangle you have input has side lengths %lf, %lf, %lf\n\n",a,b,c);             printf("Angle A = %lf radians",acos(A));     printf(" or %lf degrees\n",acos(A)*(180/3.1415927));     printf("Angle B = %lf radians",acos(B));     printf(" or %lf degrees\n",acos(B)*(180/3.1415927));     printf("Angle C = %lf radians",acos(C));     printf(" or %lf degrees\n",acos(C)*(180/3.1415927));         if(a==b && b==c && c==a)printf("\nTriangle is equilateral");     if(a!=b && b!=c && c!=a)printf("\nTriangle is scalene");            if((a==b && c!=a) || (a==c && a!=b) || (b==c && b!=a))printf("\nTriangle is isoceles");     if(acos(A)==acos(0)||acos(B)==acos(0)||acos(C)==acos(0))printf(" and right-angled\n\n");     if(acos(A)>acos(0)||acos(B)>acos(0)||acos(C)>acos(0))printf(" and obtuse\n\n");     else if(acos(A)<acos(0)&&acos(B)<acos(0)&&acos(C)<acos(0))printf(" and acute\n\n");   }   else printf("Triangle is impossible!\n\n");   }   else printf("Input is invalid, lengths must be real numbers\n\n");   printf("Would you like to categorise another triangle?(Y or N)");   another=getchar();   while (another !='Y'&&another !='y'&&another !='n'&&another !='N')   another=getchar();   }  printf("\nThis program is now ending\n\n");  return 0; }```
• 04-16-2002
vsriharsha
Dont u think its better for u to try the same using the sides themselves ... because, C has that problem with Floats and Doubles due to the way it internally represents them...

Try to change it using...

if all three sides (a,b,c) are equal, then it is EQUILATERAL,
if any two are equal then it is ISOSLES
else it is SCALEN...

Regards,
Sriharsha.
• 04-16-2002
Unregistered
the type of triangle in regards to its side lengths works fine, its the type of triangle in regards to its angle sizes that isnt working.

In the code i have if an angle = 90 then it outputs "right angled", but the program is working out an angle that SHOULD be 90 exactly as 89.99999. Is that because of rounding errors or because of the amount of decimal places i used Pi as? Is there a C function or operator for Pi?
• 04-16-2002
vsriharsha
Just go through this simple code...

void main()
{
float a;
a=1.59;
if(a==1.59)
printf("\nEQUAL");
else
printf("\nNOT EQUAL");
}

This will always print NOT EQUAL. Because of the method of internal representation of floats. Actually a is stored as 1.589999...

The moral is.... IF u want to do comparisions in Floats in C then u only do it in the ranges like

if(value>some value && value < somevalue)
-------
else
--------

Because it is very difficult to compare constant floats to them...

Or U have another way out...

Just assign the constant values u want to compare... for example... some

float ra=90.0;

and then try comparing

Regards,
Sriharsha
• 04-16-2002
swoopy
>printf(" or %lf degrees\n",acos(A)*(180/3.1415927));
>printf(" or %lf degrees\n",acos(B)*(180/3.1415927));
>printf(" or %lf degrees\n",acos(C)*(180/3.1415927));

Try:
printf(" or %lf degrees\n",acos(A)*(180./M_PI));
printf(" or %lf degrees\n",acos(B)*(180./M_PI));
printf(" or %lf degrees\n",acos(C)*(180./M_PI));
• 04-16-2002
quzah
> void main()

ARRRRG. Jesus, does everyone do this??

Quzah.
• 04-16-2002
swoopy
Quote:

Originally posted by quzah
> void main()

ARRRRG. Jesus, does everyone do this??

Quzah.

Hehe, could be a trend!
• 04-17-2002
vsriharsha
>void main()

>ARRRRG. Jesus, does everyone do this??

>Quzah.

Just to Avoid the

return(0)

and just a Habit.,.....

:D
• 04-17-2002
Prelude
>Just to Avoid the
>return(0)
Yes, return 0 is a nasty thing that should be removed from the language. Tell me, is it really THAT hard to type return 0?

>and just a Habit
This is no excuse for undefined behavior. Your habit is wrong, change it.

>ARRRRG. Jesus, does everyone do this??
It sure does seem like it, doesn't it?

-Prelude
• 04-17-2002
vsriharsha
Now, I understand that when I say

void main() { .......... }

It informs the OS that the program is not going to return any value to it (and so, it can safely assume the successful value, 0, upon the termination of the program).
If that is correct,
typing void (4 chars) is easier than typing return(0); (10 chars).
;)

Sriharsha.
• 04-17-2002
Prelude
>(and so, it can safely assume the successful value, 0, upon the termination of the program)
Which is an unwarranted assumption. Are you sure that the calling function will receive a correct value? Can you prove it? Will the same thing happen on any platform? If you can answer yes to all of those questions then I'll happily send in a defect report for the ANSI standard to add void main.

-Prelude
• 04-17-2002
vsriharsha
Well Prelude,
Thnx for correcting... I've checked up and found that the behavior of the program can be unexpected and implementation specific if the return type of main is not int.
BTW, y do u reply to my queries in a fighting spirit?? Well anyway thnx again.
• 04-17-2002
Prelude
>y do u reply to my queries in a fighting spirit??
I have a personal vendetta against undefined behavior. :)

-Prelude