Thread: checking for floating point number

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    checking for floating point number

    hello guys,
    i have a very quick question, which might be very silly, well, i was wondering if there any funtion to find out wheather a eneterd value is a float, int or anything like that. i thought of writting a simple programe to check the value which basically check for an '.' char in a string and at the same time i am not checkking for char 'e' which sometimes used to denote floating num. does any one know any inbuilt function which actully does that.

    the code which i wrote
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        char num[20];
        int i;
        printf("Enter a floating point number\n?");
        fgets(num,sizeof(num),stdin);
        for(i=0;num[i]!='\0';i++)
        {
           if(num[i]=='.' && (num[i+1]-'0') >=0)
           {
           printf("%lf",atof(num));       
           getchar();
           return 0;
           }
        }
        printf("Its not a floating point number");
        getchar();
        return 0;
    }
    /*my output
    Enter a floating point number
    ?12.125
    12.125000
    
    Enter a floating point number
    ?1254
    Its not a floating point number
    */
    thax very much

    ssharish2005

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You could use sscanf on it, reading it into a float or a double, and check that it's scanned the whole string by using the %n specifier to write the offset to another variable. If it has only received part of the string, you'll know it's not a valid floating point number.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ssharish2005
    does any one know any inbuilt function which actully does that.
    Or like the strtol version here, but with strtod.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    This is not exactly a textbook approach but here's my idea:
    Take the entered floating point number and save it as an integer. This will drop off the data behind the '.' You can then change the integer back to a float and the float won't have the data behind the '.' You can then check to see if the two floating point number are the same. If they are then the floating point didn't have anything behind the '.' to begin with. This should be alittle easier than what you made because you won't have to convert the string to a float. Here's the code:

    Code:
    float OriginalFloat, FloatWithNoDecimals.
    int TempInt;
    
    TempInt = OriginalFloat;
    FloatWithNoDecimals = TempInt;
    
    if(OriginalFoat == FloatWithNoDecimals)
    {
        //T he float didn't have any decimals to begin with
    }
    else
    {
        // The float did have decimals
    }
    There is could be a more effective solution, but I thought it was pretty creative. Hope that helps. You could make this into a function and put it into a math library. If you really get into programming, you will probably have a library with some common functions. I have 3 libraries. Good Luck!
    Last edited by Brad0407; 10-18-2005 at 07:49 PM.
    Don't quote me on that... ...seriously

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, it seem bit interesting to have different kinds of solution from u people, thax very much for that. well, i worte a programe on what Dave_Sinkula and Brad0407 recommanded me. the goood thing i seem to give me the solution which i expected. but i am not able to get the CWR logic. can any one tell me more about that logic

    thax very much
    ssharish2005

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.daniweb.com/code/snippet358.html

    [edit]But the %n part would be to check whether all of the text entered was part of a valid floating point number. Like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int mygetd(double *result)
    {
       int n;
       char buff [ 32 ];
       if( fgets(buff, sizeof buff, stdin) != NULL)
       {
          char *newline = strchr(buff, '\n');
          if (newline != NULL)
          {
             *newline = '\0';
          }
       }
       return sscanf(buff, "%lf%n", result, &n) == 1 & (int)strlen(buff) == n;
    }
    
    int main(void)
    {
       double value;
       do
       {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
       printf("value = %g\n", value);
       return 0;
    }
    
    /* my output
    Enter a floating-point number: one
    Enter a floating-point number:
    Enter a floating-point number: f12.3
    Enter a floating-point number: -45.67
    value = -45.67
    
    Enter a floating-point number: -12.3f
    Enter a floating-point number:    125 help
    Enter a floating-point number: 1.2.3
    Enter a floating-point number: 1.23
    value = 1.23
    */
    Last edited by Dave_Sinkula; 10-18-2005 at 08:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    If you read in a float with scanf, it will read as many characters as it can convert into a float. So for instance, if it tried to read in:
    1.234abc
    it would read in 1.234, get to 'a', see that that's not a valid part of a float, and stop. So the float you would get back would be 1.234. It will stop at any invalid character, so that includes a second decimal point, whitespace, any alpha characters except 'e' in the correct place and any non-alphanumeric characters except a single '.'.

    %n tells you how many characters have been read in. So if you know that your string is 10 characters long, and you used scanf to try to read it in as a float, but using %n you found out that it only read in 8 characters, you would know the last two characters were not part of a valid float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Header files and classes
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2008, 09:02 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM