2016-05-15 5 views
0

einigeIst dieser Test tautologisch?

double x; 

was ist der Unterschied zwischen

if((x>0 || (x<=0))) 

und if(true) gegeben? Gibt es einen Unterschied? Ich denke, sie sind gleichwertig, aber es scheint nicht so zu sein. Jeder weiß warum?

Ok, ich bin neu bei cpp und es tut mir leid für diese Frage. Aber ich muss wirklich dieses Problem lösen double sigmaX = 0.1; DoppelsigmaY = 0,1;

SimPFilter::SimPFilter() { 
    totalParticles = 100; 
    nFilters = 2; 
    gsl_rng_env_setup(); 
    rng = gsl_rng_alloc(gsl_rng_mt19937); 
    gsl_rng_set(rng, time(NULL)); 
} 

SimPFilter::~SimPFilter() { 

} 

void SimPFilter::initParticles(double initX, gsl_rng* rng){ 
    for (int i=0; i < totalParticles; i++) { 
     particles[i].xp = particles[i].x = 0.5 + gsl_ran_gaussian(rng, sigmaX*sigmaX); 
     if(i > totalParticles/2) 
      particles[i].xp = particles[i].x = -0.5 + gsl_ran_gaussian(rng, sigmaX*sigmaX); 
     particles[i].wNormalized = 1/(totalParticles/nFilters); 
     int j = (int)i/(totalParticles/nFilters); 
     particles[i].id = j; 
     filters[j].nParticles = (totalParticles/nFilters); 
     particles[i].w = particles[i].wp = particles[i].wNormalized = 1/totalParticles; 

    } 
    for(int i =0; i<nFilters; i++){ 
     filters[i].weight = filters[i].weightPrev = 1.0/nFilters; 

    } 
} 

void SimPFilter::transition(gsl_rng* rng){ 
    for (int i=0; i < totalParticles; i++) { 
     double temp = particles[i].x; 
     particles[i].xp = temp; 
     particles[i].x += gsl_ran_gaussian(rng, sigmaX*sigmaX); //the centre of each region  
    } 
} 

double SimPFilter::measure_prob(particle *p, double obser_y, gsl_rng* rng){ 
    double x = p->x; 
    //cout << "x value is " << x << endl; 
    double prob = gsl_ran_gaussian_pdf (obser_y - (x*x), sigmaY); 
    //cout << "probability " << prob << endl; 
    std::cout << std::boolalpha; 
    //cout << (x>0 || (x<=0)) << endl; 
    if(true){ 
     //cout << "probability2 " << prob << endl; 
     return prob; 
    } 
    return prob; 
} 

void SimPFilter::updateWeight(double obser_y, gsl_rng* rng){ 
    for (int i=0; i < totalParticles; i++) { 
     double temp = particles[i].w; 
     particles[i].wp = particles[i].w; 
     particles[i].w = measure_prob(&particles[i], obser_y, rng); 
    } 

    //normalized particle weight 
    double eachFilterSum[nFilters]; 
    for(int i=0; i < totalParticles; i++){ 
     int id = particles[i].id; 
     eachFilterSum[id] += particles[i].w; 
    } 

    for(int i=0; i < totalParticles; i++){ 
     particles[i].wNormalized = particles[i].w/eachFilterSum[particles[i].id]; 
    } 

    //update each filter's weight 
    double eachFilterW[nFilters]; 
    for(int i=0; i< totalParticles; i++){ 
     //cout << "prticle i's weight1 "<<particles[i].w << endl; 
     int id = particles[i].id; 
     eachFilterW[id] += particles[i].wNormalized; 
    } 
    double sigmaweight =0; // simga pi n t-1 * wnt 
    for(int j=0; j<nFilters; j++){ 
     sigmaweight += filters[j].weightPrev * eachFilterW[j]; 
    } 
    for(int j=0; j<nFilters; j++){ 
     double temp = filters[j].weight; 
     filters[j].weight = filters[j].weightPrev * eachFilterW[j]/sigmaweight; 
     filters[j].weightPrev = temp; 
    } 
} 

void SimPFilter::resample(gsl_rng* rng){ 
    particle * newParticles; 
    newParticles = (particle*) malloc(totalParticles * sizeof(particle)); 
    int index =(int)gsl_rng_uniform (rng)* totalParticles; 
    double beta = 0.0; 
    double maxWeight =0; 
    for (int i=0; i < totalParticles; i++) { 
     maxWeight = max(maxWeight, particles[i].wNormalized* filters[particles[i].id].weight); 
    } 
    for(int j=0; j < totalParticles; j++){ 
     beta += (rng, 0, 2*maxWeight); 
     while(beta > particles[index].wNormalized * (filters[particles[index].id].weight)){ 
      beta -= particles[index].wNormalized * (filters[particles[index].id].weight); 
      index = (index+1) % totalParticles; 
     } 
     newParticles[j] = particles[index]; 
    } 
    for(int i=0; i<totalParticles; i++){ 
     particles[i] = newParticles[i]; 
     particles[i].w = particles[i].wNormalized = particles[i].wp =1/filters[particles[i].id].nParticles; 
    } 
    //update each filter particles number 
    vector<int> countP; 
    for(int i=0; i<nFilters; i++){ 
     countP.push_back(0); 
    } 
    for(int i=0; i< totalParticles; i++){ 
     countP.at(particles[i].id)++; 
    } 
    for(int i=0; i< nFilters; i++){ 
     filters[i].nParticles = countP.at(i); 
    } 
    for(int i=0; i< nFilters;i++) 
     cout << "n particle value is " << filters[i].nParticles << endl; 
    free(newParticles); 
} 

` in der measure_prob Funktion, finde ich heraus, ob (true) oder if (x> = 0 || x < 0) gibt anderes Ergebnis. Wenn if (true) zwei Objekte verfolgen kann, aber wenn ich if (x> = 0 || x < 0) verwende, konvergiert es schnell zu einem Objekt. Ich bin wirklich verwirrt ...

+1

Und was ist 'x'? –

+0

Es kommt sehr darauf an, was 'x' ist. Ist es ein Ausdruck mit Nebenwirkungen? Ist es von einem Typ, der die '>' oder '<=' Operatoren überladen hat? Übergibt ein potentieller x <0'-Operator eine Überladung, die den logischen oder '||' Operator überladen hat? –

+4

Wenn x ein 'float' oder' double' ist und 'NaN' (Not a Number) ist, sind beide Bedingungen falsch. In diesem Fall sind sie nicht ganz gleichwertig. – SirGuy

Antwort

4

Beispiel:

double x = log2(-1); // Evaluates to NAN 

if (x>0 || (x<=0)) { 
    printf("True\n"); 
} 
else { 
    printf("False\n"); 
} 

OUTPUT

False 

Daher sie sind nicht gleichwertig. NAN steht für Not A Number also ist es weder größer als 0, noch ist es gleich oder kleiner als null, weil es keine Zahl ist.