-
October 14th, 2003, 08:38 AM
#16
Not only that. The thing is that floor(-1.000001) is -2.0, so even if your FLT_MIN is only 0.0001 then it will fail.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 14th, 2003, 09:29 AM
#17
Originally posted by Kheun
avi123,
Since you already captured the int/double value in the form of string, why not scan for the decimal point or '.'. If there exists non-zero value character(s) after the decimal point, the string presentation must be a double.
As said before, the string will not contain the '.' character. From what I understand, the captured number is kind of a monetary number, captured without the '.', but it still include 2 decimals (cents).
Checking the last 2 digits might be the more accurate solution, easy enough to implement.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
October 14th, 2003, 10:05 AM
#18
Yves - depends on where/how you use fabs() !
double abs = fabs(dbl);
if ( ( abs - floor(abs) ) < FLT_MIN ) ...
(but you've made good point either way)
-rick
-
October 21st, 2003, 07:01 PM
#19
Yeek. There has been some dodgy code posted here.
Firstly, you cannot use a simple floor() or modf() function. If you do, then 14.99999999999 will be rounded to 14, not 15.
On VC++ 6.0, FLT_MIN is 1.175494351e-38F, which is way too small for your epsilon value.
You might use FLT_EPS, which is 1.192092896e-07F, however you would be better looking at the data and deciding on a suitable epsilon value. You need to consider where the data came from, and how accurate it truely is. If it is only recorded to the nearest 0.1, then an epsilon value of 1.0e-7 is silly.
You might get something like this:
Code:
#include <cmath>
const double epsilon = 0.000001; // choose an appropriate value for this.
double Round(double val)
{
return floor(val+0.5);
}
bool IsInt(double val)
{
return epsilon > fabs(val-Round(val));
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|