Problem with ascii to floating point functions using gcc

Found this issue while playing around with inline assembly code of floating point to string encoding and decoding functions on Linux. If you are using gcc (haven’t checked any other C compiler yet) and try to convert c-strings to floating points you might get some weired results in some cases. Here is trimmed down version of my code:

#include <stdio.h>
int main () {
char *pch;
double d1 = atof( “00.000001” );
double d2 = strtod( “00.000001”, pch = NULL );
printf ( ” d1 : %f \n d2 : %f \n”, d1, d2 );
}

Output:
d1 : -1598689907.000000
d2 : -1598689907.000000

If you turn on warnings (-Wall) you might get something like:

warning: implicit declaration of function ‘atof’

which gives clear hint about the problem. The problem is related to the return type of these conversion functions. C compiler doesn’t know the return type of these symbols unless we include stdlib.h. So it assumes the default return type which is integer and then convert that integer to double later on so loses actual floating point value.

Advertisements

One Response to “Problem with ascii to floating point functions using gcc”

  1. bet365 Says:

    how are you I was luck to look for your website in baidu
    your topic is marvelous
    I learn much in your blog really thank your very much
    btw the theme of you site is really terrific
    where can find it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: