Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code error?

Status
Not open for further replies.

rzrdigo

Programmer
Jan 7, 2011
19
BR
Does anybody have any idea why this code doesnt work?
the error is in the ACHA_FI_P() function... the problem is I couldnt debug this code using dev c++, anybody know a good debugger?
the ACHA_FI_P function is to resolve 2 equations to find values of Fi2 and Fi3 that zero W and Z. any help will be very apreciated!



#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

double ERF(double Z),ERFC(double A);
float A;
int ACHA_FI_P();
float Fi2,Fi3,FiP[2];


int main()
{
double B,C,D;
while(1)
{
printf("Entre com o valor de A:");

scanf("%f", &A);
printf("A= %f \n",A);
B=ERF(A);
C=ERFC(A);
printf("ERF(A)=%f\n", B);
printf("ERFC(A)=%f\n\n", C);
ACHA_FI_P();
printf("\nValores obtidos para Fi2 e Fi3 sao respectivamente %f e %f.",FiP[0],FiP[1]);
}

return(0);


}

double ERF(double Z)
{
int P;
double a = 0.348;
double b = -0.096;
double c = 0.748;
double d = 0.471;
double y, ERF;

if (Z < 0) { Z = -Z; P = -1;}
else { P = 1; }

y = 1./(1. + (Z * d));

ERF = 1 - ((a * y) + (b * y*y) + (c * y*y*y))*exp(-(Z*Z));

if(P == -1) ERF = -ERF;

return(ERF);
}

double ERFC(double A)
{
double ERFC;
ERFC=(1-ERF(A));
return(ERFC);
}



int ACHA_FI_P ()
{
float dFi,W,Z;

Fi2=0.001;
Fi3=0.001;
dFi=0.001;

while (1)
{
FiP[0]=Fi2;
FiP[1]=Fi3;
W=(exp(-Fi2*Fi2)/ERF(Fi2)-exp(-Fi2*Fi2)/(ERF(Fi3)-ERF(Fi2))-sqrt(3.14));
Z=((exp(-Fi3*Fi3))/(ERF(Fi3)-ERF(Fi2))+(exp(-Fi3*Fi3))/(ERFC(Fi3))-sqrt(3.14));

if((W==0||(W<=0.01 && W>=-0.01))&&((Z==0)||(Z<=0.01 && Z>=-0.01))) {return(0);}

Fi2=Fi2+dFi;

if(Fi2==4){Fi2=0.001; Fi3=Fi3+dFi;}
if(Fi3==4){printf("Nenhum valor encontrado.\n");}
}
}
 
First, post your code in code tags, so it looks nicely indented, like so.
Code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

double ERF(double Z), ERFC(double A);
float A;
int ACHA_FI_P();
float Fi2, Fi3, FiP[2];

int main()
{
  double B, C, D;
  while (1) {
    printf("Entre com o valor de A:");

    scanf("%f", &A);
    printf("A= %f \n", A);
    B = ERF(A);
    C = ERFC(A);
    printf("ERF(A)=%f\n", B);
    printf("ERFC(A)=%f\n\n", C);
    ACHA_FI_P();
    printf("\nValores obtidos para Fi2 e Fi3 sao respectivamente %f e %f.",
           FiP[0], FiP[1]);
  }

  return (0);
}

double ERF(double Z)
{
  int P;
  double a = 0.348;
  double b = -0.096;
  double c = 0.748;
  double d = 0.471;
  double y, ERF;

  if (Z < 0) {
    Z = -Z;
    P = -1;
  } else {
    P = 1;
  }

  y = 1. / (1. + (Z * d));

  ERF = 1 - ((a * y) + (b * y * y) + (c * y * y * y)) * exp(-(Z * Z));

  if (P == -1)
    ERF = -ERF;

  return (ERF);
}

double ERFC(double A)
{
  double ERFC;
  ERFC = (1 - ERF(A));
  return (ERFC);
}


int ACHA_FI_P()
{
  float dFi, W, Z;

  Fi2 = 0.001;
  Fi3 = 0.001;
  dFi = 0.001;

  while (1) {
    FiP[0] = Fi2;
    FiP[1] = Fi3;
    W = (exp(-Fi2 * Fi2) / ERF(Fi2) -
         exp(-Fi2 * Fi2) / (ERF(Fi3) - ERF(Fi2)) - sqrt(3.14));
    Z = ((exp(-Fi3 * Fi3)) / (ERF(Fi3) - ERF(Fi2)) +
         (exp(-Fi3 * Fi3)) / (ERFC(Fi3)) - sqrt(3.14));

    if ((W == 0 || (W <= 0.01 && W >= -0.01))
        && ((Z == 0) || (Z <= 0.01 && Z >= -0.01))) {
      return (0);
    }

    Fi2 = Fi2 + dFi;

    if (Fi2 == 4) {
      Fi2 = 0.001;
      Fi3 = Fi3 + dFi;
    }
    if (Fi3 == 4) {
      printf("Nenhum valor encontrado.\n");
    }
  }
}

