Finding how a function works..

This is a discussion on Finding how a function works.. within the C Programming forums, part of the General Programming Boards category; Say I want to look into the details of how a function works because I have been trying to come ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    49

    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?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Google "atof source code" is one way: https://www.google.com/search?q=atof+source+code.

    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. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    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
    Last edited by sdbuilt; 09-28-2012 at 12:48 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In (almost no) testing, this handles the digits in the string, on the left side of the decimal point, of your float.

    Use at your own risk!

    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. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,693
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by sdbuilt View Post
    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. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    49
    perfecto.. I got it now thanks christop. Don't know why I put j = 1 in the loop itself. I appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 03-02-2008, 12:27 PM
  2. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  3. Function works one time, and not another...
    By Captain Penguin in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2002, 10:14 PM
  4. My own line function, ALMOST works
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2002, 04:50 PM
  5. Can someone explain how this function works?
    By Drew in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2001, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21