Test if number is integer or float?

Discussion in 'C++' started by Rui Maciel, Mar 7, 2010.

  1. Rui Maciel

    Rui Maciel Guest

    Is it possible to test a given string using standard C++ routines to check if it describes an
    integer or a floating point number?


    Thanks in advance,
    Rui Maciel
     
    Rui Maciel, Mar 7, 2010
    #1
    1. Advertising

  2. Rui Maciel

    Rune Allnor Guest

    On 7 Mar, 01:31, Rui Maciel <> wrote:
    > Is it possible to test a given string using standard C++ routines to check if it describes an
    > integer or a floating point number?  


    No. The string literal '1' might refer to the integer 1
    or the floating point number 1.00000 but to only one
    significant digit. There is no way one (!) can tell.

    What you can do, is to use regular expressions to test if
    the number follows one of the floating point patterns.

    Rune
     
    Rune Allnor, Mar 7, 2010
    #2
    1. Advertising

  3. Rui Maciel

    Stefan Ram Guest

    Rune Allnor <> writes:
    >>Is it possible to test a given string using standard C++ routines to check if it describes an
    >>integer or a floating point number?  

    >No. The string literal '1' might refer to the integer 1
    >or the floating point number 1.00000 but to only one
    >significant digit. There is no way one (!) can tell.


    Reading the »or« as non-exclusive:

    #include <iostream> /* ::std::cout */
    #include <ostream> /* << */
    #include <sstream> /* ::std::stringstream */

    int main()
    { ::std::string s( "1" );
    ::std::stringstream i( s ); int i0; i >> i0;
    ::std::cout <<( i.eof() && !i.fail() )<< '\n';
    ::std::stringstream d( s ); double d0; d >> d0;
    ::std::cout <<( i.eof() && !i.fail() )<< '\n'; }
     
    Stefan Ram, Mar 7, 2010
    #3
  4. Rui Maciel

    Rui Maciel Guest

    Pete Becker wrote:

    > Rui Maciel wrote:
    >> Is it possible to test a given string using standard C++ routines to
    >> check if it describes an integer or a floating point number?
    >>

    >
    > Only if you say what you mean by "describes an integer or a floating
    > point number". A string means whatever you interpret it to mean. "1.1"
    > can describe the integer 1, or it can describe the floating point value
    > 1.1, or it can describe the floating point value 1.0, or a host of other
    > things.


    As we are talking about a syntax used to represent numbers, I don't believe that there could
    possibly be any ambiguity in this subject. It is pretty clear that, in this context, the
    string "123.0" represents a floating point number while the string "123" represents an
    integer.


    Rui Maciel
     
    Rui Maciel, Mar 14, 2010
    #4
  5. Rui Maciel

    Rui Maciel Guest

    Rune Allnor wrote:

    > No. The string literal '1' might refer to the integer 1
    > or the floating point number 1.00000 but to only one
    > significant digit. There is no way one (!) can tell.
    >
    > What you can do, is to use regular expressions to test if
    > the number follows one of the floating point patterns.


    I was trying to avoid that in hopes that somewhere some standard routine or even library was
    able to do that job. Oh well, a parser it is then.


    Thanks for the help,
    Rui Maciel
     
    Rui Maciel, Mar 14, 2010
    #5
  6. On Mar 14, 3:10 pm, Rui Maciel <> wrote:
    /snip/
    > As we are talking about a syntax used to represent numbers, I don't believe that there could
    > possibly be any ambiguity in this subject.  It is pretty clear that, in this context, the
    > string "123.0"  represents a floating point number while the string "123" represents an
    > integer.
    >
    > Rui Maciel


    In fact, 123.0 is double, and 123.0f is float
     
    Mohammad Nabil Al-Aggan, Mar 14, 2010
    #6
  7. Rui Maciel

    Guest

    On Mar 6, 8:31 pm, Rui Maciel <> wrote:
    > Is it possible to test a given string using standard C++ routines to check if it describes an
    > integer or a floating point number?  
    >
    > Thanks in advance,
    > Rui Maciel


    Read the string into a double (or float). Then take the integer part
    using standard <cmath> routines and put that into another double (or
    float). Then compare the two doubles (or floats) for equality within
    a tolerance. If they're equal it's an integer (or a floating point
    number that's equal to an integer) otherwise it's a floating point
    number.

    This method relies on standard library math routines and standard
    library parsing routines. No need to write your own string parser.

    HTH
     
    , Mar 14, 2010
    #7
  8. Rui Maciel

    James Kanze Guest

    On Mar 14, 2:10 pm, Rui Maciel <> wrote:
    > Pete Becker wrote:
    > > Rui Maciel wrote:
    > >> Is it possible to test a given string using standard C++
    > >> routines to check if it describes an integer or a floating
    > >> point number?


    > > Only if you say what you mean by "describes an integer or a
    > > floating point number". A string means whatever you
    > > interpret it to mean. "1.1" can describe the integer 1, or
    > > it can describe the floating point value 1.1, or it can
    > > describe the floating point value 1.0, or a host of other
    > > things.


    > As we are talking about a syntax used to represent numbers, I
    > don't believe that there could possibly be any ambiguity in
    > this subject. It is pretty clear that, in this context, the
    > string "123.0" represents a floating point number while the
    > string "123" represents an integer.


    Why? (In most of the work I'm doing at present, "123"
    represents a floating point value.)

    --
    James Kanze
     
    James Kanze, Mar 15, 2010
    #8
  9. Rui Maciel

    Jorgen Grahn Guest

    On Sun, 2010-03-07, Paavo Helde wrote:
    > Rui Maciel <> wrote in
    > news:4b92f3f3$0$6782$:
    >
    >> Is it possible to test a given string using standard C++ routines to
    >> check if it describes an integer or a floating point number?

    >
    > The decimal point in the string representation of a floating-point number
    > depends on the locale, so you first have to specify which locale you have
    > to use. Next, what you mean by an integer? A number like
    > 12345678901234567890 is an integer, but probably cannot be assigned to an
    > int. It all depends on what exactly one wants to achive.
    >
    > One solution to this underspecified problem would be to use strtol(), and
    > if this is able to consume all of the string, consider the result as a
    > (long) integer, otherwise the string is assumed to represent a floating-
    > point number.


    You don't have to assume; you can then try strtod() in the same way.

    Note that naive regexp solutions probably will give false negatives
    on a lot of rarely-used formats, such as hexadecimal floats and whatnot.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 21, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,291
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    651
  3. Derek Basch

    Remove integer from float number

    Derek Basch, Mar 23, 2006, in forum: Python
    Replies:
    7
    Views:
    358
    Grant Edwards
    Mar 23, 2006
  4. Michael Yanowitz

    RE: Remove integer from float number

    Michael Yanowitz, Mar 23, 2006, in forum: Python
    Replies:
    5
    Views:
    557
    Michael Tobis
    Mar 25, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,606
    James Kanze
    Oct 8, 2009
Loading...

Share This Page