-1

I have the following situation wherein I'm reading a file line-by-line where the first few lines (example) are as follows:

0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 20.00000000000000000, 0.18854095325733133
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 28.88888888888888900, 0.18861310382752311
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 37.77777777777777900, 0.18870756072971304
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 46.66666666666667100, 0.18876790723761561
0.00000000000000000, 0.00000000000000000, 0.00000000000000000, 55.55555555555555700, 0.18879081871602224

and the processing of the lines:

fp = fopen(fn, "r");

if (fp == NULL) {
    <...>
}

while ((read = getline(&line, &len, fp)) !=-1) {
    <...>
    char *pt;
    pt = strtok(line,",");
    while (pt != NULL) {
        //order: pressure, velocity, strain, temp
        pnn = atof(pt);             //pressure
        pt = strtok(NULL, ",");

        vrm = atof(pt);             //velocity
        pt = strtok(NULL, ",");

        epx = atof(pt);             //elastic strain
        pt = strtok(NULL, ",");

        temp = atof(pt);            //temperature
        pt = strtok(NULL, ",");

        //reference friction coefficient
        REAL ref_ffric = (REAL)atof(pt);
        ref_fric = &ref_ffric;
        pt = strtok(NULL, ",");

REAL is in this context defined as a float

However the ref_fric is never the correct amount, i.e the first line it should be 0.18854095325733133 but rather when I print it out it's 0.18854095041751862.

How do I solve this char* to float conversion precision problem?

Marvin Effing
  • 2,693
  • 3
  • 20
  • 35
  • 2
    What is `REAL`? Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also you may want to see [language agnostic - Is floating point math broken? - Stack Overflow](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – MikeCAT Oct 30 '19 at 14:38
  • 3
    The precision seems to be the one expected for a `float`. – AProgrammer Oct 30 '19 at 14:38
  • 1
    Can you define REAL as a double? Float in c has 7 digits of precision, so it isn't an error just the way float is implemented – Tyler Oct 30 '19 at 14:38
  • @Tyler I will try that – Marvin Effing Oct 30 '19 at 14:47
  • 1
    Double has 15 digits of precision so it still will be *slightly* off because your file has 17 digits of precision – Tyler Oct 30 '19 at 14:48

2 Answers2

2

Float isn't perfectly precise, and small fractional values might not come out the same way they were put in. Double is better, if you can use that. See this related question: See this related question: 'float' vs. 'double' precision

izzy
  • 769
  • 2
  • 12
  • 22
-1

Floating point numbers cannot accurately represent many decimal places, so if you want more precision you have to switch away from standard floating points to a library like GMP.

Aplet123
  • 33,825
  • 1
  • 29
  • 55