# Thread: Need help with function and returning double

1. ## 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.

2. I don't see where you're calling your "double" function.

3. s=(a, b , c); i just forgot the 2nd S

4. What happens if a=b=c=0? What will be the return value of S()?

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. If i would ask to print S() it would print 0

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

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

10. Thanks for the help guys