Problem with functions returns

Guys, I'm having some problems with the code below.
The first of them is on the return from the "funcao" function to "bisseccao", where I have to cast double on the part "intervalo/10". I have found this solution on the web, but couldn't understand why since the function returns double value.
The second (and last) is about the return from "bisseccao" to main, where the variables "bis09" and "bis01" get the value 0 instead of its real value (different from 0 if I print it inside the "bisseccao" function).

Thanks you all in advance.
Leo


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double funcao (double valor, int intervalo);
double bisseccao (double a, double b, int intervalo);

main ()
{
       double a,b,c,d;
       printf("Insira os intervalos para g(T)= 0,1\n");
       scanf("%lf",&a);
       scanf("%lf",&b);
       printf("Insira os intervalos para g(T)= 0,9\n");
       scanf("%lf",&c);
       scanf("%lf",&d);
       double bis09 = bisseccao(c,d,9);   
       double bis01 = bisseccao(a,b,1);
       printf("%lf",bis09);
       printf("%lf",bis01);
       printf("O tempo de subida e : %lf", (bis09 - bis01));
}

double funcao (double valor, int intervalo)
{  
       return (1 - (double)intervalo/10 - (1 + valor + pow(valor,2)/2) * exp(valor * (-1)));
}

double bisseccao (double a, double b, int intervalo)
{
       if (a > b)
       {
             printf("Intervalo incorreto. O valor de \"a\" deve ser menor que o de \"b\".");
             exit(1);
       }
       double funca,funcb;
       funca = funcao(a,intervalo);
       printf("f(a) para 0,%d = %lf\n",intervalo,funca);
       funcb = funcao(b,intervalo);
       printf("f(b) para 0,%d = %lf\n",intervalo,funcb);
       if (funca * funcb > 0)
       {
             printf("Intervalo incorreto. O sinal de \"f(a)\" deve ser contrario ao de \"f(b)\" (intervalo 0,%d).\n",intervalo);
             printf("f(a) = %lf\n",funca);
             printf("f(b) = %lf",funcb);
             exit(1);
       }
       double x,funcx;
       x = (a+b)/2;
       funcx = funcao(x,intervalo);
       printf("a = %lf\n",a);
       printf("b = %lf\n",b);
       if ( (b-a) > pow(10,(-5)) )
       {
             if (funca * funcx < 0)
             {
                       bisseccao(a,x,intervalo);
             }
             else
             {
                       bisseccao(x,b,intervalo);
             }
       }
       else
       {
            return x;
       }
}
Both intervalo and 0 are integers. When you divide two integers, the result is an integer (the remainder is discarded):

11/ 10 = 1
10 / 10 = 1
9 / 10 = 0
8 / 10 = 0
...

When you cast intervalo to a double, you are now dividing a double by an integer. In this case, the compiler will promote the '0' to a double (0.0) and then do floating point division:

11 / 10 = 1.1
10 / 10 = 1.0
9 / 10 = .9
8 / 10 = .8

Hope that helps,
Hey guys !

After long hours looking at the code (and with a little help of course) we`ve found the problem.

The problem is inside the recursive call to "bisseccao" where there was no return. We also found 2 ways to solve it, but i prefered the one that follows :

1
2
3
4
5
6
7
8
9
10
11
12
13
       if ( (b-a) > pow(10,(-5)) )
       {
             if (funca * funcx < 0)
             {
                       bisseccao(a,x,intervalo);
                       return x;
             }
             else
             {
                       bisseccao(x,b,intervalo);
                       return x;
             }
       }


Thanks to all. Problem solved !

Leo
Topic archived. No new replies allowed.