Rounding & DOUBLE type

This is a discussion on Rounding & DOUBLE type within the C Programming forums, part of the General Programming Boards category; Ive written the program below to determine types of triangle from their side lengths. The problem is that it cannot ...

  1. #1
    Unregistered
    Guest

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

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    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.
    Help everyone you can

  3. #3
    Unregistered
    Guest
    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?

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    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
    Help everyone you can

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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));

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > void main()

    ARRRRG. Jesus, does everyone do this??

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by quzah
    > void main()

    ARRRRG. Jesus, does everyone do this??

    Quzah.
    Hehe, could be a trend!

  8. #8
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Talking

    >void main()

    >ARRRRG. Jesus, does everyone do this??

    >Quzah.

    Just to Avoid the

    return(0)


    and just a Habit.,.....



    Help everyone you can

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  10. #10
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Red face

    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.
    Help everyone you can

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >(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
    My best code is written with the delete key.

  12. #12
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    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.
    Help everyone you can

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >y do u reply to my queries in a fighting spirit??
    I have a personal vendetta against undefined behavior.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21