Thread: Need help with function and returning double

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    5

    Need help with function and returning double

    Code:
    
    
    Code:
    void duom(double &a, double &b, double &c)
    {
        ifstream in("sk.txt");
        in >> a;
        in >> b;
        in >> c;
        in.close();
    }
    
    
    double S(double a, double b, double c)
    {
        double p;
        double S;
        if ((a+b>c) && (a+c>b) && (b+c>a))
        {
        p = (a+b+c)/2;
        S = sqrt(p*(p-a)*(p-b)*(p-c));
        }
        return S;
    }
    
    int main()
    {
        double a;
        double b;
        double c;
        double s;
        duom(a , b , c);
        s=(a, b , c);
        if(s>0)
        {
            cout << "area of a triangle is  " << s;
        }
         else
        {
            cout << "triangle does not exist" << endl;
        }
        return 0;
    for some reason the double function does not work that is the program just skips the whole thing.
    Last edited by Kaza; 02-28-2019 at 04:31 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't see where you're calling your "double" function.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    s=(a, b , c); i just forgot the 2nd S

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    What happens if a=b=c=0? What will be the return value of S()?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    Well it should fail 2nd test and print triangle does not exist but i also rewrote the code. Now it checks if the triangle is real at the end and prints the answer based on that

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    If i would ask to print S() it would print 0

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Pay attention... S is not initialized inside the function (and I recommend to rename the variable to s, lower case) if the condition isn't satisfied...

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Your function should be:

    Code:
    double S(double a, double b, double c)
    {
      double p;
      double s = 0.0;  // NOW s == 0.0 by default!
    
      if ((a+b>c) && (a+c>b) && (b+c>a))
      {
        p = (a+b+c)/2;
        s = sqrt(p*(p-a)*(p-b)*(p-c));
      }
    
      return s;
    }

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Second: Consider using other way to calculate the triangle's area than Heron's Formula... It isn't that stable for very small angles. For instance:

    Code:
    $ bc -lq
    scale=50
    a=10^5
    b=10^-5
    c=sqrt(a^2+b^2)
    d=(a*b)/2
    s=(a+b+c)/2
    e=sqrt(s*(s-a)*(s-b)*(s-c))
    d
    .50000000000000000000000000000000000000000000000000
    e
    .49999999999999999999999999999999999999999999999998
    Here, a, b and c are sides of a right triangle (note I calculated the hypotenuse to apply the usual formula). d uses the usual formula and e is Heron's Formula (using s).

    In this example a is very big and b is very small, so the angle between a and the hypotenuse is very small as well (approx 10^-10 degrees)...

    The area is exactly 0.5 (d = a*b/2 = 10^5*10^-5/2 = 1/2), but Heron's Formula is slightly off: 0.49999...

    The double type doesn't have all that precision (it garantees precision for just 16 decimal algarisms: approx 53*log(2)). To use long double isn't a good solution (only 19 algarisms of precision are garanteed). You can use multiple precision arithmetic libraries like libgmp, but will end up with the same problem above (bc uses libgmp)...

    The problem can be worst: since you cannot have precise representation of integers greater then 2^53 - 1 with double type (or 2^64-1 for long double), the error will be greater because when you multiply 3 big factors (if a, b and c are big), maybe you will loose bits in the process... It is always good to remember: floating point arithmetic doesn't apply to real domain, but rational domain. A floating point representation is always a fraction between 2 integers (except for NaNs and INFs).
    Last edited by flp1969; 02-28-2019 at 08:07 PM.

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    Thanks for the help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a double matrix
    By adarpodracir in forum C Programming
    Replies: 5
    Last Post: 03-25-2012, 03:20 PM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  4. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  5. returning a double array [][]
    By Morrow in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2004, 10:01 AM

Tags for this Thread