Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a float variable loses its precision in C

Hi yes I am having some trouble with the following code. The function above executes and outputs 132.87 to the float variable but when I return this back to the main program the output is then shortened to 132.00

This is obviously something simple I am missing can anyone help me with this one please? Much appreciated.

calcSigmaXY(max) {
 int count= 0;
 float runTotal = 0, product[max];
 for (count = 1; count <= max; count++) {
     product[count] = pointData[count][1] * pointData[count][2];
     runTotal = runTotal + product[count];
 }
 printf("\nruntotal is %4.2f",runTotal); // outputs 132.87
 return(runTotal);
} 

int maxPoints = 6;
float sigmaXY = 0,
sigmaXY = calcSigmaXY(maxPoints);
printf("\nsigmaxy set to : %4.2f\n", sigmaXY); // outputs 132.00
like image 876
Matthew Walton Avatar asked Dec 16 '22 17:12

Matthew Walton


2 Answers

In some C versions, return values & types default to int, so by declaring your function like

calcSigmaXY(max) 

you're basically saying

int calcSigmaXY(max) 

ergo the loss of precision - the float you return is converted to an int. Declare your function as

float calcSigmaXY(max) {
      //...
}
like image 108
Luchian Grigore Avatar answered Jan 09 '23 14:01

Luchian Grigore


You have to declare the return value of calcSigmaXY, otherwise the return value is int implicitly.

If you declare your function as float calcSigmaXY it should work as intended.

Also consider enabling and reading warnings in your compiler/IDE.

like image 31
Otto Allmendinger Avatar answered Jan 09 '23 14:01

Otto Allmendinger