Test if number is integer or float?

R

Rui Maciel

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
 
R

Rune Allnor

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
 
S

Stefan Ram

Rune Allnor said:
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'; }
 
R

Rui Maciel

Pete said:
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
 
R

Rui Maciel

Rune said:
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
 
M

Mohammad Nabil Al-Aggan

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
 
A

AnonMail2005

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
 
J

James Kanze

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.)
 
J

Jorgen Grahn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top