# Thread: Finding how a function works..

1. ## Finding how a function works..

Say I want to look into the details of how a function works because I have been trying to come up with my own version of it and it is falling short.. how would I do that?

For example the atof() function. I have been trying to write my own function to convert a string into a floating point value and am running into difficulties. I know the atof() function can do it for me, but I can't find anywhere the details of how that function actually works. Any sites you can direct me to?

Also, you could find the code for a free implementation of the standard C library, such as GNU's glibc: The GNU C Library.

Note, those versions may be a bit more complex than you want because they cover a lot more cases than the basic "+/-123.456" format. They also cover scientific notation, hexadecimal floats, and special strings like "INF", "INFINITY" and "NAN" (not a number).

3. Do you want to use another built in way to do it, or do you want to do it all yourself, at a low level?

sscanf(nameOfString, "%f", &aFloatingptNumber)

would be the "other" way of doing it, using a built in C function.

For a low level way to do it, think of digit by digit, and powers of 10 for each digit. Same way a number is made up, in base 10. I'm never looked for something to describe it, however. Just visualize it, and start out.

4. Considering I am not even at pointers yet, I want to do it myself at as low level as possible. One of the problems I am running into is that when I google atof I am not quite sure what I am looking at! Lol.. I can show you how I am trying it so far..

Code:
```#include <stdio.h>
#include <stdbool.h>
#include <math.h>

float  strToFloat (const char  string[])
{
int  i = 0;
int j;
float floatValue;
float result = 0.0;
float result2 = 0.0;
int negative = false;

// test for leading minus sign

if ( string[0] == '-') {
negative = true;
i = 1;
}

// formula that works for string to int function and that I use for before the decimal point
while ( string[i] >= '0' && string[i] <= '9')
{
floatValue = string[i] - '0';
result = result * 10.0 + floatValue;
++i;
}

// for converting after the decimal point
if (string[i] == '.')
while  ((string[i] >= '0' && string[i] <= '9') || string[i] == '.')
{
if (string[i] == '.')
i++;
else
j = 1;
floatValue = string[i] - '0';
result2 +=  (floatValue / pow(10, j));
i++;
j++;
}

result = result + result2;

if ( negative )
result  = -result;

return result;
}

int main (void)
{
float strToFloat (const char string[]);

printf ("%f\n", strToFloat("-867.6921"));

return 0;
}```
I get an output of -868.200012

5. In (almost no) testing, this handles the digits in the string, on the left side of the decimal point, of your float.

Code:
```#include <stdio.h>
#include <stdbool.h>
#include <math.h>

float  strToFloat (const char  string[])
{
int i,j,lenLeft;
long int n,n2,n3;
float floatValue;
float result = 0.0;
float result2 = 0.0;
int negative = false;
char left[20];
// test for leading minus sign

printf("\nstring: %s\n",string);
if ( string[0] == '-') {
negative = true;
i = 1;
}
else
i=0;
j=0;
while(string[i] != '.') {
if(string[i] != '-')   {
left[j++]=string[i];
}
++i;
}
left[j]='\0';
lenLeft=--j;
printf("left: %s     lenLeft: %d\n",left,lenLeft);

for(i=0,n=0;i<=lenLeft;i++) {
n2=0;
n3=pow(10,lenLeft-i);

n2+=((left[i]-'0') *n3);
n+=n2;
}
if(negative)
n*=-1;
printf("**n: %ld\n",n);

return 0;
/*
// for converting after the decimal point
if (string[i] == '.')
while  ((string[i] >= '0' && string[i] <= '9') || string[i] == '.')
{
if (string[i] == '.')
i++;
else
j = 1;
floatValue = string[i] - '0';
result2 +=  (floatValue / pow(10, j));
i++;
j++;
}

result = result + result2;

if ( negative )
result  = -result;
*/
return result;
}

int main (void)
{
float strToFloat (const char string[]);

strToFloat("-987654321.6921");
//printf ("%f\n", strToFloat("-867.6921"));

return 0;
}```

6. From IEEE 754-1985 - Wikipedia, the free encyclopedia

The leading 1 bit is omitted since all numbers except zero start with a leading 1; the leading 1 is implicit and doesn't actually need to be stored which gives an extra bit of precision for "free."
I am guessing the above is why you are .5 off.

Tim S.

7. Originally Posted by sdbuilt
Code:
```            if (string[i] == '.')
i++;
else
j = 1;
floatValue = string[i] - '0';
result2 +=  (floatValue / pow(10, j));
i++;
j++;```
You need braces around the statements following the "else". Otherwise it treats the period as a number.
You also need to not reset j to 1 with every digit. Set it to 1 before the loop starts.

8. perfecto.. I got it now thanks christop. Don't know why I put j = 1 in the loop itself. I appreciate your help.