Ionut Hulub
Ionut Hulub

Reputation: 6326

C function returns wrong value

float a, b;
float sa() { return a;};
int main() {
  a = 10;
  b = sa();
  printf("%f", b);
  return 0;
}

This is a simplified version of my code. I believe the program should print 10 but it gives me really small numbers like -65550, not always the same but very alike.

I have used the debugger to check the value of variabe a right before it is returned and it is 10, so the function returns 10, but b is set to something like -65550. I don't understand why this happens.

I'd appreciate some intell.

Thanks in advance.

Here is the full code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

int dimensiuni, nrBitiSolutie, bitiPeDimensiune, gasitInbunatatire, nrRulari;
float limInf, limSup, precizie, valoareFunctie, minim, minimNou, T;
char solutie[100000];
float solutieReala[100];

void generateRandomSolution();
void bitesToFloat();
void rastrigin();
void rosenbrock();
float nextFirstFit();
float nextBestFit();


void main() {
    int k;
    T = 10;
    gasitInbunatatire = 1;
    srand ( time(NULL) );
    printf("Introduceti numarul de dimensiuni: ");
    scanf("%d", &dimensiuni);
    printf("Introduceti limita inferioara si cea superioara: ");
    scanf("%f%f", &limInf, &limSup);
    printf("Introduceti precizia: ");
    scanf("%f", &precizie);

    //calculam numarul de biti necesari ca sa reprezentam solutia
    nrBitiSolutie = dimensiuni * ceil(log(limSup-limInf * pow(10, precizie)))/log(2.0);
    bitiPeDimensiune = nrBitiSolutie/dimensiuni;

    //generam o solutie random
    generateRandomSolution();
    bitesToFloat();
    rastrigin();
    minim = valoareFunctie;

    printf("Pornim de la %f\n", minim);

    while( (nrRulari < 10000) && (T > 0.001)) {
        minimNou = sa(); //error occurs here. sa() returns about 200 but minimNou is set to -65550
        if (minimNou < minim) {
            printf("Minim nou: %f\n", minimNou);
            minim = minimNou;
            T *= 0.995;
        }
        nrRulari++;
    }
    printf("Minimul aproximat: %f\n", minim);
    system("pause");
}

void generateRandomSolution() {
    int l;
    for (l = 0; l < nrBitiSolutie; l++) solutie[l] = rand()%2;
}

void bitesToFloat() {
    int i, parcurse = 1, gasite = 0;
    int variabila = 0;
    float nr;
    for (i = 0; i < nrBitiSolutie; i++) {
        variabila = variabila<<1 | (int)solutie[i];
        if(parcurse == bitiPeDimensiune) {
            nr = (float)variabila / (float)pow(2, bitiPeDimensiune);
            nr *= limSup-limInf;
            nr += limInf;
            nr *= pow(10, precizie);
            nr = (int)nr;
            nr /= pow(10, precizie);
            parcurse = 0;
            solutieReala[gasite++] = nr;
            variabila = 0;
        }
        parcurse++;
    }
}

void rastrigin() {
    int i;
    valoareFunctie = 10 * dimensiuni;
    for (i = 0; i < dimensiuni; i++) {
        valoareFunctie += pow((float)solutieReala[i], 2) - 10 * (float)cos(2 * 3.14 * (float)solutieReala[i]);
    }
}

void rosenbrock() {
    int i;
    valoareFunctie = 0;
    for (i = 0; i < dimensiuni - 1; i++) {
        valoareFunctie += 100 * pow((solutieReala[i+1] - pow(solutieReala[i], 2)), 2) + pow((1-solutieReala[i]), 2);
    }
}

float sa() {
    int j;
    for (j = 0; j < nrBitiSolutie; j++) {
        solutie[j] = solutie[j] == 0 ? 1 : 0;
        bitesToFloat();
        rastrigin();
        if (valoareFunctie < minim) return valoareFunctie;
        else if ( (rand()/INT_MAX) < exp((minim - valoareFunctie)/T) )  
            return valoareFunctie;
        else solutie[j] = solutie[j] == 0 ? 1 : 0;
    }
    return minim;
}

I have marked where the error occurs with error occurs here comment

Upvotes: 1

Views: 4190

Answers (1)

Raymond Chen
Raymond Chen

Reputation: 45172

You simplified the code incorrectly. In your simplification, you defined sa() before calling it. But in your full program, you call sa() before defining it. In the absence of a declaration, functions are assumed to return int. Since your function actually returns a float, the result is undefined. (In this case, you will read a garbage value from the top of the floating point stack and then the floating point stack will underflow, and things go downhill from there.)

Upvotes: 5

Related Questions