Second, decide whether you're programming in C [red]OR[/red] C++
Throwing together random bits of syntax which happen to compile isn't the way to go. You can delete these lines, and it would still compile as a proper C program.
[tt]#include <cstdlib>
#include <iostream>
using namespace std;
[/tt]

Third, you need to be more specific than "an error" or "it doesn't work".
For example
If it doesn't compile, then post your error messages.
If it doesn't run at all, post error messages
If it doesn't work for some specific test case, then you REALLY do need to state your test cases to give us any chance of replicating the issue you see.

What are we supposed to do - make wild guesses as to what to type in for the input(s) the program requests?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Alright, maybe I was not clear.
The following code compiles, no errors, it runs normaly until the ACHA_FI_P() function is called.
Then nothing happens, I can't know if the program is in an infinite loop because i couldnt debug it with Dev C++, or if it crashes, but the printf after the function isnt shown.
What I need now is a debugger to see what is happening in that function, any tips?


Code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

double ERF(double Z),ERFC(double A);
float A;
int ACHA_FI_P();
float Fi2,Fi3,FiP[2];


int main()
{ 
double B,C,D;
while(1)
{
printf("Entre com o valor de A:");

scanf("%f", &A);
printf("A= %f \n",A);
B=ERF(A);
C=ERFC(A);
printf("ERF(A)=%f\n", B);
printf("ERFC(A)=%f\n\n", C);
ACHA_FI_P();
printf("\nValores obtidos para Fi2 e Fi3 sao respectivamente %f e %f.",FiP[0],FiP[1]);
}

return(0);


}

double ERF(double Z)
{
    int P;
        double a = 0.348;
    double b = -0.096;
    double c = 0.748;
    double d = 0.471;
    double y, ERF;

    if (Z < 0) { Z = -Z; P = -1;}
    else { P = 1; }

    y = 1./(1. + (Z * d));

    ERF = 1 - ((a * y) + (b * y*y) + (c * y*y*y))*exp(-(Z*Z));

    if(P == -1) ERF = -ERF;

    return(ERF);
}

double ERFC(double A)
{
        double ERFC;
        ERFC=(1-ERF(A));
        return(ERFC);
}



int ACHA_FI_P ()
{
float dFi,W,Z;

Fi2=0.001;
Fi3=0.001;
dFi=0.001;

while (1)
{
FiP[0]=Fi2;
FiP[1]=Fi3;
W=(exp(-Fi2*Fi2)/ERF(Fi2)-exp(-Fi2*Fi2)/(ERF(Fi3)-ERF(Fi2))-sqrt(3.14));
Z=((exp(-Fi3*Fi3))/(ERF(Fi3)-ERF(Fi2))+(exp(-Fi3*Fi3))/(ERFC(Fi3))-sqrt(3.14));

if((W==0||(W<=0.01 && W>=-0.01))&&((Z==0)||(Z<=0.01 && Z>=-0.01))) {return(0);}

Fi2=Fi2+dFi;

if(Fi2==4){Fi2=0.001; Fi3=Fi3+dFi;}
if(Fi3==4){printf("Nenhum valor encontrado.\n");}
}
}
 
In the [tt]ACHA_FI_P()[/tt] function, the only way out of the loop is if this condition is being met...
Code:
if((W==0||(W<=0.01 && W>=-0.01))&&((Z==0)||(Z<=0.01 && Z>=-0.01))) {return(0);}
You might put a couple [tt]printf()[/tt] statements in the loop to see what values you are getting. You apparently are not ever going through the loop with "W" and "Z" both being zero, or within one one hundredth of zero.

[tt]printf()[/tt]'s are the cheapest debug.

 
Here is an excerpt from one tutorial on how to compare float numbers in C:

"(...) It is very usual for the C programming language beginners to compare a floating point number using the "==" operator. Floating point numbers must not be compared with the "==" operator.

That is mainly because when you compute a float number you will get a result like 1.543645274878272 and if you compare 1.543645274878272 with 1.5436, the result will always be false.
(...)"

Google it and you shall find the cause and the solution for your problem.

Best Regards,
P.

 
I think he's ok on that point. He's only using "==" to compare to zero. Otherwise he's using ">=" and "<=", which should be ok.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top