testing if a number is a float

This is a discussion on testing if a number is a float within the C Programming forums, part of the General Programming Boards category; I'm writing a program and suppose to have a int as in input, but if someone enters a float, I ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    testing if a number is a float

    I'm writing a program and suppose to have a int as in input, but if someone enters a float, I want it to test if it is a float and then tell the user to re-enter the value, how do I do that?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: testing if a number is a float

    Originally posted by UnknownImage
    I'm writing a program and suppose to have a int as in input, but if someone enters a float, I want it to test if it is a float and then tell the user to re-enter the value, how do I do that?
    Read it as a string. Use strchr to see if it has a decimal point.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    other ways to test if it is an num is a floating point

    Is there any other ways to test if a number is a floating point besides reading it into a string?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703

    Re: other ways to test if it is an num is a floating point

    Originally posted by UnknownImage
    Is there any other ways to test if a number is a floating point besides reading it into a string?
    Hmmm, well let's see, you can use scanf() and check the return value. Also, atof(fgets()) is effective, but won't tell you if alpha chars screwed up the input, in which case you just get 0.0. You can also read it into a buffer one by one, just try out different things...
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On a side note, we generally like to keep the same exact topic in the same thread. You'll want to keep that in mind for future reference.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    :-)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    int is_float(char *src, size_t len)
    {
      double s_val;
      char *end;
    
      s_val = strtod(src, &end);
    
      if (s_val == 0 || s_val == HUGE_VAL)
      {
        return 0;
      }
      if (end < src + len)
      {
        return 0;
      }
    
      return 1;
    }
    
    int main(void)
    {
      char *p;
      char string[20];
    
      printf("Enter a number: ");
      if (fgets(string, sizeof(string), stdin) == 0)
      {
        return 1;
      }
    
      if ((p = strrchr(string, '\n')) != 0)
      {
        *p = '\0';
      }
    
      if (is_float(string, strlen(string)))
      {
        printf("It is floating point\n");
      }
    
      return 0;
    }
    *Cela*

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Dude, you're not forgetting string is a reserved name, are you?! Please don't use it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Dude, you're not forgetting string is a reserved name, are you?!
    Since when? :-) The ANSI standard says only that function names beginning with str and a lower case letter are reserved, it also stands to reason that other global names would be too, but local variables are out of the implementation's juristiction :-) Unless we're looking at different parts of the standard, I'm looking at section 7.26.11 of ISO/IEC 9899:1999, so you don't have to look, it says this :-)
    7.26.11 String handling <string.h>
    1 Function names that begin with str, mem, or wcs and a lowercase letter may be added
    to the declarations in the <string.h> header.
    I'm probably missing another section and you're right, if you could point it out to me so I don't make the same mistake again that'd be great :-)
    *Cela*

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Strictly speaking, I believe you are correct...... but, to help clarify: from here:
    ext: always reserved for use with external linkage (7.1.3/4.1.2.1). This means that, even if you don't include the indicated header file, you still must never create global variables or non-static functions with these names. Strictly speaking, you can create local variables, static functions, typedefs, or macros with those names (depending on which headers you include, and other circumstances, you may need to #undef them first), but it's a bad idea because even if the compiler gets it right you'll confuse any humans who look at your program.
    Personally, I agree with the last sentence of the quote, it's a bad idea to use a name that is sooooo close to being reserved On a board like this, it's too easy for a newbie to take example code and mangle it into something that appears to work, but quickly breaks the rules. For example, they take your well written example code, and try to expand it, and in the process they make the string variable global. oops!

    >>I'm probably missing another section and you're right
    No more sections, and lets not debate too long eh!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    That's a good point, I'll remember it :-) But first, one nitpick about that quote

    >>This means that, even if you don't include the indicated header file
    The ANSI standard says that they're reserved no matter what headers are included
    All external names described below are reserved no matter what headers are included by the program.
    >>and lets not debate too long eh!
    Agreed :-)
    *Cela*

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    [nitpicks]
    If you're going to check for HUGE_VAL, you might as well check for -HUGE_VAL too.
    Providing a value of "0.0" to is_float will return 0.
    [/nitpicks]

  12. #12
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>If you're going to check for HUGE_VAL, you might as well check for -HUGE_VAL too.
    Good point, but now that I think about it there's no reason to check for HUGE_VAL at all, since errno is set for both underflow and overflow I can just check that for ERANGE and then I don't have to worry about unsafe floating point comparisons. Thanks for catching that :-)

    I also realized that I don't even have to test, or even have the s_val variable since all of my testing can be done more safely and thoroughly with just src, len, end, and errno :-)
    Code:
    #include <stdlib.h>
    #include <errno.h>
    
    int is_float(char *src, size_t len)
    {
      char *end;
    
      errno = 0;
      strtod(src, &end);
    
      if (errno == ERANGE)
        /* Under/Overflow */
      {
        return 0;
      }
      if (end == src || end < src + len)
        /* No conversion/left over string */
      {
        return 0;
      }
    
      return 1;
    }
    *Cela*

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. rounding numbers
    By maple23 in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 12:33 AM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 09:03 PM

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