Search code examples
cdecimalexponential

C double to Exponential round error


I am trying to convert a double value in to an exponential but I get a rounding error.

int main (unsigned int argc, char **argv)
{
    float a=293.17;
    float b=293.10;
    double ULfreq = 2089.555000;
    double upfreq = 0.0;
    long int t = 0;
    long int u = 0;

    upfreq = ULfreq * 1000000.0;

    printf(" %f, upfreq:%22.16E\n", upfreq, upfreq);

    return 0;
}

When I run this code on a 32-bit computer I get the following result:

2089555000.000000, upfreq:2.0895549999999998E+09

Running it on a 64-bit one gives the correct answer.

Is it possible to make this conversion work on a 32-bit server and how?


Solution

  • It's not rounding error, but representation feature of floating points. Most short fractions such as 1.1 are infinite series in base 2 (as 1/3 = 0.3333... in base 10). Thus one has to clip it somewhere. When converted back to decimal, 2.08955499999 is what you get.

    In the first case you ask printf function to round it to the default precision of %f, which is 6 decimals after the decimal separator. In the second case you are asking 22 digits of precision and get that...