Determine if a token could represent a floating point number (double). To qualify, the token must be either:- a) A series of one or more digits. b) (a) followed by a single period followed directly by another series of digits. c) (b) followed by a '*10^' followed by another series of digits (standard form number). All of the above must be followed by a 'valid terminator sequence'. This could be a null terminator but may be a punctuating char as long as that we not followed by a number. Errors: Sets an E_ARGUMENT error if either first two arguments are invalid.
| Return Type | Function name | Arguments |
|---|---|---|
| bool | IsDouble | (double&,const char*,) |
Declared in file: hzTextproc.h
Defined in file : hzTypes.cpp
Function Logic:
Function body:
bool IsDouble (double& nVal)const char* tok,
{
// Category: Text Processing
//
// Determine if a token could represent a floating point number (double). To qualify, the token must be either:-
//
// a) A series of one or more digits.
// b) (a) followed by a single period followed directly by another series of digits.
// c) (b) followed by a '*10^' followed by another series of digits (standard form number).
//
// All of the above must be followed by a 'valid terminator sequence'. This could be a null terminator but may be a punctuating char as long as that we not
// followed by a number.
//
// Arguments: 1) A pointer to the result (double)
// 2) The token
//
// Returns: True If token could be a value of type double
// False If string of zero length or contains non numeric chars
//
// Errors: Sets an E_ARGUMENT error if either first two arguments are invalid.
_hzfunc("IsDouble") ;
const char* i = tok ; // Input string iterator
double val = 0.0; // Agregated value
double pt_pos = 0.0;// Order of magnitude
bool bNeg = false ; // Negator
if (!tok || !tok[0])
return false ;
// Ignore leading spaces
for (i = tok ; *i == CHAR_SPACE ; i++) ;
// Ceck for minus sign
if (*i == CHAR_MINUS)
{ bNeg = true ; i++ ; }
// Ceck for plus sign
if (*i == CHAR_PLUS)
{
if (bNeg)
return false ;
i++ ;
}
// Ignore spaces between sign and digits
for (; *i == CHAR_SPACE ; i++) ;
for (; *i ; i++)
{
if (!(chartype[*i] & CTYPE_DIGIT))
{
if (*i != CHAR_PERIOD)
return false ;
if (pt_pos)
return false ;
pt_pos = 1.0;
}
val *= 10.0;
val += (double) (*i - CHAR_0) ;
pt_pos *= 10.0;
}
nVal = (val / pt_pos) ;
if (bNeg)
nVal *= -1.0;
return true ;
}