Conversion problem in ansi c

Possible duplicates:
Examples of floating point inaccuracies
Is JavaScript math broken?

I need to convert some data from a txt file to a double value, and I use this function to do this: atof. The problem is that the value to be converted is 5.550000, and the atof function returns 5.5499999999999998, and this is a problem because I have to calculate the GPA with this number, and the retry is inaccurate. This is a function that reads data from a txt file:

void readNext(FILE* file,Lab* lab)
{
    char line[100];
getline(file,line,100);
if (strcmp(line,"") == 0)
{
    lab->is_null = 1;
    return;
}
strcpy(lab->date,line);
getline(file,line,100);
lab->presence = atoi(line);
getline(file,line,100);
strcpy(lab->num_work,line);
getline(file,line,100);
lab->mark_work = atof(line);
getline(file,line,100);
lab->test_work = atof(line);
getline(file,line,100);
lab->current = atof(line);
getline(file,line,100);
lab->status_work = atoi(line);
getline(file,line,100);
}
+3
source share
3 answers

drhirsch - 5.55 ( , 1 ÷ 7 ).

, 5.55 , , . , - %.3g. GPA, - , .

+2

. this StackOverflow.

, , , - . ( ) - .

+3

Short answer: it atof("5.55")will never return the exact (exact) representation of this decimal fraction, because there is no exact binary representation of the floating point of this number, it is an infinite binary fraction.

For a long answer, see http://www.math.umd.edu/~jkolesar/mait613/floating_point_math.pdf .

+2
source

All Articles