Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Test if number is integer or float?

Reply
Thread Tools

Test if number is integer or float?

 
 
Rui Maciel
Guest
Posts: n/a
 
      03-07-2010
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
 
Reply With Quote
 
 
 
 
Rune Allnor
Guest
Posts: n/a
 
      03-07-2010
On 7 Mar, 01:31, Rui Maciel <rui.mac...@gmail.com> 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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-07-2010
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'; }

 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-14-2010
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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-14-2010
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
 
Reply With Quote
 
Mohammad Nabil Al-Aggan
Guest
Posts: n/a
 
      03-14-2010
On Mar 14, 3:10*pm, Rui Maciel <rui.mac...@gmail.com> 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
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      03-14-2010
On Mar 6, 8:31*pm, Rui Maciel <rui.mac...@gmail.com> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-15-2010
On Mar 14, 2:10 pm, Rui Maciel <rui.mac...@gmail.com> 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
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-21-2010
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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments