tags:

views:

42

answers:

2
void main()
{
    char buffer[40];
    float x=1.2f,y=.2f;
    printf("%f",(x/y));
    printf( "\n%s\n", gcvt( x/y, 30, buffer ) );        
}

The second printf gives result as 6.0000001490116102.I expected the answer to be 6.0000...full zeros.

+1  A: 

As with all floating point questions, some numbers can not be represented exactly. Get used to that, it's a fact of life :-)

With a 32-bit floating point value (for example), there are about four billion numbers that can be represented exactly. Unfortunately, in mathematics, even between 0 and 0.00000000000000000000001, there are, hmm..., let me think, an infinite number of numbers.

One thing you may find helpful is to use doubles instead of floats - they have more bits in them so can represent more exact numbers. Beyond that, there are arbitrary precision libraries you can use if you want even more precision.

If you change your code to (including a decent main):

int main (void) {
    char buffer[40];
    float x=1.2f,y=.2f;
    double xd=1.2,yd=.2;

    printf("%f\n",(x/y));
    printf("%.30f\n",(x/y));
    printf( "%s\n\n", gcvt( x/y, 30, buffer ) );

    printf("%f\n",(xd/yd));
    printf("%.30f\n",(xd/yd));
    printf( "%s\n", gcvt( xd/yd, 30, buffer ) );
}

you'll see this in action:

6.000000
6.000000149011610162119723099750
6.00000014901161016211972309975

6.000000
5.999999999999999111821580299875
5.99999999999999911182158029987

You can see that the doubles are much closer to your desired value, but they're still not exact. IEEE754 doubles have about 16 digits of precision (single precision floats have about 7).

paxdiablo
A: 

As paxdiabolo suggest, have a look into floating point computation to learn about what is going on. But also, my man page on that has an important hint:

This function is marked as LEGACY in POSIX.1-2001. POSIX.1-2008 removes the specification of gcvt(), recommending the use of sprintf(3) instead (though snprintf(3) may be preferable).

So just don't use it.

Jens Gustedt